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
|
|
|
'Anonymous' => \drupol\valuewrapper\Object\AnonymousObject::class, |
26
|
|
|
'Closure' => \drupol\valuewrapper\Object\ClosureObject::class, |
27
|
|
|
'DateTime' => \drupol\valuewrapper\Object\DateTimeObject::class, |
28
|
|
|
], |
29
|
|
|
'resource' => [ |
30
|
|
|
'*' => \drupol\valuewrapper\Resource\ResourceValue::class, |
31
|
|
|
'Stream' => \drupol\valuewrapper\Resource\StreamResource::class, |
32
|
|
|
], |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public static function create($value) : ValueInterface |
39
|
|
|
{ |
40
|
|
|
return (new self())->make($value); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
8 |
|
public function make($value) : ValueInterface |
47
|
|
|
{ |
48
|
8 |
|
$typeClassMapping = self::$typeClassMapping; |
49
|
8 |
|
$type = \strtolower(\gettype($value)); |
50
|
|
|
|
51
|
8 |
|
if ('object' === $type && $class = \get_class($value)) { |
52
|
1 |
|
if ($value instanceof ValueInterface) { |
53
|
|
|
return $value; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
if (0 === strpos($class, 'class@anonymous')) { |
57
|
|
|
$class = 'Anonymous'; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
$object_wrapper = $typeClassMapping['object'][$class] ?? $typeClassMapping['object']['*']; |
61
|
|
|
|
62
|
1 |
|
return new $object_wrapper($value); |
63
|
|
|
} |
64
|
|
|
|
65
|
7 |
|
if ('resource' === $type && $resourceType = \get_resource_type($value)) { |
66
|
1 |
|
$resource_wrapper = $typeClassMapping['resource'][$resourceType] ?? $typeClassMapping['resource']['*']; |
67
|
|
|
|
68
|
1 |
|
return new $resource_wrapper($value); |
69
|
|
|
} |
70
|
|
|
|
71
|
6 |
|
$type_wrapper = $typeClassMapping[$type] ?? $typeClassMapping['*']; |
72
|
|
|
|
73
|
6 |
|
return new $type_wrapper($value); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|