1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\Dto\Traits; |
4
|
|
|
|
5
|
|
|
use Cerbero\Dto\Dto; |
6
|
|
|
use Cerbero\Dto\DtoFlagsHandler; |
7
|
|
|
|
8
|
|
|
use const Cerbero\Dto\ARRAY_DEFAULT_TO_EMPTY_ARRAY; |
|
|
|
|
9
|
|
|
use const Cerbero\Dto\BOOL_DEFAULT_TO_FALSE; |
|
|
|
|
10
|
|
|
use const Cerbero\Dto\CAST_PRIMITIVES; |
|
|
|
|
11
|
|
|
use const Cerbero\Dto\MUTABLE; |
|
|
|
|
12
|
|
|
use const Cerbero\Dto\NONE; |
|
|
|
|
13
|
|
|
use const Cerbero\Dto\NOT_NULLABLE; |
|
|
|
|
14
|
|
|
use const Cerbero\Dto\NULLABLE; |
|
|
|
|
15
|
|
|
use const Cerbero\Dto\NULLABLE_DEFAULT_TO_NULL; |
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Trait to interact with flags. |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
trait HasFlags |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The default flags. |
25
|
|
|
* |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
protected static $defaultFlags = NONE; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The actual flags. |
32
|
|
|
* |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
protected $flags; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Retrieve the default flags |
39
|
|
|
* |
40
|
|
|
* @return int |
41
|
|
|
*/ |
42
|
243 |
|
public static function getDefaultFlags(): int |
43
|
|
|
{ |
44
|
243 |
|
return static::$defaultFlags; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Retrieve the DTO flags |
49
|
|
|
* |
50
|
|
|
* @return int |
51
|
|
|
*/ |
52
|
240 |
|
public function getFlags(): int |
53
|
|
|
{ |
54
|
240 |
|
return $this->flags; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Determine whether the DTO flags include the given flags |
59
|
|
|
* |
60
|
|
|
* @param int $flags |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
3 |
|
public function hasFlags(int $flags): bool |
64
|
|
|
{ |
65
|
3 |
|
return ($this->getFlags() & $flags) === $flags; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Set the DTO flags |
70
|
|
|
* |
71
|
|
|
* @param int $flags |
72
|
|
|
* @return Dto |
73
|
|
|
*/ |
74
|
21 |
|
public function setFlags(int $flags): Dto |
75
|
|
|
{ |
76
|
21 |
|
$currentFlags = $this->getFlags(); |
77
|
|
|
|
78
|
21 |
|
if (!($currentFlags & MUTABLE)) { |
79
|
12 |
|
return static::make($this->toArray(), $flags); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
9 |
|
$this->flags = $flags; |
83
|
|
|
|
84
|
9 |
|
if (($currentFlags | $flags) & $this->getFlagsAffectingValues()) { |
85
|
6 |
|
$this->mapData($this->toArray()); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
9 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Retrieve the flags that affect the values of a DTO |
93
|
|
|
* |
94
|
|
|
* @return int |
95
|
|
|
*/ |
96
|
9 |
|
protected function getFlagsAffectingValues(): int |
97
|
|
|
{ |
98
|
9 |
|
return $this->getFlagsForDefaults() | NULLABLE | NOT_NULLABLE | CAST_PRIMITIVES; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Retrieve the flags that determine default values |
103
|
|
|
* |
104
|
|
|
* @return int |
105
|
|
|
*/ |
106
|
21 |
|
protected function getFlagsForDefaults(): int |
107
|
|
|
{ |
108
|
21 |
|
return NULLABLE_DEFAULT_TO_NULL | BOOL_DEFAULT_TO_FALSE | ARRAY_DEFAULT_TO_EMPTY_ARRAY; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Add the given flags to the DTO flags |
113
|
|
|
* |
114
|
|
|
* @param int $flags |
115
|
|
|
* @return Dto |
116
|
|
|
*/ |
117
|
6 |
|
public function addFlags(int $flags): Dto |
118
|
|
|
{ |
119
|
6 |
|
$mergedFlags = $this->mergeFlags($this->getFlags(), $flags); |
120
|
|
|
|
121
|
6 |
|
return $this->setFlags($mergedFlags); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Retrieve the merged flags |
126
|
|
|
* |
127
|
|
|
* @param int $initialFlags |
128
|
|
|
* @param int $flagsToMerge |
129
|
|
|
* @return int |
130
|
|
|
* @throws \Cerbero\Dto\Exceptions\IncompatibleDtoFlagsException |
131
|
|
|
*/ |
132
|
240 |
|
protected function mergeFlags(int $initialFlags, int $flagsToMerge): int |
133
|
|
|
{ |
134
|
240 |
|
return (new DtoFlagsHandler())->merge($initialFlags, $flagsToMerge); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Remove the given flags from the DTO flags |
139
|
|
|
* |
140
|
|
|
* @param int $flags |
141
|
|
|
* @return Dto |
142
|
|
|
*/ |
143
|
6 |
|
public function removeFlags(int $flags): Dto |
144
|
|
|
{ |
145
|
6 |
|
$currentFlags = $this->getFlags(); |
146
|
6 |
|
$remainingFlags = $currentFlags ^ ($currentFlags & $flags); |
147
|
|
|
|
148
|
6 |
|
return $this->setFlags($remainingFlags); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Retrieve the DTO flags excluding the flags for default values |
153
|
|
|
* |
154
|
|
|
* @return int |
155
|
|
|
*/ |
156
|
12 |
|
protected function getFlagsWithoutDefaults(): int |
157
|
|
|
{ |
158
|
12 |
|
$flags = $this->getFlags(); |
159
|
|
|
|
160
|
12 |
|
return $flags ^ ($flags & $this->getFlagsForDefaults()); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|