1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: vlitvinovs |
5
|
|
|
* Date: 2/8/17 |
6
|
|
|
* Time: 4:34 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace CodinPro\DataTransferObject; |
10
|
|
|
|
11
|
|
|
trait DTOAccessorTrait |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Check if offset exists |
15
|
|
|
* @param string $offset |
16
|
|
|
* @return bool |
17
|
|
|
*/ |
18
|
2 |
|
public function offsetExists($offset) |
19
|
|
|
{ |
20
|
2 |
|
return isset($this->data[$offset]); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Get data at scalar offset or default value instead |
25
|
|
|
* @param string $offset |
26
|
|
|
* @return mixed |
27
|
|
|
* @throws \InvalidArgumentException |
28
|
|
|
*/ |
29
|
21 |
|
private function offsetGetScalar($offset) |
30
|
|
|
{ |
31
|
21 |
|
if (isset($this->data[$offset])) { |
|
|
|
|
32
|
18 |
|
return $this->data[$offset]; |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
4 |
|
return $this->getDefaultValue($offset); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get default value at offset if set |
40
|
|
|
* @param string $offset |
41
|
|
|
* @return mixed |
42
|
|
|
* @throws \InvalidArgumentException |
43
|
|
|
*/ |
44
|
4 |
|
private function getDefaultValue($offset) |
45
|
|
|
{ |
46
|
4 |
|
if (isset($this->default[$offset])) { |
|
|
|
|
47
|
2 |
|
return $this->default[$offset]; |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
2 |
|
throw new \InvalidArgumentException('Offset '.$offset.' does not exist.'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get data at offset or default value instead |
55
|
|
|
* @param string $offset |
56
|
|
|
* @return mixed |
57
|
|
|
* @throws \InvalidArgumentException |
58
|
|
|
*/ |
59
|
9 |
|
public function offsetGet($offset) |
60
|
|
|
{ |
61
|
9 |
|
return $this->get($offset); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set data at offset |
66
|
|
|
* @param string $offset |
67
|
|
|
* @param mixed $value |
68
|
|
|
*/ |
69
|
34 |
|
public function offsetSet($offset, $value) |
70
|
|
|
{ |
71
|
34 |
|
$this->data[$offset] = $value; |
|
|
|
|
72
|
34 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Remove data at offset |
76
|
|
|
* @param string $offset |
77
|
|
|
*/ |
78
|
2 |
|
public function offsetUnset($offset) |
79
|
|
|
{ |
80
|
2 |
|
unset($this->data[$offset]); |
81
|
2 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Count data elements |
85
|
|
|
* @return int |
86
|
|
|
*/ |
87
|
2 |
|
public function count() |
88
|
|
|
{ |
89
|
2 |
|
return count($this->data); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
5 |
|
public function __get($key) |
93
|
|
|
{ |
94
|
5 |
|
return $this->offsetGet($key); |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
public function __set($key, $value) |
98
|
|
|
{ |
99
|
2 |
|
$this->offsetSet($key, $value); |
100
|
2 |
|
} |
101
|
|
|
|
102
|
2 |
|
public function __isset($key) |
103
|
|
|
{ |
104
|
2 |
|
return isset($this->data[$key]); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
} |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.