Completed
Push — master ( 0bd9fa...07ff92 )
by Pol
04:46
created

ValueWrapper::make()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6

Importance

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