-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathType.php
More file actions
110 lines (95 loc) Β· 2.31 KB
/
Type.php
File metadata and controls
110 lines (95 loc) Β· 2.31 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
declare(strict_types=1);
namespace PHPJava\Kernel\Types;
use PHPJava\Exceptions\TypeException;
abstract class Type
{
const DEFAULT_VALUE = null;
/**
* @var null|self
*/
protected $value;
/**
* @var string
*/
protected $nameInJava;
/**
* @var string
*/
protected $nameInPHP;
/**
* @param null|mixed $value
* @throws TypeException
*/
public function __construct($value = null)
{
if (!($value instanceof self) &&
$value !== null &&
!static::isValid($value)
) {
throw new TypeException(
'"' . ((string) $value) . '" is not expected in ' . get_class($this) . '.'
);
}
$this->value = static::filter(
($value instanceof self)
? $value->getValue()
: $value
);
}
public function __debugInfo()
{
return [
'value' => $this->value,
];
}
/**
* @param null|mixed $value
* @throws TypeException
*/
public static function get($value = null): self
{
static $instantiated = null;
if ($value === null) {
if (static::DEFAULT_VALUE === null) {
throw new TypeException('The type has not default value.');
}
return static::get(
static::DEFAULT_VALUE
);
}
if (is_object($value)) {
if ($value instanceof static) {
return $value;
}
$identity = spl_object_hash($value);
} else {
$identity = (string) $value;
}
return $instantiated[$identity] = $instantiated[$identity] ?? new static($value);
}
public function getValue()
{
return $this->value;
}
public function getTypeNameInJava(): string
{
return $this->nameInJava;
}
public function getTypeNameInPHP(): string
{
return $this->nameInPHP;
}
public function __toString(): string
{
return (string) $this->getValue();
}
public static function isValid($value): bool
{
throw new TypeException('Not implemented type validation.');
}
protected static function filter($value)
{
return $value;
}
}