DtoPropertyType   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 26
c 2
b 0
f 0
dl 0
loc 118
ccs 27
cts 27
cp 1
rs 10
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isCollection() 0 3 1
A __construct() 0 4 1
A name() 0 3 1
A matchesCollection() 0 15 4
A matches() 0 11 4
A isDto() 0 3 1
A declaredName() 0 3 2
1
<?php
2
3
namespace Cerbero\Dto;
4
5
/**
6
 * The type of a DTO property.
7
 *
8
 */
9
class DtoPropertyType
10
{
11
    /**
12
     * The primitive types map
13
     *
14
     */
15
    public const TYPES_MAP = [
16
        'int' => 'integer',
17
        'bool' => 'boolean',
18
        'float' => 'double',
19
    ];
20
21
    /**
22
     * The actual type name.
23
     *
24
     * @var string
25
     */
26
    protected $name;
27
28
    /**
29
     * Whether it is a collection of the same type.
30
     *
31
     * @var bool
32
     */
33
    protected $isCollection;
34
35
    /**
36
     * Instantiate the class
37
     *
38
     * @param string $name
39
     * @param bool $isCollection
40
     */
41 113
    public function __construct(string $name, bool $isCollection)
42
    {
43 113
        $this->name = $name;
44 113
        $this->isCollection = $isCollection;
45 113
    }
46
47
    /**
48
     * Retrieve the type name
49
     *
50
     * @return string
51
     */
52 113
    public function name(): string
53
    {
54 113
        return $this->name;
55
    }
56
57
    /**
58
     * Determine whether the type is a collection
59
     *
60
     * @return bool
61
     */
62 113
    public function isCollection(): bool
63
    {
64 113
        return $this->isCollection;
65
    }
66
67
    /**
68
     * Retrieve the declared name
69
     *
70
     * @return string
71
     */
72 104
    public function declaredName(): string
73
    {
74 104
        return $this->isCollection() ? $this->name() . '[]' : $this->name();
75
    }
76
77
    /**
78
     * Determine whether the current property type matches the given value
79
     *
80
     * @param mixed $value
81
     * @return bool
82
     */
83 84
    public function matches($value): bool
84
    {
85 84
        if ($this->isCollection()) {
86 1
            return $this->matchesCollection($value);
87
        }
88
89 83
        $name = $this->name();
90
91 83
        return $name == 'mixed' ||
92 81
            is_a($value, $name) ||
93 83
            gettype($value) == (static::TYPES_MAP[$name] ?? $name);
94
    }
95
96
    /**
97
     * Determine whether the current property type matches the given collection items
98
     *
99
     * @param mixed $collection
100
     * @return bool
101
     */
102 6
    public function matchesCollection($collection): bool
103
    {
104 6
        if (!is_iterable($collection)) {
105 2
            return false;
106
        }
107
108 4
        $this->isCollection = false;
109
110 4
        foreach ($collection as $item) {
111 4
            if (!$this->matches($item)) {
112 1
                return false;
113
            }
114
        }
115
116 3
        return $this->isCollection = true;
117
    }
118
119
    /**
120
     * Determine whether the current property type is a DTO
121
     *
122
     * @return bool
123
     */
124 104
    public function isDto(): bool
125
    {
126 104
        return is_subclass_of($this->name(), Dto::class);
127
    }
128
}
129