|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Recca0120\Cart; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayAccess; |
|
6
|
|
|
use ArrayIterator; |
|
7
|
|
|
use IteratorAggregate; |
|
8
|
|
|
|
|
9
|
|
|
class Cart implements ArrayAccess, IteratorAggregate |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* $items. |
|
13
|
|
|
* |
|
14
|
|
|
* @var \Illuminate\Support\Collection |
|
15
|
|
|
*/ |
|
16
|
|
|
public $items; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* |
|
20
|
|
|
* @param \Recca0120\Cart\Storage $storage |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $storage; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* __construct. |
|
26
|
|
|
* |
|
27
|
|
|
* @param \Recca0120\Cart\Storage $storage |
|
28
|
|
|
*/ |
|
29
|
1 |
|
public function __construct(Storage $storage = null) |
|
30
|
|
|
{ |
|
31
|
1 |
|
$this->storage = is_null($storage) === true ? new Storage() : $storage; |
|
32
|
1 |
|
$this->restore(); |
|
33
|
1 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* put. |
|
37
|
|
|
* |
|
38
|
|
|
* @param \Recca0120\Cart\Item $item |
|
39
|
|
|
* @param int $quantity |
|
|
|
|
|
|
40
|
|
|
* @return $this |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public function put(Item $item) |
|
43
|
|
|
{ |
|
44
|
1 |
|
$this->items->put($item->getId(), $item); |
|
45
|
|
|
|
|
46
|
1 |
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* get. |
|
51
|
|
|
* |
|
52
|
|
|
* @param \Recca0120\Cart\Item $item |
|
|
|
|
|
|
53
|
|
|
* @param int $quantity |
|
|
|
|
|
|
54
|
|
|
* @return $this |
|
55
|
|
|
*/ |
|
56
|
|
|
public function get($itemId) |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->items->get($itemId); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* remove. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $id |
|
|
|
|
|
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
1 |
|
public function remove($item) |
|
68
|
|
|
{ |
|
69
|
1 |
|
return $this->items->forget($item instanceof Item ? $item->getId() : $item); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* clear. |
|
74
|
|
|
* |
|
75
|
|
|
* @return $this |
|
76
|
|
|
*/ |
|
77
|
|
|
public function clear() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->items = $this->items->filter(function () { |
|
80
|
|
|
return false; |
|
81
|
|
|
}); |
|
82
|
|
|
|
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* items. |
|
88
|
|
|
* |
|
89
|
|
|
* @return \Illuminate\Support\Collection |
|
90
|
|
|
*/ |
|
91
|
1 |
|
public function items() |
|
92
|
|
|
{ |
|
93
|
1 |
|
return $this->items; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* count. |
|
98
|
|
|
* |
|
99
|
|
|
* @return int |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function count() |
|
102
|
|
|
{ |
|
103
|
1 |
|
return $this->items->count(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* total. |
|
108
|
|
|
* |
|
109
|
|
|
* @return float |
|
110
|
|
|
*/ |
|
111
|
1 |
|
public function total() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->items->sum(function ($item) { |
|
114
|
1 |
|
return $item->getPrice() * $item->getQuantity(); |
|
115
|
1 |
|
}); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* restore. |
|
120
|
|
|
* |
|
121
|
|
|
* @return $this |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function restore() { |
|
124
|
1 |
|
$this->items = $this->storage->restore(); |
|
125
|
|
|
|
|
126
|
1 |
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* store. |
|
131
|
|
|
* |
|
132
|
|
|
* @return $this |
|
133
|
|
|
*/ |
|
134
|
1 |
|
public function store() |
|
135
|
|
|
{ |
|
136
|
1 |
|
$this->storage->store($this->items); |
|
137
|
|
|
|
|
138
|
1 |
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* toArray. |
|
143
|
|
|
* |
|
144
|
|
|
* @return array |
|
145
|
|
|
*/ |
|
146
|
|
|
public function toArray() |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->items->map(function ($item) { |
|
149
|
|
|
return $item->toArray(); |
|
150
|
|
|
})->toArray(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* toJson. |
|
155
|
|
|
* |
|
156
|
|
|
* @param int $option |
|
157
|
|
|
* @return string |
|
158
|
|
|
*/ |
|
159
|
|
|
public function toJson($option = 0) |
|
160
|
|
|
{ |
|
161
|
|
|
return json_encode($this->toArray(), $option); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* __destruct. |
|
166
|
|
|
*/ |
|
167
|
1 |
|
public function __destruct() |
|
168
|
|
|
{ |
|
169
|
1 |
|
$this->store(); |
|
170
|
1 |
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Determine if an item exists at an offset. |
|
174
|
|
|
* |
|
175
|
|
|
* @param mixed $key |
|
176
|
|
|
* @return bool |
|
177
|
|
|
*/ |
|
178
|
|
|
public function offsetExists($key) |
|
179
|
|
|
{ |
|
180
|
|
|
return $this->items->offsetExists($key); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Get an item at a given offset. |
|
185
|
|
|
* |
|
186
|
|
|
* @param mixed $key |
|
187
|
|
|
* @return mixed |
|
188
|
|
|
*/ |
|
189
|
|
|
public function offsetGet($key) |
|
190
|
|
|
{ |
|
191
|
|
|
return $this->items->offsetGet($key); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Set the item at a given offset. |
|
196
|
|
|
* |
|
197
|
|
|
* @param mixed $key |
|
198
|
|
|
* @param mixed $value |
|
199
|
|
|
* @return void |
|
200
|
|
|
*/ |
|
201
|
|
|
public function offsetSet($key, $value) |
|
202
|
|
|
{ |
|
203
|
|
|
$this->items->offsetSet($key, $value); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Unset the item at a given offset. |
|
208
|
|
|
* |
|
209
|
|
|
* @param string $key |
|
210
|
|
|
* @return void |
|
211
|
|
|
*/ |
|
212
|
|
|
public function offsetUnset($key) |
|
213
|
|
|
{ |
|
214
|
|
|
$this->items->offsetUnset($key); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Get an iterator for the items. |
|
219
|
|
|
* |
|
220
|
|
|
* @return \ArrayIterator |
|
221
|
|
|
*/ |
|
222
|
|
|
public function getIterator() |
|
223
|
|
|
{ |
|
224
|
|
|
return new ArrayIterator($this->items->all()); |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.