Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
29 / 29 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
1 / 1 |
Property | |
100.00% |
29 / 29 |
|
100.00% |
11 / 11 |
24 | |
100.00% |
1 / 1 |
__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 |
1 | <?php |
2 | |
3 | namespace BO\Zmsentities\Helper; |
4 | |
5 | /** |
6 | * Get a property from an Array or ArrayAccess |
7 | */ |
8 | class 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 | public function offsetGet($property) |
46 | { |
47 | return $this->__get($property); |
48 | } |
49 | |
50 | public function offsetExists($property) |
51 | { |
52 | return null !== $this->__get($property) |
53 | ->get(); |
54 | } |
55 | |
56 | public function offsetSet($offset, $value) |
57 | { |
58 | throw new \BO\Zmsentities\Exception\PropertyOffsetReadOnly( |
59 | __CLASS__ . "[$offset] is readonly, could not set " . htmlspecialchars($value) |
60 | ); |
61 | } |
62 | |
63 | public function offsetUnset($offset) |
64 | { |
65 | throw new \BO\Zmsentities\Exception\PropertyOffsetReadOnly(__CLASS__ . "[$offset] is readonly"); |
66 | } |
67 | |
68 | public function __get($property) |
69 | { |
70 | if ( |
71 | (is_array($this->access) && array_key_exists($property, $this->access)) || |
72 | ($this->access instanceof \ArrayAccess && $this->access->offsetExists($property)) |
73 | ) { |
74 | return new self($this->access[$property]); |
75 | } |
76 | if (is_object($this->access) && isset($this->access->$property)) { |
77 | return new self($this->access->$property); |
78 | } |
79 | return new self(null); |
80 | } |
81 | |
82 | public function __toString() |
83 | { |
84 | $string = $this->get(''); |
85 | if (! is_string($string)) { |
86 | $string = print_r($string, true); |
87 | } |
88 | return $string; |
89 | } |
90 | |
91 | public static function __keyExists($key, $data) |
92 | { |
93 | if (is_array($data)) { |
94 | return array_key_exists($key, $data); |
95 | } |
96 | |
97 | if ($data instanceof \ArrayObject && $data->offsetExists($key)) { |
98 | return true; |
99 | } |
100 | |
101 | if (is_object($data)) { |
102 | return property_exists($data, $key); |
103 | } |
104 | } |
105 | } |