1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\Dto\Traits; |
4
|
|
|
|
5
|
|
|
use Cerbero\Dto\Manipulators\ArrayConverter; |
6
|
|
|
|
7
|
|
|
use const Cerbero\Dto\MUTABLE; |
|
|
|
|
8
|
|
|
use const Cerbero\Dto\NONE; |
|
|
|
|
9
|
|
|
use const Cerbero\Dto\PARTIAL; |
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait to manipulate a DTO data. |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
trait ManipulatesData |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Merge the given data in the DTO |
19
|
|
|
* |
20
|
|
|
* @param iterable $data |
21
|
|
|
* @param int $flags |
22
|
|
|
* @return self |
23
|
|
|
*/ |
24
|
9 |
|
public function merge(iterable $data, int $flags = NONE): self |
25
|
|
|
{ |
26
|
9 |
|
$replacements = ArrayConverter::instance()->convert($data); |
27
|
9 |
|
$mergedData = array_replace_recursive($this->toArray(), $replacements); |
|
|
|
|
28
|
9 |
|
$mergedFlags = $this->mergeFlags($this->getFlags(), $flags); |
|
|
|
|
29
|
|
|
|
30
|
9 |
|
if (!($this->getFlags() & MUTABLE)) { |
31
|
6 |
|
return new static($mergedData, $mergedFlags); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
3 |
|
$this->flags = $mergedFlags; |
|
|
|
|
35
|
3 |
|
$this->propertiesMap = $this->mapData($mergedData); |
|
|
|
|
36
|
|
|
|
37
|
3 |
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Retrieve the DTO including only the given properties |
42
|
|
|
* |
43
|
|
|
* @param array $properties |
44
|
|
|
* @param int $flags |
45
|
|
|
* @return self |
46
|
|
|
*/ |
47
|
12 |
|
public function only(array $properties, int $flags = NONE): self |
48
|
|
|
{ |
49
|
12 |
|
$data = []; |
50
|
12 |
|
$isMutable = $this->getFlags() & MUTABLE; |
51
|
12 |
|
$mergedFlags = $this->mergeFlags($this->getFlagsWithoutDefaults(), $flags | PARTIAL); |
|
|
|
|
52
|
|
|
|
53
|
12 |
|
foreach ($this->getPropertiesMap() as $name => $property) { |
|
|
|
|
54
|
12 |
|
if (in_array($name, $properties) && !$isMutable) { |
55
|
6 |
|
$data[$name] = $property->value(); |
56
|
12 |
|
} elseif (!in_array($name, $properties) && $isMutable) { |
57
|
10 |
|
unset($this->propertiesMap[$name]); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
12 |
|
if ($isMutable) { |
62
|
6 |
|
$this->flags = $mergedFlags; |
|
|
|
|
63
|
6 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
6 |
|
return new static($data, $mergedFlags); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Retrieve the DTO excluding the given properties |
71
|
|
|
* |
72
|
|
|
* @param array $properties |
73
|
|
|
* @param int $flags |
74
|
|
|
* @return self |
75
|
|
|
*/ |
76
|
6 |
|
public function except(array $properties, int $flags = NONE): self |
77
|
|
|
{ |
78
|
6 |
|
$propertiesToKeep = array_diff($this->getPropertyNames(), $properties); |
|
|
|
|
79
|
|
|
|
80
|
6 |
|
return $this->only($propertiesToKeep, $flags); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Make the DTO mutable while calling the given callback |
85
|
|
|
* |
86
|
|
|
* @param callable $callback |
87
|
|
|
* @return self |
88
|
|
|
*/ |
89
|
6 |
|
public function mutate(callable $callback): self |
90
|
|
|
{ |
91
|
6 |
|
$wasMutable = $this->getFlags() & MUTABLE; |
92
|
6 |
|
$this->flags |= MUTABLE; |
93
|
|
|
|
94
|
6 |
|
$callback($this); |
95
|
|
|
|
96
|
6 |
|
if (!$wasMutable) { |
97
|
3 |
|
$this->flags ^= MUTABLE; |
98
|
|
|
} |
99
|
|
|
|
100
|
6 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|