1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace jamesdb\Cart; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use jamesdb\Cart\Exception\CartPropertyNotIntegerException; |
7
|
|
|
use Money\Currency; |
8
|
|
|
|
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() |
43
|
|
|
{ |
44
|
45 |
|
$rowId = $this->item; |
45
|
|
|
|
46
|
45 |
|
$ignoredProperties = ['quantity']; |
47
|
|
|
|
48
|
45 |
|
foreach ($ignoredProperties as $property) { |
49
|
45 |
|
if (array_key_exists($property, $rowId)) { |
50
|
45 |
|
unset($rowId[$property]); |
51
|
45 |
|
} |
52
|
45 |
|
} |
53
|
|
|
|
54
|
45 |
|
return md5(serialize($rowId)); |
55
|
|
|
} |
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) |
88
|
|
|
{ |
89
|
39 |
|
if ($key === 'rowid') { |
90
|
3 |
|
return $this->getRowId(); |
91
|
|
|
} |
92
|
|
|
|
93
|
36 |
|
return $this->item[$key]; |
94
|
|
|
} |
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) |
104
|
|
|
{ |
105
|
3 |
|
return (new Money($this->price, $currency))->getMoney()->multiply($this->quantity); |
|
|
|
|
106
|
|
|
} |
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) |
116
|
|
|
{ |
117
|
6 |
|
return (new Money($this->price + $this->tax, $currency))->getMoney()->multiply($this->quantity); |
|
|
|
|
118
|
|
|
} |
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) |
128
|
|
|
{ |
129
|
3 |
|
return (new Money($this->tax, $currency))->getMoney()->multiply($this->quantity); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Export the item as array. |
134
|
|
|
* |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
42 |
|
public function toArray() |
138
|
|
|
{ |
139
|
|
|
return [ |
140
|
42 |
|
'id' => $this->getRowId(), |
141
|
42 |
|
'data' => $this->item |
142
|
42 |
|
]; |
143
|
|
|
} |
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) |
154
|
|
|
{ |
155
|
21 |
|
$this->set($key, $value); |
156
|
18 |
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get a property by key. |
160
|
|
|
* |
161
|
|
|
* @param string $key |
162
|
|
|
* |
163
|
|
|
* @return mixed |
164
|
|
|
*/ |
165
|
36 |
|
public function __get($key) |
166
|
|
|
{ |
167
|
36 |
|
return $this->get($key); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Array Access get. |
172
|
|
|
* |
173
|
|
|
* @param string $offset |
174
|
|
|
* |
175
|
|
|
* @return boolean |
176
|
|
|
*/ |
177
|
6 |
|
public function offsetExists($offset) |
178
|
|
|
{ |
179
|
6 |
|
return isset($this->item[$offset]); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Array Access get. |
184
|
|
|
* |
185
|
|
|
* @param string $offset |
186
|
|
|
* |
187
|
|
|
* @return mixed |
188
|
|
|
*/ |
189
|
6 |
|
public function offsetGet($offset) |
190
|
|
|
{ |
191
|
6 |
|
return $this->get($offset); |
192
|
|
|
} |
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) |
203
|
|
|
{ |
204
|
3 |
|
$this->set($key, $value); |
205
|
3 |
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Array Access unset. |
209
|
|
|
* |
210
|
|
|
* @param string $offset |
211
|
|
|
* |
212
|
|
|
* @return void |
213
|
|
|
*/ |
214
|
3 |
|
public function offsetUnset($offset) |
215
|
|
|
{ |
216
|
3 |
|
unset($this->item[$offset]); |
217
|
3 |
|
} |
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.