-
Notifications
You must be signed in to change notification settings - Fork 460
/
Copy pathsupport.php
59 lines (46 loc) · 1.38 KB
/
support.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
if ( !function_exists('hmap') ) {
/**
* Simple function which aids in converting the tree hierarchy into something
* more easily testable...
*
* @param array $nodes
* @return array
*/
function hmap(array $nodes, $preserve = null) {
$output = array();
foreach($nodes as $node) {
if ( is_null($preserve) ) {
$output[$node['name']] = empty($node['children']) ? null : hmap($node['children']);
} else {
$preserve = is_string($preserve) ? array($preserve) : $preserve;
$current = \Illuminate\Support\Arr::only($node, $preserve);
if ( array_key_exists('children', $node) ) {
$children = $node['children'];
if ( count($children) > 0 )
$current['children'] = hmap($children, $preserve);
}
$output[] = $current;
}
}
return $output;
}
}
if ( !function_exists('array_ints_keys') ) {
/**
* Cast provided keys's values into ints. This is to wrestle with PDO driver
* inconsistencies.
*
* @param array $input
* @param mixed $keys
* @return array
*/
function array_ints_keys(array $input, $keys='id') {
$keys = is_string($keys) ? array($keys) : $keys;
array_walk_recursive($input, function(&$value, $key) use ($keys) {
if ( array_search($key, $keys) !== false )
$value = (int) $value;
});
return $input;
}
}