1 | <?php |
||
2 | |||
3 | namespace Dtc\GridBundle\Mapper; |
||
4 | |||
5 | use ArrayObject; |
||
6 | use Exception; |
||
7 | |||
8 | class ArrayValueObject extends ArrayObject |
||
9 | { |
||
10 | public function getValueByArray(array $params) |
||
11 | { |
||
12 | $data = &$this; |
||
13 | $total = count($params); |
||
14 | $key = current($params); |
||
15 | |||
16 | if (0 === $total) { |
||
17 | throw new Exception('requires at least 1 arg'); |
||
18 | } |
||
19 | |||
20 | if (null === $key) { |
||
21 | throw new Exception('requires non NULL args'); |
||
22 | } |
||
23 | if (!is_scalar($key)) { |
||
24 | throw new Exception('requires scalar args'); |
||
25 | } |
||
26 | if (!isset($data[$key])) { |
||
27 | return null; |
||
28 | } |
||
29 | |||
30 | if (1 === $total) { |
||
31 | return $data[$key]; |
||
32 | } |
||
33 | |||
34 | $data = &$data[$key]; |
||
35 | $args = array_slice($params, 1); |
||
36 | |||
37 | $count = 0; |
||
38 | foreach ($args as $key) { |
||
39 | if ($count++ > 100) { |
||
40 | exit(); |
||
0 ignored issues
–
show
|
|||
41 | } |
||
42 | |||
43 | if (is_array($data)) { |
||
44 | if (!isset($data[$key])) { |
||
45 | return null; |
||
46 | } else { |
||
47 | $data = &$data[$key]; |
||
48 | } |
||
49 | } else { |
||
50 | return null; |
||
51 | } |
||
52 | } |
||
53 | |||
54 | return $data; |
||
55 | } |
||
56 | |||
57 | public function getValue() |
||
58 | { |
||
59 | return $this->getValueByArray(func_get_args()); |
||
60 | } |
||
61 | } |
||
62 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.