|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace drupol\valuewrapper; |
|
6
|
|
|
|
|
7
|
|
|
use drupol\valuewrapper\Object\AnonymousObject; |
|
8
|
|
|
use drupol\valuewrapper\Object\ClosureObject; |
|
9
|
|
|
use drupol\valuewrapper\Object\DateTimeObject; |
|
10
|
|
|
use drupol\valuewrapper\Object\Exception\ArithmeticErrorObject; |
|
11
|
|
|
use drupol\valuewrapper\Object\Exception\AssertionErrorObject; |
|
12
|
|
|
use drupol\valuewrapper\Object\Exception\DivisionByZeroErrorObject; |
|
13
|
|
|
use drupol\valuewrapper\Object\Exception\ErrorObject; |
|
14
|
|
|
use drupol\valuewrapper\Object\Exception\ExceptionObject; |
|
15
|
|
|
use drupol\valuewrapper\Object\Exception\ParseErrorObject; |
|
16
|
|
|
use drupol\valuewrapper\Object\Exception\TypeErrorObject; |
|
17
|
|
|
use drupol\valuewrapper\Object\StdClassObject; |
|
18
|
|
|
use drupol\valuewrapper\Resource\StreamResource; |
|
19
|
|
|
use drupol\valuewrapper\Type\ArrayType; |
|
20
|
|
|
use drupol\valuewrapper\Type\BooleanType; |
|
21
|
|
|
use drupol\valuewrapper\Type\DoubleType; |
|
22
|
|
|
use drupol\valuewrapper\Type\IntegerType; |
|
23
|
|
|
use drupol\valuewrapper\Type\NullType; |
|
24
|
|
|
use drupol\valuewrapper\Type\StringType; |
|
25
|
|
|
use OutOfBoundsException; |
|
26
|
|
|
|
|
27
|
|
|
use function get_class; |
|
28
|
|
|
use function gettype; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Class ValueWrapper. |
|
32
|
|
|
*/ |
|
33
|
|
|
class ValueWrapper implements ValueWrapperInterface |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* The storage variable containing the object mappings. |
|
37
|
|
|
* |
|
38
|
|
|
* @var array<string, string> |
|
39
|
|
|
*/ |
|
40
|
|
|
public static $objectMappingRegistry = [ |
|
41
|
|
|
'stdClass' => StdClassObject::class, |
|
42
|
|
|
'Anonymous' => AnonymousObject::class, |
|
43
|
|
|
'Closure' => ClosureObject::class, |
|
44
|
|
|
'DateTime' => DateTimeObject::class, |
|
45
|
|
|
'Exception' => ExceptionObject::class, |
|
46
|
|
|
'Error' => ErrorObject::class, |
|
47
|
|
|
'TypeError' => TypeErrorObject::class, |
|
48
|
|
|
'ArithmeticError' => ArithmeticErrorObject::class, |
|
49
|
|
|
'AssertionError' => AssertionErrorObject::class, |
|
50
|
|
|
'DivisionByZeroError' => DivisionByZeroErrorObject::class, |
|
51
|
|
|
'ParseError' => ParseErrorObject::class, |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* The storage variable containing the resource mappings. |
|
56
|
|
|
* |
|
57
|
|
|
* @var array<string, string> |
|
58
|
|
|
*/ |
|
59
|
|
|
public static $resourceMappingRegistry = [ |
|
60
|
|
|
'stream' => StreamResource::class, |
|
61
|
|
|
]; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* The storage variable containing the type mappings. |
|
65
|
|
|
* |
|
66
|
|
|
* @var array<string, string> |
|
67
|
|
|
*/ |
|
68
|
|
|
public static $typeMappingRegistry = [ |
|
69
|
|
|
'string' => StringType::class, |
|
70
|
|
|
'array' => ArrayType::class, |
|
71
|
|
|
'null' => NullType::class, |
|
72
|
|
|
'boolean' => BooleanType::class, |
|
73
|
|
|
'integer' => IntegerType::class, |
|
74
|
|
|
'double' => DoubleType::class, |
|
75
|
|
|
]; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
*/ |
|
80
|
10 |
|
public static function create($value): ValueInterface |
|
81
|
|
|
{ |
|
82
|
10 |
|
return (new self())->make($value); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* {@inheritdoc} |
|
87
|
|
|
*/ |
|
88
|
23 |
|
public function make($value): ValueInterface |
|
89
|
|
|
{ |
|
90
|
23 |
|
$type = $this->getType($value); |
|
91
|
|
|
|
|
92
|
23 |
|
switch ($type) { |
|
93
|
23 |
|
case 'object': |
|
94
|
15 |
|
if ($value instanceof ValueInterface) { |
|
95
|
1 |
|
return $value; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
14 |
|
$mappings = self::$objectMappingRegistry; |
|
99
|
14 |
|
$type = get_class($value); |
|
100
|
|
|
|
|
101
|
14 |
|
if (0 === mb_strpos($type, 'class@anonymous')) { |
|
102
|
1 |
|
$type = 'Anonymous'; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
14 |
|
break; |
|
106
|
8 |
|
case 'resource': |
|
107
|
2 |
|
$mappings = self::$resourceMappingRegistry; |
|
108
|
2 |
|
$type = get_resource_type($value); |
|
109
|
|
|
|
|
110
|
2 |
|
break; |
|
111
|
|
|
|
|
112
|
|
|
default: |
|
113
|
6 |
|
$mappings = self::$typeMappingRegistry; |
|
114
|
|
|
|
|
115
|
6 |
|
break; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
22 |
|
if (isset($mappings[$type])) { |
|
119
|
21 |
|
return new $mappings[$type]($value); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
1 |
|
throw new OutOfBoundsException( |
|
123
|
1 |
|
sprintf('Unable to find a wrapping class for value type "%s".', $type) |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param mixed $value |
|
129
|
|
|
* |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
23 |
|
protected function getType($value): string |
|
133
|
|
|
{ |
|
134
|
23 |
|
return mb_strtolower(gettype($value)); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|