|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cerbero\Dto\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Cerbero\Dto\DtoPropertiesMapper; |
|
6
|
|
|
use Cerbero\Dto\DtoProperty; |
|
7
|
|
|
use Cerbero\Dto\Exceptions\UnknownDtoPropertyException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Trait to interact with properties. |
|
11
|
|
|
* |
|
12
|
|
|
*/ |
|
13
|
|
|
trait HasProperties |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* The properties map. |
|
17
|
|
|
* |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $propertiesMap; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Retrieve the DTO properties map |
|
24
|
|
|
* |
|
25
|
|
|
* @return array |
|
26
|
|
|
*/ |
|
27
|
96 |
|
public function getPropertiesMap(): array |
|
28
|
|
|
{ |
|
29
|
96 |
|
return $this->propertiesMap; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Retrieve the DTO property names |
|
34
|
|
|
* |
|
35
|
|
|
* @return array |
|
36
|
|
|
*/ |
|
37
|
21 |
|
public function getPropertyNames(): array |
|
38
|
|
|
{ |
|
39
|
21 |
|
return array_keys($this->getPropertiesMap()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Retrieve the DTO properties |
|
44
|
|
|
* |
|
45
|
|
|
* @return DtoProperty[] |
|
46
|
|
|
*/ |
|
47
|
6 |
|
public function getProperties(): array |
|
48
|
|
|
{ |
|
49
|
6 |
|
return array_values($this->getPropertiesMap()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Determine whether the given property is set (even if its value is NULL) |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $property |
|
56
|
|
|
* @return bool |
|
57
|
|
|
*/ |
|
58
|
69 |
|
public function hasProperty(string $property): bool |
|
59
|
|
|
{ |
|
60
|
|
|
try { |
|
61
|
69 |
|
return !!$this->getProperty($property); |
|
62
|
45 |
|
} catch (UnknownDtoPropertyException $e) { |
|
63
|
45 |
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Retrieve the given DTO property (support dot notation) |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $property |
|
71
|
|
|
* @return DtoProperty |
|
72
|
|
|
* @throws UnknownDtoPropertyException |
|
73
|
|
|
*/ |
|
74
|
129 |
|
public function getProperty(string $property): DtoProperty |
|
75
|
|
|
{ |
|
76
|
129 |
|
if (isset($this->propertiesMap[$property])) { |
|
77
|
84 |
|
return $this->propertiesMap[$property]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
78 |
|
if (strpos($property, '.') === false) { |
|
81
|
72 |
|
throw new UnknownDtoPropertyException(static::class, $property); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
24 |
|
[$property, $nestedProperty] = explode('.', $property, 2); |
|
85
|
24 |
|
$presumedDto = $this->get($property); |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
21 |
|
if ($presumedDto instanceof self) { |
|
88
|
18 |
|
return $presumedDto->getProperty($nestedProperty); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
3 |
|
throw new UnknownDtoPropertyException(static::class, $nestedProperty); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Retrieve the given DTO property or map it if not mapped yet |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $property |
|
98
|
|
|
* @param mixed $value |
|
99
|
|
|
* @return void |
|
100
|
|
|
*/ |
|
101
|
42 |
|
protected function setPropertyValueOrMap(string $property, $value): void |
|
102
|
|
|
{ |
|
103
|
42 |
|
if ($this->hasProperty($property)) { |
|
104
|
21 |
|
$this->getProperty($property)->setValue($value, $this->getFlags()); |
|
|
|
|
|
|
105
|
21 |
|
return; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
21 |
|
$data = $this->toArray(); |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
21 |
|
if (strpos($property, '.') === false) { |
|
111
|
15 |
|
$data[$property] = $value; |
|
112
|
|
|
} else { |
|
113
|
6 |
|
[$property, $nestedProperty] = explode('.', $property, 2); |
|
114
|
6 |
|
$data[$property] = $this->resolveNestedValue($nestedProperty, $value); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
21 |
|
$this->propertiesMap = $this->mapData($data); |
|
118
|
18 |
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Retrieve the DTO mapped properties for the given data |
|
122
|
|
|
* |
|
123
|
|
|
* @param array $data |
|
124
|
|
|
* @return array |
|
125
|
|
|
* @throws \Cerbero\Dto\Exceptions\DtoNotFoundException |
|
126
|
|
|
* @throws \Cerbero\Dto\Exceptions\InvalidDocCommentException |
|
127
|
|
|
* @throws \Cerbero\Dto\Exceptions\MissingValueException |
|
128
|
|
|
* @throws \Cerbero\Dto\Exceptions\UnexpectedValueException |
|
129
|
|
|
* @throws UnknownDtoPropertyException |
|
130
|
|
|
*/ |
|
131
|
240 |
|
protected function mapData(array $data): array |
|
132
|
|
|
{ |
|
133
|
240 |
|
return DtoPropertiesMapper::for(static::class)->map($data, $this->getFlags()); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Retrieve a nested value following the dot notation |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $property |
|
140
|
|
|
* @param mixed $value |
|
141
|
|
|
* @return array |
|
142
|
|
|
*/ |
|
143
|
6 |
|
protected function resolveNestedValue(string $property, $value): array |
|
144
|
|
|
{ |
|
145
|
6 |
|
if (strpos($property, '.') === false) { |
|
146
|
6 |
|
return [$property => $value]; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
3 |
|
[$property, $nestedProperty] = explode('.', $property, 2); |
|
150
|
|
|
|
|
151
|
3 |
|
return [$property => $this->resolveNestedValue($nestedProperty, $value)]; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|