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