-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathInt_.php
More file actions
40 lines (32 loc) · 928 Bytes
/
Int_.php
File metadata and controls
40 lines (32 loc) · 928 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
<?php
declare(strict_types=1);
namespace PHPJava\Kernel\Types;
class Int_ extends Type implements PrimitiveValueInterface
{
const DEFAULT_VALUE = 0;
protected $nameInJava = 'int';
protected $nameInPHP = 'integer';
const MIN = -2147483648;
const MAX = 2147483647;
public static function isValid($value): bool
{
if (!is_scalar($value)) {
return false;
}
if (ctype_alpha($value) && strlen((string) $value) === 1) {
$value = ord($value);
}
$value = ($value << 32) >> 32;
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 << 32) >> 32;
}
}