Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
11 / 11
CRAP
100.00% covered (success)
100.00%
1 / 1
Property
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
11 / 11
24
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isAvailable
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 get
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 offsetGet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 offsetExists
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 offsetSet
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 offsetUnset
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __get
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
7
 __toString
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 __keyExists
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3namespace BO\Zmsentities\Helper;
4
5/**
6 * Get a property from an Array or ArrayAccess
7 *
8 * @implements \ArrayAccess<array-key, mixed>
9 */
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($access)
29    {
30        return new self($access);
31    }
32
33    public function isAvailable()
34    {
35        //shorter to avoid extra unit testing
36        return (null !== $this->access) ? true : false;
37    }
38
39    public function get($default = null)
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($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($key, $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}