1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Nip_Helper_Arrays extends Nip\Helpers\AbstractHelper |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Determine whether the given value is array accessible. |
8
|
|
|
* |
9
|
|
|
* @param mixed $value |
10
|
|
|
* @return bool |
11
|
|
|
*/ |
12
|
|
|
public static function accessible($value) |
13
|
|
|
{ |
14
|
|
|
return is_array($value) || $value instanceof ArrayAccess; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Determine if the given key exists in the provided array. |
19
|
|
|
* |
20
|
|
|
* @param \ArrayAccess|array $array |
21
|
|
|
* @param string|int $key |
22
|
|
|
* @return bool |
23
|
|
|
*/ |
24
|
|
|
public static function exists($array, $key) |
25
|
|
|
{ |
26
|
|
|
if ($array instanceof ArrayAccess) { |
27
|
|
|
return $array->offsetExists($key); |
28
|
|
|
} |
29
|
|
|
return array_key_exists($key, $array); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Return the first element in an array passing a given truth test. |
34
|
|
|
* |
35
|
|
|
* @param array $array |
36
|
|
|
* @param callable|null $callback |
37
|
|
|
* @param mixed $default |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
|
|
public static function first($array, callable $callback = null, $default = null) |
41
|
|
|
{ |
42
|
|
|
if (is_null($callback)) { |
43
|
|
|
if (empty($array)) { |
44
|
|
|
return value($default); |
45
|
|
|
} |
46
|
|
|
foreach ($array as $item) { |
47
|
|
|
return $item; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
foreach ($array as $key => $value) { |
51
|
|
|
if (call_user_func($callback, $value, $key)) { |
52
|
|
|
return $value; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
return value($default); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function toXLS($array, $filename, $labels = array()) |
59
|
|
|
{ |
60
|
|
|
$xls = new Spreadsheet_Excel_Writer(); |
61
|
|
|
$xls->setVersion(8); |
62
|
|
|
|
63
|
|
|
$sheet = $xls->addWorksheet(); |
64
|
|
|
$sheet->setInputEncoding("UTF-8"); |
65
|
|
|
|
66
|
|
|
$heading = $xls->addFormat(['bold' => '1', 'align' => 'center']); |
67
|
|
|
|
68
|
|
|
if ($array && !$labels) { |
|
|
|
|
69
|
|
|
$labels = array_keys(reset($array)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$i = 0; |
73
|
|
|
foreach ($labels as $label) { |
74
|
|
|
$sheet->write(0, $i, $label, $heading); |
75
|
|
|
$i++; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (count($array)) { |
79
|
|
|
$line = 1; |
80
|
|
|
foreach ($array as $item) { |
81
|
|
|
$column = 0; |
82
|
|
|
foreach ($labels as $label) { |
83
|
|
|
$sheet->write($line, $column, html_entity_decode($item[$label], ENT_QUOTES, 'UTF-8')); |
84
|
|
|
$column++; |
85
|
|
|
} |
86
|
|
|
$line++; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
header("Cache-Control: private, max-age=1, pre-check=1", true); |
91
|
|
|
header("Pragma: none", true); |
92
|
|
|
|
93
|
|
|
$xls->send($filename); |
94
|
|
|
$xls->close(); |
95
|
|
|
exit(); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get an item from an array using "dot" notation. |
100
|
|
|
* |
101
|
|
|
* @param \ArrayAccess|array $array |
102
|
|
|
* @param string $key |
103
|
|
|
* @param mixed $default |
104
|
|
|
* @return mixed |
105
|
|
|
*/ |
106
|
|
|
public static function get($array, $key, $default = null) |
107
|
|
|
{ |
108
|
|
|
if (!static::accessible($array)) { |
109
|
|
|
return value($default); |
110
|
|
|
} |
111
|
|
|
if (is_null($key)) { |
112
|
|
|
return $array; |
113
|
|
|
} |
114
|
|
|
if (static::exists($array, $key)) { |
115
|
|
|
return $array[$key]; |
116
|
|
|
} |
117
|
|
|
foreach (explode('.', $key) as $segment) { |
118
|
|
|
if (static::accessible($array) && static::exists($array, $segment)) { |
119
|
|
|
$array = $array[$segment]; |
120
|
|
|
} else { |
121
|
|
|
return value($default); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
return $array; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Produces a new version of the array that does not contain any of the specified values |
129
|
|
|
* |
130
|
|
|
* @param array $array |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
View Code Duplication |
public function without($array) |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
$values = func_get_args(); |
136
|
|
|
unset($values[0]); |
137
|
|
|
|
138
|
|
|
if ($values) { |
|
|
|
|
139
|
|
|
foreach ($values as $value) { |
140
|
|
|
unset($array[array_search($value, $array)]); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $array; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Produces a new version of the array that does not contain any of the specified keys |
149
|
|
|
* |
150
|
|
|
* @param array $array |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
View Code Duplication |
public function withoutKeys($array) |
|
|
|
|
154
|
|
|
{ |
155
|
|
|
$values = func_get_args(); |
156
|
|
|
unset($values[0]); |
157
|
|
|
|
158
|
|
|
if ($values) { |
|
|
|
|
159
|
|
|
foreach ($values as $value) { |
160
|
|
|
unset($array[$value]); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $array; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Fetch the same property for all the elements. |
169
|
|
|
* |
170
|
|
|
* @param array $array |
171
|
|
|
* @param string $property |
172
|
|
|
* @return array The property values |
173
|
|
|
*/ |
174
|
|
|
public function changeKey($array, $property) |
175
|
|
|
{ |
176
|
|
|
$return = []; |
177
|
|
|
|
178
|
|
|
if (count($array) > 0) { |
179
|
|
|
foreach ($array as $item) { |
180
|
|
|
$return[$item->$property] = $item; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $return; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Fetch the same property for all the elements. |
189
|
|
|
* |
190
|
|
|
* @param array|\Nip\Records\Collections\Collection $array |
191
|
|
|
* @param string $property |
192
|
|
|
* @param bool|string $return |
193
|
|
|
* @return array The property values |
194
|
|
|
*/ |
195
|
|
|
public function pluck($array, $property, &$return = false) |
196
|
|
|
{ |
197
|
|
|
$return = []; |
198
|
|
|
|
199
|
|
|
if (count($array) > 0) { |
200
|
|
|
foreach ($array as $item) { |
201
|
|
|
if (is_array($item)) { |
202
|
|
|
$this->pluck($array, $property, $return); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$return[] = $item->$property; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $return; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Fetch the same property for all the elements. |
214
|
|
|
* |
215
|
|
|
* @param array $array |
216
|
|
|
* @param string $property |
217
|
|
|
*/ |
218
|
|
|
public function pluckFromArray($array, $property) |
219
|
|
|
{ |
220
|
|
|
if (is_array($array)) { |
221
|
|
|
foreach ($array as $item) { |
222
|
|
|
$return[] = $item[$property]; |
|
|
|
|
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $return; |
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Finds array item that matches $params |
231
|
|
|
* |
232
|
|
|
* @param ArrayAccess $array |
233
|
|
|
* @param array $params |
234
|
|
|
* @return mixed |
235
|
|
|
*/ |
236
|
|
|
public function find($array, $params) |
237
|
|
|
{ |
238
|
|
|
if (count($array)) { |
239
|
|
|
foreach ($array as $item) { |
240
|
|
|
$found = true; |
241
|
|
|
foreach ($params as $key => $value) { |
242
|
|
|
|
243
|
|
|
if ($item->$key != $value) { |
244
|
|
|
$found = false; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
if ($found) { |
248
|
|
|
return $item; |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
return null; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Finds all array items that match $params |
258
|
|
|
* |
259
|
|
|
* @param array $array |
260
|
|
|
* @param array $params |
261
|
|
|
* @param string $key |
|
|
|
|
262
|
|
|
* @return array |
263
|
|
|
*/ |
264
|
|
|
public function findAll($array, $params, $returnKey = false) |
265
|
|
|
{ |
266
|
|
|
$return = []; |
267
|
|
|
|
268
|
|
|
if (count($array)) { |
269
|
|
|
foreach ($array as $item) { |
270
|
|
|
$found = true; |
271
|
|
|
foreach ($params as $key => $value) { |
272
|
|
|
if ($item->$key != $value) { |
273
|
|
|
$found = false; |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
if ($found) { |
277
|
|
|
if ($returnKey) { |
278
|
|
|
$return[$item->$returnKey] = $item; |
279
|
|
|
} else { |
280
|
|
|
$return[] = $item; |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return $return; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Transposes a bidimensional array (matrix) |
291
|
|
|
* |
292
|
|
|
* @param array $array |
293
|
|
|
* @return array |
294
|
|
|
*/ |
295
|
|
|
public function transpose($array) |
296
|
|
|
{ |
297
|
|
|
$return = []; |
298
|
|
|
|
299
|
|
|
if (count($array)) { |
300
|
|
|
foreach ($array as $key => $values) { |
301
|
|
|
foreach ($values as $subkey => $value) { |
302
|
|
|
$return[$subkey][$key] = $value; |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
return $return; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Pass in a multi dimensional array and this recrusively loops through and builds up an XML document. |
312
|
|
|
* |
313
|
|
|
* @param array $data |
314
|
|
|
* @param string $rootNodeName - what you want the root node to be - defaults to data |
315
|
|
|
* @param SimpleXMLElement $xml - should only be used recursively |
316
|
|
|
* @return string XML |
317
|
|
|
*/ |
318
|
|
|
public function toXML($data, $rootNodeName = 'ResultSet', &$xml = null) |
319
|
|
|
{ |
320
|
|
|
// turn off compatibility mode as simple xml throws a wobbly if you don't. |
321
|
|
|
if (ini_get('zend.ze1_compatibility_mode') == 1) |
322
|
|
|
ini_set('zend.ze1_compatibility_mode', 0); |
323
|
|
|
|
324
|
|
|
if (is_null($xml)) { |
325
|
|
|
$xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />"); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
// loop through the data passed in. |
329
|
|
|
foreach ($data as $key => $value) { |
330
|
|
|
// no numeric keys in our xml please! |
331
|
|
|
if (is_numeric($key)) { |
332
|
|
|
$numeric = 1; |
333
|
|
|
$key = $rootNodeName; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
// delete any char not allowed in XML element names |
337
|
|
|
$key = preg_replace('/[^a-z0-9\-\_\.\:]/i', '', $key); |
338
|
|
|
|
339
|
|
|
// if there is another array found recrusively call this function |
340
|
|
|
if (is_array($value)) { |
341
|
|
|
$node = $this->isAssoc($value) || $numeric ? $xml->addChild($key) : $xml; |
|
|
|
|
342
|
|
|
|
343
|
|
|
// recursive call |
344
|
|
|
if ($numeric) |
345
|
|
|
$key = 'anon'; |
346
|
|
|
$this->toXML($value, $key, $node); |
347
|
|
|
} else { |
348
|
|
|
// add single node. |
349
|
|
|
$value = htmlentities($value); |
350
|
|
|
// $xml->addChild($key, $value); |
|
|
|
|
351
|
|
|
$xml->addAttribute($key, $value); |
352
|
|
|
} |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
// pass back as XML |
356
|
|
|
// return $xml->asXML(); |
|
|
|
|
357
|
|
|
// if you want the XML to be formatted, use the below instead to return the XML |
358
|
|
|
$doc = new DOMDocument('1.0'); |
359
|
|
|
$doc->preserveWhiteSpace = false; |
360
|
|
|
$doc->loadXML($xml->asXML()); |
361
|
|
|
$doc->formatOutput = true; |
362
|
|
|
return $doc->saveXML(); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Determine if a variable is an associative array |
367
|
|
|
* |
368
|
|
|
* @param array $array |
369
|
|
|
* @return bool |
370
|
|
|
*/ |
371
|
|
|
public static function isAssoc($array) |
372
|
|
|
{ |
373
|
|
|
return (is_array($array) && 0 !== count(array_diff_key($array, array_keys(array_keys($array))))); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
function merge_distinct(array &$array1, array &$array2) |
|
|
|
|
377
|
|
|
{ |
378
|
|
|
$merged = $array1; |
379
|
|
|
|
380
|
|
|
foreach ($array2 as $key => &$value) { |
381
|
|
|
if (is_array($value) && isset($merged [$key]) && is_array($merged [$key])) { |
382
|
|
|
$merged [$key] = $this->merge_distinct($merged [$key], $value); |
383
|
|
|
} else { |
384
|
|
|
$merged [$key] = $value; |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
return $merged; |
389
|
|
|
} |
390
|
|
|
} |
391
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.