Completed
Push — master ( 0855a3...de27c0 )
by Pol
09:02 queued 07:39
created

ValueWrapper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 82.35%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 69
ccs 14
cts 17
cp 0.8235
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
B make() 0 29 7
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