|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author Marwan Al-Soltany <[email protected]> |
|
5
|
|
|
* @copyright Marwan Al-Soltany 2020 |
|
6
|
|
|
* For the full copyright and license information, please view |
|
7
|
|
|
* the LICENSE file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace MAKS\AmqpAgent\Worker; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* A trait containing the implementation of members mutation functions. |
|
16
|
|
|
* @since 1.0.0 |
|
17
|
|
|
*/ |
|
18
|
|
|
trait WorkerMutationTrait |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* The last mutation happened to a class member (for debugging purposes). |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $mutation = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Mutates a subset of an array (class property) and returns the replaced subset. |
|
28
|
|
|
* @param string $member The name of the property. |
|
29
|
|
|
* @param array $overrides An associative array of the overrides. |
|
30
|
|
|
* @return array |
|
31
|
|
|
*/ |
|
32
|
15 |
|
protected function mutateClassMember(string $member, array $overrides): array |
|
33
|
|
|
{ |
|
34
|
15 |
|
return $this->mutateClass($member, null, $overrides); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Mutates a subset of an array inside a class property (nested array inside a property) and returns the replaced subset. |
|
39
|
|
|
* @param string $member The name of the property. |
|
40
|
|
|
* @param string $sub The key which under the array stored. |
|
41
|
|
|
* @param array $overrides An associative array of the overrides. |
|
42
|
|
|
* @return array |
|
43
|
|
|
*/ |
|
44
|
4 |
|
protected function mutateClassSubMember(string $member, string $sub, array $overrides): array |
|
45
|
|
|
{ |
|
46
|
4 |
|
return $this->mutateClass($member, $sub, $overrides); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Mutates a class property nested or not and returns the replaced subset. |
|
51
|
|
|
* @param string $member The name of the property. |
|
52
|
|
|
* @param string|null $sub [optional] The key which under the array stored. |
|
53
|
|
|
* @param array $overrides An associative array of the overrides. |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
17 |
|
private function mutateClass(string $member, ?string $sub, array $overrides): array |
|
57
|
|
|
{ |
|
58
|
17 |
|
$changes = []; |
|
59
|
17 |
|
$signature = '@UNKNOWN[%s]'; |
|
60
|
|
|
|
|
61
|
17 |
|
foreach ($overrides as $key => $value) { |
|
62
|
17 |
|
if ($sub) { |
|
63
|
4 |
|
if (isset($this->{$member}[$sub][$key])) { |
|
64
|
4 |
|
$changes[$key] = $this->{$member}[$sub][$key]; |
|
65
|
|
|
} else { |
|
66
|
4 |
|
$changes[$key] = sprintf($signature, $key); |
|
67
|
|
|
} |
|
68
|
4 |
|
if ($value === sprintf($signature, $key)) { |
|
69
|
4 |
|
unset($this->{$member}[$sub][$key]); |
|
70
|
|
|
} else { |
|
71
|
4 |
|
$this->{$member}[$sub][$key] = $value; |
|
72
|
|
|
} |
|
73
|
|
|
} else { |
|
74
|
15 |
|
if (isset($this->{$member}[$key])) { |
|
75
|
15 |
|
$changes[$key] = $this->{$member}[$key]; |
|
76
|
|
|
} else { |
|
77
|
2 |
|
$changes[$key] = sprintf($signature, $key); |
|
78
|
|
|
} |
|
79
|
15 |
|
if ($value === sprintf($signature, $key)) { |
|
80
|
2 |
|
unset($this->{$member}[$key]); |
|
81
|
|
|
} else { |
|
82
|
15 |
|
$this->{$member}[$key] = $value; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
17 |
|
$this->mutation = [ |
|
88
|
17 |
|
'member' => $member, |
|
89
|
17 |
|
'old' => $changes, |
|
90
|
17 |
|
'new' => $overrides, |
|
91
|
|
|
]; |
|
92
|
|
|
|
|
93
|
17 |
|
return $changes; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|