1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace drupol\valuewrapper; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ValueWrapper |
9
|
|
|
*/ |
10
|
|
|
class ValueWrapper implements ValueWrapperInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The storage variable containing the mappings. |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private static $typeClassMapping = [ |
18
|
|
|
'*' => \drupol\valuewrapper\Type\TypeValue::class, |
19
|
|
|
'string' => \drupol\valuewrapper\Type\StringType::class, |
20
|
|
|
'array' => \drupol\valuewrapper\Type\ArrayType::class, |
21
|
|
|
'null' => \drupol\valuewrapper\Type\NullType::class, |
22
|
|
|
'boolean' => \drupol\valuewrapper\Type\BooleanType::class, |
23
|
|
|
'integer' => \drupol\valuewrapper\Type\IntegerType::class, |
24
|
|
|
'double' => \drupol\valuewrapper\Type\DoubleType::class, |
25
|
|
|
'object' => [ |
26
|
|
|
'*' => \drupol\valuewrapper\Object\ObjectValue::class, |
27
|
|
|
'stdClass' => \drupol\valuewrapper\Object\StdClassObject::class, |
28
|
|
|
'Anonymous' => \drupol\valuewrapper\Object\AnonymousObject::class, |
29
|
|
|
'Closure' => \drupol\valuewrapper\Object\ClosureObject::class, |
30
|
|
|
'DateTime' => \drupol\valuewrapper\Object\DateTimeObject::class, |
31
|
|
|
], |
32
|
|
|
'resource' => [ |
33
|
|
|
'*' => \drupol\valuewrapper\Resource\ResourceValue::class, |
34
|
|
|
'stream' => \drupol\valuewrapper\Resource\StreamResource::class, |
35
|
|
|
], |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
1 |
|
public static function create($value) : ValueInterface |
42
|
|
|
{ |
43
|
1 |
|
return (new self())->make($value); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
12 |
|
public function make($value) : ValueInterface |
50
|
|
|
{ |
51
|
12 |
|
$typeClassMapping = self::$typeClassMapping; |
52
|
12 |
|
$type = \strtolower(\gettype($value)); |
53
|
|
|
|
54
|
12 |
|
if ('object' == $type && $class = \get_class($value)) { |
55
|
5 |
|
if ($value instanceof ValueInterface) { |
56
|
|
|
return $value; |
57
|
|
|
} |
58
|
|
|
|
59
|
5 |
|
if (0 === strpos($class, 'class@anonymous')) { |
60
|
1 |
|
$class = 'Anonymous'; |
61
|
|
|
} |
62
|
|
|
|
63
|
5 |
|
$object_wrapper = $typeClassMapping['object'][$class] ?? $typeClassMapping['object']['*']; |
64
|
|
|
|
65
|
5 |
|
return new $object_wrapper($value); |
66
|
|
|
} |
67
|
|
|
|
68
|
7 |
|
if ('resource' == $type && $resourceType = \get_resource_type($value)) { |
69
|
1 |
|
$resource_wrapper = $typeClassMapping['resource'][$resourceType] ?? $typeClassMapping['resource']['*']; |
70
|
|
|
|
71
|
1 |
|
return new $resource_wrapper($value); |
72
|
|
|
} |
73
|
|
|
|
74
|
6 |
|
$type_wrapper = $typeClassMapping[$type] ?? $typeClassMapping['*']; |
75
|
|
|
|
76
|
6 |
|
return new $type_wrapper($value); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|