1
|
|
|
<?php |
2
|
|
|
namespace JClaveau\Arrays; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* This class is meant to provide an oriented object way to use arrays |
6
|
|
|
* in PHP. |
7
|
|
|
* |
8
|
|
|
* This file contains only the essential API of the class. The rest is |
9
|
|
|
* gathered by feature in traits: |
10
|
|
|
* + One for the ArrayAcess interface |
11
|
|
|
* + One for the native functions of PHP. |
12
|
|
|
* + One for functions that do ot exist in PHP. |
13
|
|
|
* |
14
|
|
|
* |
15
|
|
|
* @todo : |
16
|
|
|
* + recursive |
17
|
|
|
*/ |
18
|
|
|
class ChainableArray implements \ArrayAccess, \IteratorAggregate, \JsonSerializable, \Countable |
19
|
|
|
{ |
20
|
|
|
use ChainableArray_ArrayAccess_Trait; |
21
|
|
|
use ChainableArray_NativeFunctions_Trait; |
22
|
|
|
use ChainableArray_Utils_Trait; |
23
|
|
|
use ChainableArray_Wip_Trait; |
24
|
|
|
|
25
|
|
|
protected $data; |
26
|
|
|
protected $isConstant; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
*/ |
30
|
|
|
public static function from(array $data=[], $isConstant=false) |
31
|
|
|
{ |
32
|
|
|
return new static($data, $isConstant); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
*/ |
37
|
|
|
public function __construct(array $data=[], $isConstant=false) |
38
|
|
|
{ |
39
|
|
|
$this->data = $data; |
40
|
|
|
$this->isConstant = $isConstant; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Getter of the array behid the helper classe. |
45
|
|
|
* |
46
|
|
|
* @return array The data of this array |
47
|
|
|
*/ |
48
|
|
|
public function getArray() |
49
|
|
|
{ |
50
|
|
|
return $this->data; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Deprecated alias of getArray() |
55
|
|
|
* |
56
|
|
|
* @deprecated |
57
|
|
|
*/ |
58
|
|
|
public function getData() |
59
|
|
|
{ |
60
|
|
|
return $this->data; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @see self::getData() |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
public function toArray() |
68
|
|
|
{ |
69
|
|
|
return $this->data; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Changes the "constant" state of this instance. If it is "constant", |
74
|
|
|
* any modification done by one of its methods will return a copy of |
75
|
|
|
* the current instance instead of modifying it. |
76
|
|
|
* |
77
|
|
|
* @param bool $state Whether to enable or not the constant state. |
78
|
|
|
* |
79
|
|
|
* @return ChainableArray $this |
80
|
|
|
*/ |
81
|
|
|
public function setConstant($state=true) |
82
|
|
|
{ |
83
|
|
|
$this->isConstant = $state; |
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Depending on the isConstant property of the instance the data |
89
|
|
|
* will be impacted by the methods like mergeWith or not. |
90
|
|
|
* |
91
|
|
|
* @param array $out The modified array to return |
92
|
|
|
* |
93
|
|
|
* @return ChainableArray $this or a new instance having $out as data. |
94
|
|
|
*/ |
95
|
|
|
protected function returnConstant(array $out) |
96
|
|
|
{ |
97
|
|
|
if ($this->isConstant) { |
98
|
|
|
return new static($out); |
99
|
|
|
} |
100
|
|
|
else { |
101
|
|
|
$this->data = $out; |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @deprecated |
108
|
|
|
*/ |
109
|
|
|
public function clone_() |
110
|
|
|
{ |
111
|
|
|
return $this->copy(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* clone the current object |
116
|
|
|
*/ |
117
|
|
|
public function copy() |
118
|
|
|
{ |
119
|
|
|
return clone $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Make foreach() possible on this object. |
124
|
|
|
* This doesn't look to work well on every PHP version but is tested |
125
|
|
|
* on 5.6.25. |
126
|
|
|
* We use a generator here to support foreach with references. |
127
|
|
|
* @see http://stackoverflow.com/questions/29798586/php-an-iterator-cannot-be-used-with-foreach-by-reference#29798888 |
128
|
|
|
*/ |
129
|
|
|
public function &getIterator() |
130
|
|
|
{ |
131
|
|
|
foreach ($this->data as $position => &$row) { |
132
|
|
|
yield $position => $row; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* This method is required to have the good value while we want to |
138
|
|
|
* json_encode an instance of this class. |
139
|
|
|
* |
140
|
|
|
* @see http://stackoverflow.com/questions/4697656/using-json-encode-on-objects-in-php-regardless-of-scope |
141
|
|
|
*/ |
142
|
|
|
public function jsonSerialize() |
143
|
|
|
{ |
144
|
|
|
return $this->data; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* This method is required by the Countable interface. |
149
|
|
|
* |
150
|
|
|
* @see https://secure.php.net/manual/en/class.countable.php |
151
|
|
|
*/ |
152
|
|
|
public function count() |
153
|
|
|
{ |
154
|
|
|
return count($this->data); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Checks if an argument is an array or a ChainableArray |
159
|
|
|
*/ |
160
|
|
|
private function argumentIsArrayOrArrayObject( $argument ) |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
return is_array($argument) |
163
|
|
|
|| $argument instanceof ChainableArray; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Throws an exception that will have, as file and line, the position |
168
|
|
|
* where the public api is called. |
169
|
|
|
*/ |
170
|
|
|
private static function throwUsageException($message) |
|
|
|
|
171
|
|
|
{ |
172
|
|
|
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 20); |
173
|
|
|
$i = 1; |
174
|
|
|
$caller = $backtrace[$i]; |
175
|
|
|
while ( isset($backtrace[$i]['class']) |
176
|
|
|
&& $backtrace[$i]['class'] == __CLASS__ ) { |
177
|
|
|
$caller = $backtrace[$i]; |
178
|
|
|
$i++; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$exception = new \ErrorException($message); |
182
|
|
|
|
183
|
|
|
$reflectionClass = new \ReflectionClass( get_class($exception) ); |
184
|
|
|
|
185
|
|
|
//file |
186
|
|
|
$reflectionProperty = $reflectionClass->getProperty('file'); |
187
|
|
|
$reflectionProperty->setAccessible(true); |
188
|
|
|
$reflectionProperty->setValue($exception, $caller['file']); |
189
|
|
|
|
190
|
|
|
// line |
191
|
|
|
$reflectionProperty = $reflectionClass->getProperty('line'); |
192
|
|
|
$reflectionProperty->setAccessible(true); |
193
|
|
|
$reflectionProperty->setValue($exception, $caller['line']); |
194
|
|
|
|
195
|
|
|
throw $exception; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/**/ |
199
|
|
|
} |
200
|
|
|
|
This check looks for private methods that have been defined, but are not used inside the class.