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     * @psalm-api
40     */
41    public function getPath()
42    {
43        if (!$this->offsetExists('path')) {
44            return false;
45        }
46        return $this['path'];
47    }
48
49    public static function hasValidOffset($item, string $index): bool
50    {
51        return (
52            (is_object($item) && $item->offsetExists($index)) ||
53            (is_array($item) && array_key_exists($index, $item))
54        );
55    }
56
57    /** @psalm-api */
58    public function getLocale()
59    {
60        $meta = $this['meta'];
61        if (false === static::hasValidOffset($meta, 'locale')) {
62            return false;
63        }
64        return $this['meta']['locale'];
65    }
66
67    /** @psalm-api */
68    public function getLink()
69    {
70        if (!$this->offsetExists('link')) {
71            return false;
72        }
73        return $this['link'];
74    }
75
76    public function getType()
77    {
78        if (!$this->offsetExists('type')) {
79            return false;
80        }
81        return $this['type'];
82    }
83
84    protected static function subcount($countable): int|null
85    {
86        if (is_array($countable) || $countable instanceof \Countable) {
87            return count($countable);
88        }
89        return null;
90    }
91
92    public function __set($name, $value)
93    {
94        $this->offsetSet($name, $value);
95    }
96
97    #[\Override]
98    public function offsetSet($index, $value): void
99    {
100        if ('data_json' == $index) {
101            $value = json_decode($value, true);
102            $this->exchangeArray($value);
103        } else {
104            if (stripos($index, '_json')) {
105                $value = json_decode($value, true);
106                $index = str_replace('_json', '', $index);
107            }
108            if (stripos($index, '__')) {
109                static::doubleUnterlineToArray($this, $index, $value);
110                return;
111            }
112
113            parent::offsetSet($index, $value);
114        }
115    }
116
117    /**
118     * @param static $array
119     */
120    public static function doubleUnterlineToArray(&$array, string $key, $value)
121    {
122        if (is_null($key)) {
123            return $array = $value;
124        }
125        $keys = explode('__', $key);
126
127        $numKeys = count($keys);
128        while ($numKeys > 1) {
129            $key = array_shift($keys);
130            $numKeys = count($keys);
131            if (! isset($array[$key]) || ! is_array($array[$key])) {
132                $array[$key] = [];
133            }
134
135            $array = &$array[$key];
136        }
137
138        $array[array_shift($keys)] = $value;
139
140        return $array;
141    }
142}