Completed
Push — master ( 8c7c58...85a264 )
by Pol
04:06
created

ValueWrapper::make()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 19
cts 19
cp 1
rs 9.0968
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5
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 type mappings.
14
     *
15
     * @var array
16
     */
17
    public static $typeMappingRegistry = [
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
    ];
26
27
    /**
28
     * The storage variable containing the object mappings.
29
     *
30
     * @var array
31
     */
32
    public static $objectMappingRegistry = [
33
        '*' => \drupol\valuewrapper\Object\ObjectValue::class,
34
        'stdClass' => \drupol\valuewrapper\Object\StdClassObject::class,
35
        'Anonymous' => \drupol\valuewrapper\Object\AnonymousObject::class,
36
        'Closure' => \drupol\valuewrapper\Object\ClosureObject::class,
37
        'DateTime' => \drupol\valuewrapper\Object\DateTimeObject::class,
38
    ];
39
40
    /**
41
     * The storage variable containing the resource mappings.
42
     *
43
     * @var array
44
     */
45
    public static $resourceMappingRegistry = [
46
        '*' => \drupol\valuewrapper\Resource\ResourceValue::class,
47
        'stream' => \drupol\valuewrapper\Resource\StreamResource::class,
48
    ];
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 2
    public static function create($value) : ValueInterface
54
    {
55 2
        return (new self())->make($value);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 14
    public function make($value) : ValueInterface
62
    {
63 14
        $type = $this->getType($value);
64
65 14
        switch ($type) {
66 14
            case 'object':
67 6
                if ($value instanceof ValueInterface) {
68 1
                    return $value;
69
                }
70
71 5
                $mappings = self::$objectMappingRegistry;
72 5
                $type = \get_class($value);
73
74 5
                if (0 === strpos($type, 'class@anonymous')) {
75 1
                    $type = 'Anonymous';
76
                }
77 5
                break;
78
79 8
            case 'resource':
80 2
                $mappings = self::$resourceMappingRegistry;
81 2
                $type = \get_resource_type($value);
82 2
                break;
83
84
            default:
85 6
                $mappings = self::$typeMappingRegistry;
86 6
                break;
87
        }
88
89 13
        $class = $mappings[$type] ?? $mappings['*'];
90
91 13
        return new $class($value);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 14
    protected function getType($value) : string
98
    {
99 14
        return \strtolower(\gettype($value));
100
    }
101
}
102