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