Commit 8bae3bee authored by Thorsten Buss's avatar Thorsten Buss

* rename some Classes for better usage (more laravel style)

* change the namespace
parent 509bbbb9
# Laravel 5 Shopping Cart
[![Build Status](https://travis-ci.org/darryldecode/laravelshoppingcart.svg?branch=master)](https://travis-ci.org/darryldecode/laravelshoppingcart)
[![Total Downloads](https://poser.pugx.org/darryldecode/cart/d/total.svg)](https://packagist.org/packages/darryldecode/cart)
[![License](https://poser.pugx.org/darryldecode/cart/license.svg)](https://packagist.org/packages/darryldecode/cart)
A Shopping Cart Implementation for Laravel Framework
*** THIS PACKAGE IS BASED ON darryldecode/cart - ALL CREDITS TO HIM ***
##INSTALLATION
Install the package through [Composer](http://getcomposer.org/). Edit your project's `composer.json` file by adding:
### Laravel 5
```php
"require": {
"laravel/framework": "5.0.*",
"darryldecode/cart": "dev-master"
}
```
composer require "bnet/cart"
Next, run the Composer update command from the Terminal:
composer update
or
composer update "darryldecode/cart"
##CONFIGURATION
1. Open config/app.php and addd this line to your Service Providers Array
```php
'Darryldecode\Cart\CartServiceProvider'
'Bnet\Cart\CartServiceProvider'
```
2. Open config/app.php and addd this line to your Aliases
```php
'Cart' => 'Darryldecode\Cart\Facades\CartFacade'
'Cart' => 'Bnet\Cart\Facades\CartFacade'
```
## HOW TO USE
......@@ -193,7 +178,7 @@ Getting cart's contents and count: **Cart::items()**
/**
* get the cart
*
* @return CartCollection
* @return Items
*/
$cartCollection = Cart::items();
......@@ -291,7 +276,7 @@ Also, when adding conditions, the 'value' field will be the bases of calculation
```php
// add single condition on a cart bases
$condition = new \Darryldecode\Cart\CartCondition(array(
$condition = new \Bnet\Cart\CartCondition(array(
'name' => 'VAT 12.5%',
'type' => 'tax',
'target' => 'subtotal',
......@@ -305,13 +290,13 @@ $condition = new \Darryldecode\Cart\CartCondition(array(
Cart::condition($condition);
// or add multiple conditions from different condition instances
$condition1 = new \Darryldecode\Cart\CartCondition(array(
$condition1 = new \Bnet\Cart\CartCondition(array(
'name' => 'VAT 12.5%',
'type' => 'tax',
'target' => 'subtotal',
'value' => '12.5%',
));
$condition2 = new \Darryldecode\Cart\CartCondition(array(
$condition2 = new \Bnet\Cart\CartCondition(array(
'name' => 'Express Shipping $15',
'type' => 'shipping',
'target' => 'subtotal',
......@@ -366,7 +351,7 @@ Now let's add condition on an item.
```php
// lets create first our condition instance
$saleCondition = new \Darryldecode\Cart\CartCondition(array(
$saleCondition = new \Bnet\Cart\CartCondition(array(
'name' => 'SALE 5%',
'type' => 'tax',
'target' => 'item',
......@@ -387,7 +372,7 @@ $product = array(
Cart::add($product);
// you may also add multiple condition on an item
$itemCondition1 = new \Darryldecode\Cart\CartCondition(array(
$itemCondition1 = new \Bnet\Cart\CartCondition(array(
'name' => 'SALE 5%',
'type' => 'sale',
'target' => 'item',
......@@ -399,7 +384,7 @@ $itemCondition2 = new CartCondition(array(
'target' => 'item',
'value' => '-25',
));
$itemCondition3 = new \Darryldecode\Cart\CartCondition(array(
$itemCondition3 = new \Bnet\Cart\CartCondition(array(
'name' => 'MISC',
'type' => 'misc',
'target' => 'item',
......@@ -672,7 +657,7 @@ foreach($items as $item)
$item->quantity; // the quantity
$item->attributes; // the attributes
// Note that attribute returns ItemAttributeCollection object that extends the native laravel collection
// Note that attribute returns Attribute object that extends the native laravel collection
// so you can do things like below:
if( $item->attributes->has('size') )
......@@ -711,6 +696,12 @@ $items->each(function($item)
## Changelogs
**3.0.0
- the internal repesentation of amounts is now int instead of float
- rename some functions (items() instead of getItems()) and classes (Item instead of ItemCollection) for better usage (more laravel style)
- change the namespace to BNet
- make item_rules customizable (preparing for own CartClass with additional custom item fields)
**2.4.0
- added new method on a condition: $condition->getAttributes(); (Please see [Conditions](#conditions) section)
......
{
"name": "darryldecode/cart",
"name": "bnet/laravel-cart",
"description": "Laravel 5 Shopping cart",
"keywords": ["laravel", "shopping cart", "cart"],
"license": "MIT",
"authors": [
{
"name": "Darryl Fernandez",
"email": "engrdarrylfernandez@gmail.com"
"name": "Thorsten Buss",
"email": "thorsten.buss@buss-networks.de"
}
],
"require": {
......@@ -21,7 +21,7 @@
},
"autoload": {
"psr-4": {
"Darryldecode\\": "src/Darryldecode"
"Bnet\\": "src/"
}
},
"minimum-stability": "dev"
......
<?php namespace Darryldecode\Cart;
<?php namespace Bnet\Cart;
/**
* Created by PhpStorm.
......@@ -9,7 +9,7 @@
use Illuminate\Support\Collection;
class ItemAttributeCollection extends Collection {
class Attribute extends Collection {
public function __get($name) {
if ($this->has($name)) return $this->get($name);
......
<?php namespace Darryldecode\Cart;
<?php namespace Bnet\Cart;
use Darryldecode\Cart\Exceptions\InvalidConditionException;
use Darryldecode\Cart\Exceptions\InvalidItemException;
use Darryldecode\Cart\Helpers\Helpers;
use Darryldecode\Cart\Validators\CartItemValidator;
use Bnet\Cart\Exceptions\InvalidConditionException;
use Bnet\Cart\Exceptions\InvalidItemException;
use Bnet\Cart\Helpers\Helpers;
use Bnet\Cart\Validators\ItemValidator;
/**
* Class Cart
* @package Darryldecode\Cart
* @package Bnet\Cart
*/
class Cart {
......@@ -86,7 +86,7 @@ class Cart {
* get an item on a cart by item ID
*
* @param $itemId
* @return mixed
* @return Item
*/
public function get($itemId) {
return $this->items()->get($itemId);
......@@ -110,7 +110,7 @@ class Cart {
* @param int $price
* @param int $quantity
* @param array $attributes
* @param CartCondition|array $conditions
* @param Condition|array $conditions
* @return $this
* @throws InvalidItemException
*/
......@@ -151,7 +151,7 @@ class Cart {
'name' => $name,
'price' => Helpers::normalizePrice($price),
'quantity' => $quantity,
'attributes' => new ItemAttributeCollection($attributes),
'attributes' => new Attribute($attributes),
'conditions' => $conditions,
));
......@@ -209,7 +209,7 @@ class Cart {
$item = $this->updateQuantityRelative($item, $key, $value);
}
} elseif ($key == 'attributes') {
$item[$key] = new ItemAttributeCollection($value);
$item[$key] = new Attribute($value);
} else {
$item[$key] = $value;
}
......@@ -226,30 +226,26 @@ class Cart {
* add condition on an existing item on the cart
*
* @param int|string $productId
* @param CartCondition $itemCondition
* @param Condition $itemCondition
* @return $this
*/
public function addItemCondition($productId, $itemCondition) {
public function addItemCondition($productId, Condition $itemCondition) {
if ($product = $this->get($productId)) {
$conditionInstance = "\\Darryldecode\\Cart\\CartCondition";
if ($itemCondition instanceof $conditionInstance) {
// we need to copy first to a temporary variable to hold the conditions
// to avoid hitting this error "Indirect modification of overloaded element of Darryldecode\Cart\ItemCollection has no effect"
// this is due to laravel Collection instance that implements Array Access
// // see link for more info: http://stackoverflow.com/questions/20053269/indirect-modification-of-overloaded-element-of-splfixedarray-has-no-effect
$itemConditionTempHolder = $product['conditions'];
if (is_array($itemConditionTempHolder)) {
array_push($itemConditionTempHolder, $itemCondition);
} else {
$itemConditionTempHolder = $itemCondition;
}
$this->update($productId, array(
'conditions' => $itemConditionTempHolder // the newly updated conditions
));
// we need to copy first to a temporary variable to hold the conditions
// to avoid hitting this error "Indirect modification of overloaded element of Bnet\Cart\Item has no effect"
// this is due to laravel Collection instance that implements Array Access
// // see link for more info: http://stackoverflow.com/questions/20053269/indirect-modification-of-overloaded-element-of-splfixedarray-has-no-effect
$itemConditionTempHolder = $product['conditions'];
if (is_array($itemConditionTempHolder)) {
array_push($itemConditionTempHolder, $itemCondition);
} else {
$itemConditionTempHolder = $itemCondition;
}
$this->update($productId, array(
'conditions' => $itemConditionTempHolder // the newly updated conditions
));
}
return $this;
......@@ -289,7 +285,7 @@ class Cart {
/**
* add a condition on the cart
*
* @param CartCondition|array $condition
* @param Condition|array $condition
* @return $this
* @throws InvalidConditionException
*/
......@@ -302,7 +298,7 @@ class Cart {
return $this;
}
if (!$condition instanceof CartCondition) throw new InvalidConditionException('Argument 1 must be an instance of \'Darryldecode\Cart\CartCondition\'');
if (!$condition instanceof Condition) throw new InvalidConditionException('Argument 1 must be an instance of \'Bnet\Cart\CartCondition\'');
$conditions = $this->getConditions();
......@@ -316,17 +312,17 @@ class Cart {
/**
* get conditions applied on the cart
*
* @return CartConditionCollection
* @return Conditions
*/
public function getConditions() {
return new CartConditionCollection($this->session->get($this->sessionKeyCartConditions));
return new Conditions($this->session->get($this->sessionKeyCartConditions));
}
/**
* get condition applied on the cart by its name
*
* @param $conditionName
* @return CartCondition
* @return Condition
*/
public function getCondition($conditionName) {
return $this->getConditions()->get($conditionName);
......@@ -338,10 +334,10 @@ class Cart {
* specifically on an per item bases
*
* @param $type
* @return CartConditionCollection
* @return Conditions
*/
public function getConditionsByType($type) {
return $this->getConditions()->filter(function (CartCondition $condition) use ($type) {
return $this->getConditions()->filter(function (Condition $condition) use ($type) {
return $condition->getType() == $type;
});
}
......@@ -419,9 +415,7 @@ class Cart {
// on the given condition name the user wants to remove, if so,
// lets just make $item['conditions'] an empty array as there's just 1 condition on it anyway
else {
$conditionInstance = "Darryldecode\\Cart\\CartCondition";
if ($item['conditions'] instanceof $conditionInstance) {
if ($item['conditions'] instanceof Condition) {
if ($tempConditionsHolder->getName() == $conditionName) {
$item['conditions'] = array();
}
......@@ -533,10 +527,10 @@ class Cart {
/**
* get the cart
*
* @return CartCollection
* @return Items
*/
public function items() {
return (new CartCollection($this->session->get($this->sessionKeyCartItems)));
return (new Items($this->session->get($this->sessionKeyCartItems)));
}
/**
......@@ -545,7 +539,7 @@ class Cart {
* @return bool
*/
public function isEmpty() {
$cart = new CartCollection($this->session->get($this->sessionKeyCartItems));
$cart = new Items($this->session->get($this->sessionKeyCartItems));
return $cart->isEmpty();
}
......@@ -558,7 +552,7 @@ class Cart {
* @throws InvalidItemException
*/
protected function validate($item) {
$validator = CartItemValidator::make($item, $this->item_rules);
$validator = ItemValidator::make($item, $this->item_rules);
if ($validator->fails()) {
throw new InvalidItemException($validator->messages()->first());
......@@ -578,7 +572,7 @@ class Cart {
$cart = $this->items();
$cart->put($id, new ItemCollection($item));
$cart->put($id, new Item($item));
$this->save($cart);
......@@ -588,7 +582,7 @@ class Cart {
/**
* save the cart
*
* @param $cart CartCollection
* @param $cart Items
*/
protected function save($cart) {
$this->session->put($this->sessionKeyCartItems, $cart);
......@@ -610,17 +604,13 @@ class Cart {
* @return bool
*/
protected function itemHasConditions($item) {
if (!isset($item['conditions'])) return false;
if (!isset($item['conditions']))
return false;
if (is_array($item['conditions'])) {
if (is_array($item['conditions']))
return count($item['conditions']) > 0;
}
$conditionInstance = "Darryldecode\\Cart\\CartCondition";
if ($item['conditions'] instanceof $conditionInstance) return true;
return false;
return $item['conditions'] instanceof Condition;
}
/**
......
<?php namespace Darryldecode\Cart;
<?php namespace Bnet\Cart;
use Illuminate\Support\ServiceProvider;
......
<?php namespace Darryldecode\Cart;
<?php namespace Bnet\Cart;
use Darryldecode\Cart\Exceptions\InvalidConditionException;
use Darryldecode\Cart\Helpers\Helpers;
use Darryldecode\Cart\Validators\CartConditionValidator;
use Bnet\Cart\Exceptions\InvalidConditionException;
use Bnet\Cart\Helpers\Helpers;
use Bnet\Cart\Validators\ConditionValidator;
/**
* Created by PhpStorm.
......@@ -10,7 +10,7 @@ use Darryldecode\Cart\Validators\CartConditionValidator;
* Date: 1/15/2015
* Time: 9:02 PM
*/
class CartCondition {
class Condition {
/**
* @var array
......@@ -216,7 +216,7 @@ class CartCondition {
'value' => 'required',
);
$validator = CartConditionValidator::make($args, $rules);
$validator = ConditionValidator::make($args, $rules);
if ($validator->fails()) {
throw new InvalidConditionException($validator->messages()->first());
......
<?php namespace Darryldecode\Cart;
<?php namespace Bnet\Cart;
/**
* Created by PhpStorm.
......@@ -9,6 +9,6 @@
use Illuminate\Support\Collection;
class CartConditionCollection extends Collection {
class Conditions extends Collection {
}
\ No newline at end of file
<?php namespace Darryldecode\Cart\Exceptions;
<?php namespace Bnet\Cart\Exceptions;
/**
* Created by PhpStorm.
......
<?php namespace Darryldecode\Cart\Exceptions;
<?php namespace Bnet\Cart\Exceptions;
/**
* Created by PhpStorm.
......
<?php namespace Darryldecode\Cart\Facades;
<?php namespace Bnet\Cart\Facades;
use Illuminate\Support\Facades\Facade;
......
<?php namespace Darryldecode\Cart\Helpers;
<?php namespace Bnet\Cart\Helpers;
/**
* Created by PhpStorm.
......
<?php namespace Darryldecode\Cart;
<?php namespace Bnet\Cart;
/**
* Created by PhpStorm.
......@@ -9,16 +9,15 @@
use Illuminate\Support\Collection;
class ItemCollection extends Collection {
/**
* get the price
*
* @return mixed|null
*/
public function price() {
return $this->price;
}
/**
* @property int id
* @property int quantity
* @property string name
* @property mixed|null price
* @property Attribute attributes
* @property Conditions conditions
*/
class Item extends Collection {
/**
* get the sum of price
......@@ -40,14 +39,13 @@ class ItemCollection extends Collection {
* @return bool
*/
public function hasConditions() {
if (!isset($this['conditions'])) return false;
if (is_array($this['conditions'])) {
if (!isset($this['conditions']))
return false;
if (is_array($this['conditions']))
return count($this['conditions']) > 0;
}
$conditionInstance = "Darryldecode\\Cart\\CartCondition";
if ($this['conditions'] instanceof $conditionInstance) return true;
return false;
return $this['conditions'] instanceof Condition;
}
/**
......
<?php namespace Darryldecode\Cart;
<?php namespace Bnet\Cart;
use Illuminate\Support\Collection;
class CartCollection extends Collection {
class Items extends Collection {
}
\ No newline at end of file
<?php namespace Darryldecode\Cart\Validators;
<?php namespace Bnet\Cart\Validators;
/**
* Created by PhpStorm.
......@@ -7,6 +7,6 @@
* Time: 11:03 AM
*/
class CartConditionValidator extends Validator {
class ConditionValidator extends Validator {
}
\ No newline at end of file
<?php namespace Darryldecode\Cart\Validators;
<?php namespace Bnet\Cart\Validators;
/**
* Created by PhpStorm.
......@@ -7,6 +7,6 @@
* Time: 11:00 AM
*/
class CartItemValidator extends Validator {
class ItemValidator extends Validator {
}
\ No newline at end of file
<?php namespace Darryldecode\Cart\Validators;
<?php namespace Bnet\Cart\Validators;
use Symfony\Component\Translation\Translator;
use Illuminate\Validation\Factory;
......@@ -15,6 +15,7 @@ abstract class Validator {
public static function instance() {
if (!static::$factory) {
// $translator = new Translator(\Config::get('app.locale'));
$translator = new Translator('en');
static::$factory = new Factory($translator);
}
......
This diff is collapsed.
......@@ -6,7 +6,7 @@
* Time: 9:59 PM
*/
use Darryldecode\Cart\Cart;
use Bnet\Cart\Cart;
use Mockery as m;
require_once __DIR__ . '/helpers/SessionMock.php';
......@@ -14,7 +14,7 @@ require_once __DIR__ . '/helpers/SessionMock.php';
class CartTest extends PHPUnit_Framework_TestCase {
/**
* @var Darryldecode\Cart\Cart
* @var Bnet\Cart\Cart
*/
protected $cart;
......@@ -61,6 +61,7 @@ class CartTest extends PHPUnit_Framework_TestCase {
}
public function test_cart_can_add_items_with_multidimensional_array() {
$this->assertCount(0, $this->cart->items()->toArray(), 'Cart should have 0 items');
$items = array(
array(
'id' => 456,
......@@ -92,6 +93,7 @@ class CartTest extends PHPUnit_Framework_TestCase {
}
public function test_cart_can_add_item_without_attributes() {
$this->assertTrue($this->cart->isEmpty(), 'Cart should be empty');
$item = array(
'id' => 456,
'name' => 'Sample Item 1',
......@@ -104,7 +106,7 @@ class CartTest extends PHPUnit_Framework_TestCase {
$this->assertFalse($this->cart->isEmpty(), 'Cart should not be empty');
}
public function test_cart_update_with_attribute_then_attributes_should_be_still_instance_of_ItemAttributeCollection() {
public function test_cart_update_with_attribute_then_attributes_should_be_still_instance_of_Attribute() {
$item = array(
'id' => 456,
'name' => 'Sample Item 1',
......@@ -118,22 +120,24 @@ class CartTest extends PHPUnit_Framework_TestCase {
$this->cart->add($item);
// lets get the attribute and prove first its an instance of
// ItemAttributeCollection
// Attribute
$item = $this->cart->get(456);
$this->assertInstanceOf('Darryldecode\Cart\ItemAttributeCollection', $item->attributes);
$this->assertInstanceOf('Bnet\Cart\Attribute', $item->attributes);
$this->assertEquals('red', $item->attributes->get('color'), 'attribute value matched');
// now lets update the item with its new attributes
// when we get that item from cart, it should still be an instance of ItemAttributeCollection
// when we get that item from cart, it should still be an instance of Attribute
$updatedItem = array(
'attributes' => array(
'product_id' => '145',
'color' => 'red'
'color' => 'blue'
)
);
$this->cart->update(456, $updatedItem);
$this->assertInstanceOf('Darryldecode\Cart\ItemAttributeCollection', $item->attributes);
$this->assertInstanceOf('Bnet\Cart\Attribute', $item->attributes);
$this->assertEquals('blue', $item->attributes->get('color'), 'attribute value matched');
}
public function test_cart_items_attributes() {
......@@ -397,19 +401,19 @@ class CartTest extends PHPUnit_Framework_TestCase {
}
public function test_should_throw_exception_when_provided_invalid_values_scenario_one() {
$this->setExpectedException('Darryldecode\Cart\Exceptions\InvalidItemException');
$this->setExpectedException('Bnet\Cart\Exceptions\InvalidItemException');
$this->cart->add(455, 'Sample Item', 10099, 0, array());
}
public function test_should_throw_exception_when_provided_invalid_values_scenario_two() {
$this->setExpectedException('Darryldecode\Cart\Exceptions\InvalidItemException');
$this->setExpectedException('Bnet\Cart\Exceptions\InvalidItemException');
$this->cart->add('', 'Sample Item', 10099, 2, array());
}
public function test_should_throw_exception_when_provided_invalid_values_scenario_three() {
$this->setExpectedException('Darryldecode\Cart\Exceptions\InvalidItemException');
$this->setExpectedException('Bnet\Cart\Exceptions\InvalidItemException');
$this->cart->add(523, '', 10099, 2, array());
}
......
......@@ -6,7 +6,7 @@
* Time: 3:20 PM
*/
use Darryldecode\Cart\Cart;
use Bnet\Cart\Cart;
use Mockery as m;
require_once __DIR__ . '/helpers/SessionMock.php';
......
......@@ -6,7 +6,7 @@
* Time: 1:45 PM
*/
use Darryldecode\Cart\Cart;
use Bnet\Cart\Cart;
use Mockery as m;
require_once __DIR__ . '/helpers/SessionMock.php';
......@@ -14,12 +14,12 @@ require_once __DIR__ . '/helpers/SessionMock.php';
class CartTestMultipleInstances extends PHPUnit_Framework_TestCase {
/**
* @var Darryldecode\Cart\Cart
* @var Bnet\Cart\Cart
*/
protected $cart1;
/**
* @var Darryldecode\Cart\Cart
* @var Bnet\Cart\Cart
*/
protected $cart2;
......
......@@ -6,7 +6,7 @@
* Time: 6:17 PM
*/
use Darryldecode\Cart\Cart;
use Bnet\Cart\Cart;
use Mockery as m;
require_once __DIR__ . '/helpers/SessionMock.php';
......@@ -14,7 +14,7 @@ require_once __DIR__ . '/helpers/SessionMock.php';
class ItemTest extends PHPUnit_Framework_TestCase {
/**
* @var Darryldecode\Cart\Cart
* @var Bnet\Cart\Cart
*/
protected $cart;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment