Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
17.02% covered (danger)
17.02%
8 / 47
9.09% covered (danger)
9.09%
1 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
Base
17.02% covered (danger)
17.02%
8 / 47
9.09% covered (danger)
9.09%
1 / 11
475.94
0.00% covered (danger)
0.00%
0 / 1
 getId
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPath
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 hasValidOffset
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 getLocale
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getLink
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getType
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 subcount
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
 __set
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 offsetSet
40.00% covered (danger)
40.00%
4 / 10
0.00% covered (danger)
0.00%
0 / 1
7.46
 doubleUnterlineToArray
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3/**
4 * @package Zmsdldb
5 * @copyright BerlinOnline Stadtportal GmbH & Co. KG
6 **/
7
8namespace BO\Zmsdldb\Entity;
9
10/**
11 * @extends \ArrayObject<string, mixed>
12 */
13class Base extends \ArrayObject
14{
15    /**
16     * return an ID for this entity
17     *
18     */
19    public function getId()
20    {
21        if (!$this->offsetExists('id')) {
22            return false;
23        }
24        return $this['id'];
25    }
26
27    /**
28     * return a name for this entity
29     *
30     */
31    public function getName()
32    {
33        return $this['name'];
34    }
35
36    /**
37     * return a path for this entity
38     *
39     */
40    public function getPath()
41    {
42        if (!$this->offsetExists('path')) {
43            return false;
44        }
45        return $this['path'];
46    }
47
48    public static function hasValidOffset($item, $index)
49    {
50        return (
51            (is_object($item) && $item->offsetExists($index)) ||
52            (is_array($item) && array_key_exists($index, $item))
53        );
54    }
55
56    public function getLocale()
57    {
58        $meta = $this['meta'];
59        if (false === static::hasValidOffset($meta, 'locale')) {
60            return false;
61        }
62        return $this['meta']['locale'];
63    }
64
65    public function getLink()
66    {
67        if (!$this->offsetExists('link')) {
68            return false;
69        }
70        return $this['link'];
71    }
72
73    public function getType()
74    {
75        if (!$this->offsetExists('type')) {
76            return false;
77        }
78        return $this['type'];
79    }
80
81    protected static function subcount($countable)
82    {
83        if (is_array($countable) || $countable instanceof \Countable) {
84            return count($countable);
85        }
86        return null;
87    }
88
89    public function __set($name, $value)
90    {
91        $this->offsetSet($name, $value);
92    }
93
94    #[\Override]
95    public function offsetSet($index, $value): void
96    {
97        if ('data_json' == $index) {
98            $value = json_decode($value, true);
99            $this->exchangeArray($value);
100        } else {
101            if (stripos($index, '_json')) {
102                $value = json_decode($value, true);
103                $index = str_replace('_json', '', $index);
104            }
105            if (stripos($index, '__')) {
106                static::doubleUnterlineToArray($this, $index, $value);
107                return;
108            }
109
110            parent::offsetSet($index, $value);
111        }
112    }
113
114    public static function doubleUnterlineToArray(&$array, $key, $value)
115    {
116        if (is_null($key)) {
117            return $array = $value;
118        }
119        $keys = explode('__', $key);
120
121        $numKeys = count($keys);
122        while ($numKeys > 1) {
123            $key = array_shift($keys);
124            $numKeys = count($keys);
125            if (! isset($array[$key]) || ! is_array($array[$key])) {
126                $array[$key] = [];
127            }
128
129            $array = &$array[$key];
130        }
131
132        $array[array_shift($keys)] = $value;
133
134        return $array;
135    }
136}