Passed
Push — master ( acd513...675cc6 )
by Andrea Marco
02:53 queued 11s
created

HasProperties::setPropertyValueOrMap()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 2
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 3
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace Cerbero\Dto\Traits;
4
5
use Cerbero\Dto\DtoProperty;
6
use Cerbero\Dto\Exceptions\UnknownDtoPropertyException;
7
8
/**
9
 * Trait to interact with properties.
10
 *
11
 */
12
trait HasProperties
13
{
14
    /**
15
     * The properties map.
16
     *
17
     * @var array
18
     */
19
    protected $propertiesMap;
20
21
    /**
22
     * Retrieve the DTO properties map
23
     *
24
     * @return array
25
     */
26 72
    public function getPropertiesMap(): array
27
    {
28 72
        return $this->propertiesMap;
29
    }
30
31
    /**
32
     * Retrieve the DTO property names
33
     *
34
     * @return array
35
     */
36 18
    public function getPropertyNames(): array
37
    {
38 18
        return array_keys($this->getPropertiesMap());
39
    }
40
41
    /**
42
     * Retrieve the DTO properties
43
     *
44
     * @return DtoProperty[]
45
     */
46 6
    public function getProperties(): array
47
    {
48 6
        return array_values($this->getPropertiesMap());
49
    }
50
51
    /**
52
     * Determine whether the given property is set (even if its value is NULL)
53
     *
54
     * @param string $property
55
     * @return bool
56
     */
57 63
    public function hasProperty(string $property): bool
58
    {
59
        try {
60 63
            return !!$this->getProperty($property);
61 39
        } catch (UnknownDtoPropertyException $e) {
62 39
            return false;
63
        }
64
    }
65
66
    /**
67
     * Retrieve the given DTO property (support dot notation)
68
     *
69
     * @param string $property
70
     * @return DtoProperty
71
     * @throws UnknownDtoPropertyException
72
     */
73 123
    public function getProperty(string $property): DtoProperty
74
    {
75 123
        if (isset($this->propertiesMap[$property])) {
76 84
            return $this->propertiesMap[$property];
77
        }
78
79 72
        if (strpos($property, '.') === false) {
80 66
            throw new UnknownDtoPropertyException(static::class, $property);
81
        }
82
83 24
        [$property, $nestedProperty] = explode('.', $property, 2);
84 24
        $presumedDto = $this->get($property);
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
        /** @scrutinizer ignore-call */ 
85
        $presumedDto = $this->get($property);
Loading history...
85
86 21
        if ($presumedDto instanceof self) {
87 18
            return $presumedDto->getProperty($nestedProperty);
88
        }
89
90 3
        throw new UnknownDtoPropertyException(static::class, $nestedProperty);
91
    }
92
93
    /**
94
     * Retrieve the given DTO property or map it if not mapped yet
95
     *
96
     * @param string $property
97
     * @param mixed $value
98
     * @return void
99
     */
100 36
    protected function setPropertyValueOrMap(string $property, $value): void
101
    {
102 36
        if ($this->hasProperty($property)) {
103 21
            $this->getProperty($property)->setValue($value, $this->getFlags());
0 ignored issues
show
Bug introduced by
It seems like getFlags() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
            $this->getProperty($property)->setValue($value, $this->/** @scrutinizer ignore-call */ getFlags());
Loading history...
104 21
            return;
105
        }
106
107 15
        $data = $this->toArray();
0 ignored issues
show
Bug introduced by
It seems like toArray() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
        /** @scrutinizer ignore-call */ 
108
        $data = $this->toArray();
Loading history...
108
109 15
        if (strpos($property, '.') === false) {
110 9
            $data[$property] = $value;
111
        } else {
112 6
            [$property, $nestedProperty] = explode('.', $property, 2);
113 6
            $data[$property] = $this->resolveNestedValue($nestedProperty, $value);
114
        }
115
116 15
        $this->propertiesMap = $this->mapData($data);
0 ignored issues
show
Bug introduced by
It seems like mapData() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

116
        /** @scrutinizer ignore-call */ 
117
        $this->propertiesMap = $this->mapData($data);
Loading history...
117 12
    }
118
119
    /**
120
     * Retrieve a nested value following the dot notation
121
     *
122
     * @param string $property
123
     * @param mixed $value
124
     * @return array
125
     */
126 6
    protected function resolveNestedValue(string $property, $value): array
127
    {
128 6
        if (strpos($property, '.') === false) {
129 6
            return [$property => $value];
130
        }
131
132 3
        [$property, $nestedProperty] = explode('.', $property, 2);
133
134 3
        return [$property => $this->resolveNestedValue($nestedProperty, $value)];
135
    }
136
}
137