1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\Dto\Traits; |
4
|
|
|
|
5
|
|
|
use Cerbero\Dto\Exceptions\UnknownDtoPropertyException; |
6
|
|
|
use Cerbero\Dto\Exceptions\UnsetDtoPropertyException; |
7
|
|
|
use Cerbero\Dto\Manipulators\Listener; |
8
|
|
|
|
9
|
|
|
use const Cerbero\Dto\IGNORE_UNKNOWN_PROPERTIES; |
|
|
|
|
10
|
|
|
use const Cerbero\Dto\MUTABLE; |
|
|
|
|
11
|
|
|
use const Cerbero\Dto\PARTIAL; |
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Trait to interact with values. |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
trait HasValues |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The default values. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected static $defaultValues = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Retrieve the default values |
28
|
|
|
* |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
81 |
|
public static function getDefaultValues(): array |
32
|
|
|
{ |
33
|
81 |
|
return static::$defaultValues; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Determine whether the given property has a value (return FALSE if the value is NULL) |
38
|
|
|
* |
39
|
|
|
* @param string $property |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
10 |
|
public function has(string $property): bool |
43
|
|
|
{ |
44
|
|
|
try { |
45
|
10 |
|
return $this->get($property) !== null; |
46
|
3 |
|
} catch (UnknownDtoPropertyException $e) { |
47
|
3 |
|
return false; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Retrieve the given property value |
53
|
|
|
* |
54
|
|
|
* @param string $property |
55
|
|
|
* @return mixed |
56
|
|
|
* @throws UnknownDtoPropertyException |
57
|
|
|
*/ |
58
|
33 |
|
public function get(string $property) |
59
|
|
|
{ |
60
|
33 |
|
$value = $this->getProperty($property)->value(); |
|
|
|
|
61
|
|
|
|
62
|
26 |
|
return $this->getListener()->getting(static::class, $property, $value); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Retrieve the listener instance |
67
|
|
|
* |
68
|
|
|
* @return Listener |
69
|
|
|
*/ |
70
|
31 |
|
protected function getListener(): Listener |
71
|
|
|
{ |
72
|
31 |
|
return Listener::instance(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set the given property to the provided value |
77
|
|
|
* |
78
|
|
|
* @param string $property |
79
|
|
|
* @param mixed $value |
80
|
|
|
* @return self |
81
|
|
|
* @throws UnknownDtoPropertyException |
82
|
|
|
*/ |
83
|
14 |
|
public function set(string $property, $value): self |
84
|
|
|
{ |
85
|
14 |
|
$flags = $this->getFlags(); |
|
|
|
|
86
|
14 |
|
$dto = ($flags & MUTABLE) ? $this : $this->clone(); |
|
|
|
|
87
|
|
|
|
88
|
|
|
try { |
89
|
14 |
|
$value = $this->getListener()->setting(static::class, $property, $value); |
90
|
14 |
|
$dto->setPropertyValueOrMap($property, $value); |
|
|
|
|
91
|
1 |
|
} catch (UnknownDtoPropertyException $e) { |
92
|
1 |
|
if (!($flags & IGNORE_UNKNOWN_PROPERTIES)) { |
93
|
1 |
|
throw $e; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
13 |
|
return $dto; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Unset the given property |
102
|
|
|
* |
103
|
|
|
* @param string $property |
104
|
|
|
* @return self |
105
|
|
|
* @throws UnsetDtoPropertyException |
106
|
|
|
* @throws UnknownDtoPropertyException |
107
|
|
|
*/ |
108
|
8 |
|
public function unset(string $property): self |
109
|
|
|
{ |
110
|
8 |
|
$flags = $this->getFlags(); |
111
|
|
|
|
112
|
8 |
|
if (!($flags & PARTIAL)) { |
113
|
2 |
|
throw new UnsetDtoPropertyException(static::class, $property); |
114
|
|
|
} |
115
|
|
|
|
116
|
6 |
|
if (strpos($property, '.') !== false) { |
117
|
3 |
|
[$property, $nestedProperty] = explode('.', $property, 2); |
118
|
3 |
|
$unsetDto = $this->get($property)->unset($nestedProperty); |
119
|
2 |
|
return $this->set($property, $unsetDto); |
120
|
|
|
} |
121
|
|
|
|
122
|
5 |
|
if ($flags & MUTABLE) { |
123
|
3 |
|
unset($this->propertiesMap[$property]); |
|
|
|
|
124
|
3 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
2 |
|
$data = $this->toArray(); |
|
|
|
|
128
|
2 |
|
unset($data[$property]); |
129
|
|
|
|
130
|
2 |
|
return new static($data, $flags); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Determine whether a given property has a value |
135
|
|
|
* |
136
|
|
|
* @param string $property |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
3 |
|
public function __isset(string $property): bool |
140
|
|
|
{ |
141
|
3 |
|
return $this->offsetExists($property); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Retrieve the given property value |
146
|
|
|
* |
147
|
|
|
* @param string $property |
148
|
|
|
* @return mixed |
149
|
|
|
* @throws UnknownDtoPropertyException |
150
|
|
|
*/ |
151
|
9 |
|
public function &__get(string $property) |
152
|
|
|
{ |
153
|
9 |
|
return $this->offsetGet($property); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Set the given property to the provided value |
158
|
|
|
* |
159
|
|
|
* @param string $property |
160
|
|
|
* @param mixed $value |
161
|
|
|
* @return void |
162
|
|
|
* @throws \Cerbero\Dto\Exceptions\ImmutableDtoException |
163
|
|
|
* @throws UnknownDtoPropertyException |
164
|
|
|
*/ |
165
|
4 |
|
public function __set(string $property, $value): void |
166
|
|
|
{ |
167
|
4 |
|
$this->offsetSet($property, $value); |
|
|
|
|
168
|
3 |
|
} |
169
|
|
|
} |
170
|
|
|
|