Lines 100.00% 29 / 29
Methods 100.00% 11 / 11
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 create 100.00% 1 / 1 100.00% 1 / 1 1
 isAvailable 100.00% 1 / 1 100.00% 1 / 1 2
 get 100.00% 3 / 3 100.00% 1 / 1 2
 offsetGet 100.00% 1 / 1 100.00% 1 / 1 1
 offsetExists 100.00% 2 / 2 100.00% 1 / 1 1
 offsetSet 100.00% 3 / 3 100.00% 1 / 1 1
 offsetUnset 100.00% 1 / 1 100.00% 1 / 1 1
 __get 100.00% 6 / 6 100.00% 1 / 1 7
 __toString 100.00% 4 / 4 100.00% 1 / 1 2
 __keyExists 100.00% 6 / 6 100.00% 1 / 1 5
10class Property implements \ArrayAccess
11{
12    /**
13     *
14     * @var Mixed $access
15     *
16     */
17    protected $access = null;
18
19    /**
20     *
21     * @param Mixed $access
22     */
23    public function __construct($access)
24    {
25        $this->access = $access;
26    }
27
28    public static function create(mixed $access): self
29    {
30        return new self($access);
31    }
32
33    public function isAvailable(): bool
34    {
35        //shorter to avoid extra unit testing
36        return (null !== $this->access) ? true : false;
37    }
38
39    public function get(mixed $default = null): mixed
40    {
41        if (null !== $this->access) {
42            return $this->access;
43        }
44        return $default;
45    }
46
47    #[\Override]
48    public function offsetGet(mixed $property): mixed
49    {
50        return $this->__get($property);
51    }
52
53    #[\Override]
54    public function offsetExists(mixed $property): bool
55    {
56        return null !== $this->__get($property)
57            ->get();
58    }
59
60    #[\Override]
61    public function offsetSet(mixed $offset, mixed $value): void
62    {
63        throw new \BO\Zmsentities\Exception\PropertyOffsetReadOnly(
64            __CLASS__ . "[$offset] is readonly, could not set " . htmlspecialchars($value)
65        );
66    }
67
68    #[\Override]
69    public function offsetUnset(mixed $offset): void
70    {
71        throw new \BO\Zmsentities\Exception\PropertyOffsetReadOnly(__CLASS__ . "[$offset] is readonly");
72    }
73
74    public function __get(mixed $property)
75    {
76        if (
77            (is_array($this->access) && array_key_exists($property, $this->access)) ||
78            ($this->access instanceof \ArrayAccess && $this->access->offsetExists($property))
79        ) {
80            return new self($this->access[$property]);
81        }
82        if (is_object($this->access) && isset($this->access->$property)) {
83            return new self($this->access->$property);
84        }
85        return new self(null);
86    }
87
88    public function __toString()
89    {
90        $string = $this->get('');
91        if (! is_string($string)) {
92            $string = print_r($string, true);
93        }
94        return $string;
95    }
96
97    public static function __keyExists(mixed $key, mixed $data)
98    {
99        if (is_array($data)) {
100            return array_key_exists($key, $data);
101        }
102
103        if ($data instanceof \ArrayObject && $data->offsetExists($key)) {
104            return true;
105        }
106
107        if (is_object($data)) {
108            return property_exists($data, $key);
109        }
110    }
111}