|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Admingenerator\GeneratorBundle\Twig\Extension; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @author Piotr Gołębiewski <[email protected]> |
|
7
|
|
|
* @author Stéphane Escandell |
|
8
|
|
|
*/ |
|
9
|
|
|
class ArrayExtension extends \Twig_Extension |
|
|
|
|
|
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* {@inheritdoc} |
|
13
|
|
|
*/ |
|
14
|
|
|
public function getFilters() |
|
15
|
|
|
{ |
|
16
|
|
|
return array( |
|
17
|
|
|
'mapBy' => new \Twig_SimpleFilter('mapBy', array($this, 'mapBy')), |
|
|
|
|
|
|
18
|
|
|
'flatten' => new \Twig_SimpleFilter('flatten', array($this, 'flatten')), |
|
|
|
|
|
|
19
|
|
|
'intersect' => new \Twig_SimpleFilter('intersect', array($this, 'intersect')), |
|
|
|
|
|
|
20
|
|
|
'clean' => new \Twig_SimpleFilter('clean', array($this, 'clean')), |
|
|
|
|
|
|
21
|
|
|
'unique' => new \Twig_SimpleFilter('unique', array($this, 'unique')), |
|
|
|
|
|
|
22
|
|
|
); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Map collection by key. For objects, return the property, use |
|
27
|
|
|
* get method or is method, if avaliable. |
|
28
|
|
|
* |
|
29
|
|
|
* @param array $input Array of arrays or objects. |
|
30
|
|
|
* @param string $key Key to map by. |
|
31
|
|
|
* @throws \InvalidArgumentException If array item is not an array or object. |
|
32
|
|
|
* @throws \LogicException If array item could not be mapped by given key. |
|
33
|
|
|
* @return array Mapped array. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function mapBy(array $input, $key) |
|
36
|
|
|
{ |
|
37
|
|
|
return array_map(function ($item) use ($key) { |
|
38
|
|
|
if (is_array($item)) { |
|
39
|
|
|
if (!array_key_exists($key, $item)) { |
|
40
|
|
|
throw new \LogicException("Could not map item by key \"$key\". Array key does not exist."); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $item[$key]; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// TODO: use PropertyAccessor ?? |
|
47
|
|
|
if (!is_object($item)) { |
|
48
|
|
|
throw new \InvalidArgumentException("Item must be an array or object."); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$ref = new \ReflectionClass($item); |
|
52
|
|
|
|
|
53
|
|
|
if ($ref->hasProperty($key) && $ref->getProperty($key)->isPublic()) { |
|
54
|
|
|
return $item->$key; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if ($ref->hasMethod($key) && !$ref->getMethod($key)->isPrivate()) { |
|
58
|
|
|
return $item->$key(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$get = 'get'.ucfirst($key); |
|
62
|
|
|
if ($ref->hasMethod($get) && !$ref->getMethod($get)->isPrivate()) { |
|
63
|
|
|
return $item->$get(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$is = 'is'.ucfirst($key); |
|
67
|
|
|
if ($ref->hasMethod($is) && !$ref->getMethod($is)->isPrivate()) { |
|
68
|
|
|
return $item->$is(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
throw new \LogicException("Could not map item by key \"$key\". Cannot access the property directly or through getter/is method."); |
|
72
|
|
|
|
|
73
|
|
|
}, $input); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Flatten nested arrays. |
|
78
|
|
|
* |
|
79
|
|
|
* @param array $input Array of arrays. |
|
80
|
|
|
* @return array Flat array. |
|
81
|
|
|
*/ |
|
82
|
|
|
public function flatten(array $input) |
|
83
|
|
|
{ |
|
84
|
|
|
$it = new \RecursiveIteratorIterator( |
|
85
|
|
|
new \RecursiveArrayIterator($input) |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
return iterator_to_array($it, false); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Computes the intersection of arrays |
|
93
|
|
|
* |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
public function intersect() |
|
97
|
|
|
{ |
|
98
|
|
|
return call_user_func_array('array_intersect', func_get_args()); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Remove entries with value $car |
|
103
|
|
|
* |
|
104
|
|
|
* @param array $input |
|
105
|
|
|
* @param string $car |
|
106
|
|
|
* @return array |
|
107
|
|
|
*/ |
|
108
|
|
|
public function clean(array $input, $car = '') |
|
109
|
|
|
{ |
|
110
|
|
|
return array_filter($input, function($v) use ($car) { |
|
111
|
|
|
return $car != $v; |
|
112
|
|
|
}); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Remove duplicate entries |
|
117
|
|
|
* |
|
118
|
|
|
* @param array $input |
|
119
|
|
|
* @return array |
|
120
|
|
|
*/ |
|
121
|
|
|
public function unique(array $input) |
|
122
|
|
|
{ |
|
123
|
|
|
return array_unique($input); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Returns the name of the extension. |
|
128
|
|
|
* |
|
129
|
|
|
* @return string The extension name |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getName() |
|
132
|
|
|
{ |
|
133
|
|
|
return 'admingenerator_array'; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.