-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathChar_.php
More file actions
44 lines (35 loc) · 979 Bytes
/
Char_.php
File metadata and controls
44 lines (35 loc) · 979 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
declare(strict_types=1);
namespace PHPJava\Kernel\Types;
class Char_ extends Type implements PrimitiveValueInterface
{
const DEFAULT_VALUE = 0;
protected $nameInJava = 'char';
protected $nameInPHP = 'string';
const MIN = 0;
const MAX = 65535;
public static function isValid($value): bool
{
if (!is_scalar($value)) {
return false;
}
if (ctype_alpha($value) && strlen((string) $value) === 1) {
$value = ord($value);
}
if (!ctype_digit((string) abs($value))) {
return false;
}
return $value >= static::MIN && $value <= static::MAX;
}
protected static function filter($value)
{
if (ctype_alpha($value) && strlen((string) $value) === 1) {
return ord($value);
}
return $value;
}
public function __toString(): string
{
return json_decode(sprintf('"\\u%04X"', $this->value));
}
}