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); |
|
|
|
|
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()); |
|
|
|
|
104
|
21 |
|
return; |
105
|
|
|
} |
106
|
|
|
|
107
|
15 |
|
$data = $this->toArray(); |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|