1 | <?php |
||
11 | class ArrayExtension extends AbstractExtension |
||
12 | { |
||
13 | /** |
||
14 | * Return extension name |
||
15 | * |
||
16 | * @return string |
||
17 | */ |
||
18 | public function getName() |
||
19 | { |
||
20 | return 'jasny/array'; |
||
21 | } |
||
22 | |||
23 | /** |
||
24 | * Callback for Twig |
||
25 | * @ignore |
||
26 | */ |
||
27 | 11 | public function getFilters() |
|
37 | |||
38 | |||
39 | /** |
||
40 | * Calculate the sum of values in an array |
||
41 | * |
||
42 | * @param array $array |
||
43 | * @return int |
||
44 | */ |
||
45 | 2 | public function sum($array) |
|
46 | { |
||
47 | 2 | return isset($array) ? array_sum((array)$array) : null; |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * Calculate the product of values in an array |
||
52 | * |
||
53 | * @param array $array |
||
54 | * @return int |
||
55 | */ |
||
56 | 2 | public function product($array) |
|
57 | { |
||
58 | 2 | return isset($array) ? array_product((array)$array) : null; |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * Return all the values of an array or object |
||
63 | * |
||
64 | * @param array|object $array |
||
65 | * @return array |
||
66 | */ |
||
67 | 2 | public function values($array) |
|
68 | { |
||
69 | 2 | return isset($array) ? array_values((array)$array) : null; |
|
70 | } |
||
71 | |||
72 | /** |
||
73 | * Cast value to an array |
||
74 | * |
||
75 | * @param object|mixed $value |
||
76 | * @return array |
||
77 | */ |
||
78 | 3 | public function asArray($value) |
|
79 | { |
||
80 | 3 | return is_object($value) ? get_object_vars($value) : (array)$value; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * Cast an array to an HTML attribute string |
||
85 | * |
||
86 | * @param mixed $array |
||
87 | * @return string |
||
88 | */ |
||
89 | 2 | public function htmlAttributes($array) |
|
110 | } |
||
111 |