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