|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: daniel.jurkovic |
|
5
|
|
|
* Date: 15.06.17 |
|
6
|
|
|
* Time: 13:23 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Macroparts\Vortex\ArrayTools; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class Patcher |
|
13
|
|
|
* @package Macroparts\Vortex\ArrayPatcher |
|
14
|
|
|
*/ |
|
15
|
|
|
class Reorganizer |
|
16
|
|
|
{ |
|
17
|
|
|
const CAST = 'executeCast'; |
|
18
|
|
|
const READ = 'executeRead'; |
|
19
|
|
|
const WRITE = 'executeWrite'; |
|
20
|
|
|
const DELETE = 'scheduleUnset'; |
|
21
|
|
|
const CUSTOM_TRANSFORM = 'executeCustom'; |
|
22
|
|
|
|
|
23
|
|
|
private $scheduledUnsets = []; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Execute $tasks on $array and return result |
|
27
|
|
|
* @param array $array |
|
28
|
|
|
* @param array[] $tasks |
|
29
|
|
|
* @return array (patched) |
|
30
|
|
|
*/ |
|
31
|
18 |
|
public function patchArray(&$array, $tasks) |
|
32
|
|
|
{ |
|
33
|
18 |
|
$temporaryValue = null; |
|
34
|
|
|
|
|
35
|
18 |
|
while ($tasks) { |
|
|
|
|
|
|
36
|
18 |
|
$taskName = array_shift($tasks); |
|
37
|
18 |
|
$taskParams = array_shift($tasks); |
|
38
|
18 |
|
$this->runTask($array, $taskName, $taskParams, $temporaryValue); |
|
39
|
18 |
|
} |
|
40
|
|
|
|
|
41
|
18 |
|
$this->executeUnsets($array); |
|
42
|
|
|
|
|
43
|
18 |
|
return $array; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param array $array |
|
48
|
|
|
*/ |
|
49
|
18 |
|
private function executeUnsets(&$array) |
|
50
|
|
|
{ |
|
51
|
18 |
|
foreach ($this->scheduledUnsets as $path => $nobodyCares) { |
|
52
|
6 |
|
RecursiveAccessor::unsetInPath($array, $path); |
|
53
|
18 |
|
} |
|
54
|
18 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @uses executeCast |
|
58
|
|
|
* @uses executeRead |
|
59
|
|
|
* @uses executeWrite |
|
60
|
|
|
* @uses scheduleUnset |
|
61
|
|
|
* @uses executeCustom |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $array |
|
64
|
|
|
* @param string $taskName |
|
65
|
|
|
* @param mixed $taskParams |
|
66
|
|
|
* @param mixed $temporaryValue |
|
67
|
|
|
*/ |
|
68
|
18 |
|
private function runTask(&$array, $taskName, $taskParams, &$temporaryValue) |
|
69
|
|
|
{ |
|
70
|
18 |
|
$this->$taskName($array, $temporaryValue, $taskParams); |
|
71
|
18 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param array $array |
|
75
|
|
|
* @param mixed $temporaryValue |
|
76
|
|
|
* @param string $path |
|
77
|
|
|
*/ |
|
78
|
15 |
|
private function executeRead(&$array, &$temporaryValue, $path) |
|
79
|
|
|
{ |
|
80
|
15 |
|
$temporaryValue = RecursiveAccessor::readSingleFromPath($array, $path); |
|
81
|
15 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param array $array |
|
85
|
|
|
* @param mixed $temporaryValue |
|
86
|
|
|
* @param string $path |
|
87
|
|
|
*/ |
|
88
|
15 |
|
private function executeWrite(&$array, $temporaryValue, $path) |
|
89
|
|
|
{ |
|
90
|
15 |
|
RecursiveAccessor::integrateIntoPath($array, $path, $temporaryValue); |
|
91
|
15 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param array $array |
|
95
|
|
|
* @param mixed $temporaryValue |
|
96
|
|
|
* @param string $path |
|
97
|
|
|
*/ |
|
98
|
6 |
|
private function scheduleUnset(&$array, &$temporaryValue, $path) |
|
|
|
|
|
|
99
|
|
|
{ |
|
100
|
6 |
|
$this->scheduledUnsets[$path] = 1; |
|
101
|
6 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param array $array |
|
105
|
|
|
* @param mixed $temporaryValue |
|
106
|
|
|
* @param string $type |
|
107
|
|
|
*/ |
|
108
|
3 |
|
private function executeCast(&$array, &$temporaryValue, $type) |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
3 |
|
settype($temporaryValue, $type); |
|
111
|
3 |
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param array $array |
|
115
|
|
|
* @param mixed $temporaryValue |
|
116
|
|
|
* @param array $customFunction |
|
117
|
|
|
*/ |
|
118
|
3 |
|
private function executeCustom(&$array, &$temporaryValue, $customFunction) |
|
119
|
|
|
{ |
|
120
|
3 |
|
list($callable, $parameters) = $customFunction; |
|
121
|
3 |
|
call_user_func_array($callable, array_merge([&$array, &$temporaryValue], $parameters)); |
|
122
|
3 |
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.