Total Complexity | 5 |
Total Lines | 49 |
Duplicated Lines | 0 % |
Coverage | 81.25% |
Changes | 3 | ||
Bugs | 1 | Features | 0 |
1 | <?php |
||
25 | trait JsonHelperTrait |
||
26 | { |
||
27 | /** |
||
28 | * @param mixed[] $array |
||
29 | * @return mixed |
||
30 | */ |
||
31 | 8 | public function arrayToObject(array $array) |
|
32 | { |
||
33 | try { |
||
34 | 8 | return json_decode( |
|
35 | 8 | json_encode($array, JSON_THROW_ON_ERROR), |
|
36 | 8 | false, |
|
37 | 8 | 512, |
|
38 | 8 | JSON_THROW_ON_ERROR |
|
39 | 8 | ); |
|
40 | } catch (Exception $e) { |
||
41 | throw new ParseErrorException('', 0, $e->getPrevious()); |
||
42 | } |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @param mixed[] $array |
||
47 | * @return stdClass |
||
48 | */ |
||
49 | 8 | public function assocArrToObject(array $array): stdClass |
|
50 | { |
||
51 | 8 | if ($this->isAssoc($array) === false) { |
|
52 | throw new RuntimeException('Array is not associative'); |
||
53 | } |
||
54 | |||
55 | 8 | return $this->arrayToObject($array); |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * method from Kohana |
||
60 | * Tests if an array is associative or not. |
||
61 | * |
||
62 | * @param mixed[] $array |
||
63 | * @return bool |
||
64 | */ |
||
65 | 8 | public function isAssoc(array &$array): bool |
|
74 | } |
||
75 | } |
||
76 |