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