1 | <?php |
||
9 | class CartItem implements ArrayAccess |
||
10 | { |
||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | protected $item = []; |
||
15 | |||
16 | /** |
||
17 | * Constructor. |
||
18 | * |
||
19 | * @param array $data |
||
20 | */ |
||
21 | 51 | public function __construct(array $data = []) |
|
22 | { |
||
23 | $defaults = [ |
||
24 | 51 | 'options' => [], |
|
25 | 51 | 'price' => 0, |
|
26 | 51 | 'quantity' => 1, |
|
27 | 'tax' => 0 |
||
28 | 51 | ]; |
|
29 | |||
30 | 51 | $data = array_merge($defaults, $data); |
|
31 | |||
32 | 51 | foreach ($data as $key => $value) { |
|
33 | 51 | $this->set($key, $value); |
|
34 | 51 | } |
|
35 | 51 | } |
|
36 | |||
37 | /** |
||
38 | * Return the rowid of item. |
||
39 | * |
||
40 | * @return string |
||
41 | */ |
||
42 | 45 | public function getRowId() |
|
56 | |||
57 | /** |
||
58 | * Set property key and value. |
||
59 | * |
||
60 | * @param string $key |
||
61 | * @param mixed $value |
||
62 | * |
||
63 | * @throws \jamesdb\Cart\Exception\CartPropertyNotIntegerException |
||
64 | * |
||
65 | * @return void |
||
66 | */ |
||
67 | 51 | public function set($key, $value) |
|
68 | { |
||
69 | 51 | $numeric = ['price', 'tax', 'quantity']; |
|
70 | |||
71 | 51 | if ((in_array($key, $numeric)) && (! is_int($value))) { |
|
72 | 3 | throw new CartPropertyNotIntegerException( |
|
73 | 3 | sprintf('The [%s] property must be an integer', $key) |
|
74 | 3 | ); |
|
75 | } |
||
76 | |||
77 | 51 | $this->item[$key] = $value; |
|
78 | 51 | } |
|
79 | |||
80 | /** |
||
81 | * Get a property by key. |
||
82 | * |
||
83 | * @param string $key |
||
84 | * |
||
85 | * @return mixed |
||
86 | */ |
||
87 | 39 | public function get($key) |
|
95 | |||
96 | /** |
||
97 | * Return item price excluding tax. |
||
98 | * |
||
99 | * @param \Money\Currency $currency |
||
100 | * |
||
101 | * @return \Money\Money |
||
102 | */ |
||
103 | 3 | public function getPriceExcludingTax(Currency $currency) |
|
107 | |||
108 | /** |
||
109 | * Return item price including tax. |
||
110 | * |
||
111 | * @param \Money\Currency $currency |
||
112 | * |
||
113 | * @return \Money\Money |
||
114 | */ |
||
115 | 6 | public function getPrice(Currency $currency) |
|
119 | |||
120 | /** |
||
121 | * Return the item tax. |
||
122 | * |
||
123 | * @param \Money\Currency $currency |
||
124 | * |
||
125 | * @return \Money\Money |
||
126 | */ |
||
127 | 3 | public function getTax(Currency $currency) |
|
131 | |||
132 | /** |
||
133 | * Export the item as array. |
||
134 | * |
||
135 | * @return array |
||
136 | */ |
||
137 | 42 | public function toArray() |
|
144 | |||
145 | /** |
||
146 | * Set property key and value. |
||
147 | * |
||
148 | * @param string $key |
||
149 | * @param mixed $value |
||
150 | * |
||
151 | * @return void |
||
152 | */ |
||
153 | 21 | public function __set($key, $value) |
|
157 | |||
158 | /** |
||
159 | * Get a property by key. |
||
160 | * |
||
161 | * @param string $key |
||
162 | * |
||
163 | * @return mixed |
||
164 | */ |
||
165 | 36 | public function __get($key) |
|
169 | |||
170 | /** |
||
171 | * Array Access get. |
||
172 | * |
||
173 | * @param string $offset |
||
174 | * |
||
175 | * @return boolean |
||
176 | */ |
||
177 | 6 | public function offsetExists($offset) |
|
181 | |||
182 | /** |
||
183 | * Array Access get. |
||
184 | * |
||
185 | * @param string $offset |
||
186 | * |
||
187 | * @return mixed |
||
188 | */ |
||
189 | 6 | public function offsetGet($offset) |
|
193 | |||
194 | /** |
||
195 | * Array Access set. |
||
196 | * |
||
197 | * @param string $key |
||
198 | * @param mixed $value |
||
199 | * |
||
200 | * @return void |
||
201 | */ |
||
202 | 3 | public function offsetSet($key, $value) |
|
206 | |||
207 | /** |
||
208 | * Array Access unset. |
||
209 | * |
||
210 | * @param string $offset |
||
211 | * |
||
212 | * @return void |
||
213 | */ |
||
214 | 3 | public function offsetUnset($offset) |
|
218 | } |
||
219 |
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.