Skip to content

Commit 65f3bcd

Browse files
committed
updated to laravel 5, PSR-4 and updated readme docs
1 parent 2602f70 commit 65f3bcd

File tree

10 files changed

+163
-105
lines changed

10 files changed

+163
-105
lines changed

README.md

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Laravel Shopping Cart
1+
# Laravel 5 Shopping Cart
22
[![Build Status](https://travis-ci.org/darryldecode/laravelshoppingcart.svg?branch=master)](https://travis-ci.org/darryldecode/laravelshoppingcart)
33
[![Total Downloads](https://poser.pugx.org/darryldecode/cart/downloads.svg)](https://packagist.org/packages/darryldecode/cart)
44
[![License](https://poser.pugx.org/darryldecode/cart/license.svg)](https://packagist.org/packages/darryldecode/cart)
@@ -12,7 +12,7 @@ Install the package through [Composer](http://getcomposer.org/). Edit your proje
1212
```php
1313
"require": {
1414
"laravel/framework": "4.2.*",
15-
"darryldecode/laravelshoppingcart": "dev-master"
15+
"darryldecode/cart": "dev-master"
1616
}
1717
```
1818

@@ -36,6 +36,7 @@ Next, run the Composer update command from the Terminal:
3636
* [Instances](#instances)
3737
* [Exceptions](#exceptions)
3838
* [Events](#events)
39+
* [Examples](#examples)
3940
* [License](#license)
4041

4142
## Usage
@@ -400,6 +401,76 @@ So for you wishlist cart instance, events will look like this:
400401
* wishlist.adding($items, $cart)
401402
* wishlist.added($items, $cart) and so on..
402403

404+
## Examples
405+
406+
```php
407+
408+
// add items to cart
409+
Cart::add(array(
410+
array(
411+
'id' => 456,
412+
'name' => 'Sample Item 1',
413+
'price' => 67.99,
414+
'quantity' => 4,
415+
'attributes' => array()
416+
),
417+
array(
418+
'id' => 568,
419+
'name' => 'Sample Item 2',
420+
'price' => 69.25,
421+
'quantity' => 4,
422+
'attributes' => array(
423+
'size' => 'L',
424+
'color' => 'blue'
425+
)
426+
),
427+
));
428+
429+
// then you can:
430+
$items = Cart::getContent();
431+
432+
foreach($items as $item)
433+
{
434+
$item->id; // the Id of the item
435+
$item->name; // the name
436+
$item->price; // the price
437+
$item->quantity; // the quantity
438+
$item->attributes; // the attributes
439+
440+
// Note that attribute returns ItemAttributeCollection object that extends the native laravel collection
441+
// so you can do things like below:
442+
443+
if( $item->attributes->has('size') )
444+
{
445+
// item has attribute size
446+
}
447+
else
448+
{
449+
// item has no attribute size
450+
}
451+
}
452+
453+
// or
454+
$items->each(function($item)
455+
{
456+
$item->id; // the Id of the item
457+
$item->name; // the name
458+
$item->price; // the price
459+
$item->quantity; // the quantity
460+
$item->attributes; // the attributes
461+
462+
if( $item->attributes->has('size') )
463+
{
464+
// item has attribute size
465+
}
466+
else
467+
{
468+
// item has no attribute size
469+
}
470+
});
471+
472+
```
473+
403474
## License
404475

405476
The Laravel Shopping Cart is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)

composer.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "darryldecode/cart",
3-
"description": "Laravel 4 Shopping cart",
3+
"description": "Laravel 5 Shopping cart",
44
"keywords": ["laravel", "shopping cart", "cart"],
55
"license": "MIT",
66
"authors": [
@@ -11,18 +11,17 @@
1111
],
1212
"require": {
1313
"php": ">=5.4.0",
14-
"illuminate/support": "4.2.*@dev",
15-
"illuminate/validation": "4.2.*@dev"
14+
"illuminate/support": "5.0.*@dev",
15+
"illuminate/validation": "5.0.*@dev"
1616
},
1717
"require-dev": {
1818
"mockery/mockery": "~0.9",
1919
"phpunit/phpunit": "~4.0",
20-
"symfony/var-dumper": "2.7.*@dev",
21-
"illuminate/events": "4.2.*@dev"
20+
"symfony/var-dumper": "2.7.*@dev"
2221
},
2322
"autoload": {
24-
"psr-0": {
25-
"Darryldecode\\Cart": "src/"
23+
"psr-4": {
24+
"Darryldecode\\": "src/Darryldecode"
2625
}
2726
},
2827
"minimum-stability": "dev"

src/Darryldecode/Cart/Cart.php

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,13 @@ public function getInstanceName()
8383
* get an item on a cart by item ID
8484
*
8585
* @param $itemId
86-
* @return null
86+
* @return mixed
8787
*/
8888
public function get($itemId)
8989
{
9090
$contents = $this->getContent();
9191

92-
$itemToBeReturned = null;
93-
94-
$contents->each(function($item) use ($itemId, &$itemToBeReturned)
95-
{
96-
if( $item['id'] == $itemId )
97-
{
98-
$itemToBeReturned = $item;
99-
}
100-
});
101-
102-
return $itemToBeReturned;
92+
return $contents->get($itemId);
10393
}
10494

10595
/**
@@ -151,24 +141,21 @@ public function add($id, $name = null, $price = null, $quantity = null, $attribu
151141
return $this;
152142
}
153143

154-
// prepare the item data
155-
$item = array(
144+
// validate data
145+
$item = $this->validate(array(
156146
'id' => $id,
157147
'name' => $name,
158148
'price' => Helpers::normalizePrice($price),
159149
'quantity' => $quantity,
160-
'attributes' => $attributes,
150+
'attributes' => new ItemAttributeCollection($attributes),
161151
'conditions' => $conditions,
162-
);
163-
164-
// validate data
165-
$this->validate($item);
152+
));
166153

167154
// get the cart
168155
$cart = $this->getContent();
169156

170157
// if the item is already in the cart we will just update it
171-
if( $cart->hasItem($id) )
158+
if( $cart->has($id) )
172159
{
173160
$this->events->fire($this->getInstanceName().'.updating', array($item, $this));
174161

@@ -180,9 +167,7 @@ public function add($id, $name = null, $price = null, $quantity = null, $attribu
180167
{
181168
$this->events->fire($this->getInstanceName().'.adding', array($item, $this));
182169

183-
$cart->push($item);
184-
185-
$this->save($cart);
170+
$this->addRow($id, $item);
186171

187172
$this->events->fire($this->getInstanceName().'.added', array($item, $this));
188173
}
@@ -194,23 +179,23 @@ public function add($id, $name = null, $price = null, $quantity = null, $attribu
194179
* update a cart
195180
*
196181
* @param $id
197-
* @param array $data
182+
* @param $data
198183
*
199184
* the $data will be an associative array, you don't need to pass all the data, only the key value
200185
* of the item you want to update on it
201186
*/
202-
public function update($id, array $data)
187+
public function update($id, $data)
203188
{
204189
$cart = $this->getContent();
205190

206-
$item = $cart->pullItem($id);
191+
$item = $cart->pull($id);
207192

208193
foreach($data as $key => $value)
209194
{
210195
$item[$key] = $value;
211196
}
212197

213-
$cart->push($item);
198+
$cart->put($id, $item);
214199

215200
$this->save($cart);
216201
}
@@ -226,7 +211,7 @@ public function remove($id)
226211

227212
$this->events->fire($this->getInstanceName().'.removing', array($id, $this));
228213

229-
$cart->removeItem($id);
214+
$cart->forget($id);
230215

231216
$this->save($cart);
232217

@@ -295,17 +280,17 @@ public function getSubTotal()
295280

296281
$sum = $cart->sum(function($item)
297282
{
298-
$originalPrice = $item['price'];
283+
$originalPrice = $item->price;
299284

300285
$newPrice = 0.00;
301286

302287
$processed = 0;
303288

304289
if( $this->itemHasConditions($item) )
305290
{
306-
if( is_array($item['conditions']) )
291+
if( is_array($item->conditions) )
307292
{
308-
foreach($item['conditions'] as $condition)
293+
foreach($item->conditions as $condition)
309294
{
310295
if( $condition->getTarget() === 'subtotal' )
311296
{
@@ -325,11 +310,11 @@ public function getSubTotal()
325310
}
326311
}
327312

328-
return $newPrice * $item['quantity'];
313+
return $newPrice * $item->quantity;
329314
}
330315
else
331316
{
332-
return $originalPrice * $item['quantity'];
317+
return $originalPrice * $item->quantity;
333318
}
334319
});
335320

@@ -373,7 +358,7 @@ public function getTotal()
373358
*/
374359
public function getContent()
375360
{
376-
return (new CartCollection($this->session->get($this->sessionKey)))->values();
361+
return (new CartCollection($this->session->get($this->sessionKey)));
377362
}
378363

379364
/**
@@ -392,8 +377,8 @@ public function isEmpty()
392377
* validate Item data
393378
*
394379
* @param $item
380+
* @return array $item;
395381
* @throws InvalidItemException
396-
* @throws InvalidItemFieldException
397382
*/
398383
protected function validate($item)
399384
{
@@ -410,6 +395,23 @@ protected function validate($item)
410395
{
411396
throw new InvalidItemException($validator->messages()->first());
412397
}
398+
399+
return $item;
400+
}
401+
402+
/**
403+
* add row to cart collection
404+
*
405+
* @param $id
406+
* @param $item
407+
*/
408+
protected function addRow($id, $item)
409+
{
410+
$cart = $this->getContent();
411+
412+
$cart->put($id, new ItemCollection($item));
413+
414+
$this->save($cart);
413415
}
414416

415417
/**
@@ -419,8 +421,6 @@ protected function validate($item)
419421
*/
420422
protected function save($cart)
421423
{
422-
$cart = $cart->toArray();
423-
424424
$this->session->put($this->sessionKey, $cart);
425425
}
426426

src/Darryldecode/Cart/CartCollection.php

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,4 @@
44

55
class CartCollection extends Collection {
66

7-
/**
8-
* check if the cart contains an item with the given ID
9-
*
10-
* @param $itemId
11-
* @return bool
12-
*/
13-
public function hasItem($itemId)
14-
{
15-
foreach($this->items as $item)
16-
{
17-
if( $item['id'] === $itemId ) return true;
18-
}
19-
return false;
20-
}
21-
22-
/**
23-
* pull an item on cart content using item id and then remove it totally
24-
*
25-
* @param $itemId
26-
* @return null
27-
*/
28-
public function pullItem($itemId)
29-
{
30-
$itemToBePulled = null;
31-
32-
foreach($this->items as $k => $v)
33-
{
34-
if( $v['id'] === $itemId )
35-
{
36-
$this->forget($k);
37-
$itemToBePulled = $v;
38-
}
39-
}
40-
41-
return $itemToBePulled;
42-
}
43-
44-
/**
45-
* remove an item on the cart by item Id
46-
*
47-
* @param $itemId
48-
*/
49-
public function removeItem($itemId)
50-
{
51-
foreach($this->items as $k => $v)
52-
{
53-
if( $v['id'] === $itemId )
54-
{
55-
$this->forget($k);
56-
}
57-
}
58-
}
597
}

0 commit comments

Comments
 (0)