Commit 86573aee authored by Thorsten Buss's avatar Thorsten Buss

* allow to add an already created Item Object or a list of items

* add CurrencyCartServiceProvider
* change the calculation-princip of the currencyItems
parent a200b566
......@@ -13,10 +13,17 @@ Install the package through [Composer](http://getcomposer.org/). Edit your proje
##CONFIGURATION
1. Open config/app.php and addd this line to your Service Providers Array
1. Open config/app.php and addd this line to your Service Providers Array if you want int prices
```php
Bnet\Cart\CartServiceProvider::class
```
OR if you want Money() prices:
```php
Bnet\Cart\CurrencyCartServiceProvider::class
```
2. Open config/app.php and addd this line to your Aliases
......@@ -696,6 +703,11 @@ $items->each(function($item)
## Changelogs
**3.1.0
- allow to add an already created Item Object or a list of items
- add CurrencyCartServiceProvider
- change the calculation-princip of the currencyItems
**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)
......
......@@ -13,7 +13,7 @@
"php": ">=5.4.0",
"illuminate/support": "5.0.x|5.1.x|5.2.x",
"illuminate/validation": "5.0.x|5.1.x|5.2.x",
"bnet/money-datatype": "^0.1.0"
"bnet/money-datatype": "^0.1.3"
},
"require-dev": {
"mockery/mockery": "~0.9",
......
......@@ -143,17 +143,26 @@ class Cart {
}
return $this;
} elseif ($id instanceOf Item) {
$item = $id;
$id = $item->id;
} elseif ($id instanceOf Items) {
foreach ($id as $item) {
$this->add($item);
}
return $this;
} else {
// validate data
$item = $this->validate(array(
'id' => $id,
'name' => $name,
'price' => Helpers::normalizePrice($price),
'quantity' => $quantity,
'attributes' => new Attribute($attributes),
'conditions' => $conditions,
));
}
// validate data
$item = $this->validate(array(
'id' => $id,
'name' => $name,
'price' => Helpers::normalizePrice($price),
'quantity' => $quantity,
'attributes' => new Attribute($attributes),
'conditions' => $conditions,
));
// get the cart
$cart = $this->items();
......@@ -566,11 +575,15 @@ class Cart {
* @param $item
*/
protected function addRow($id, $item) {
// convert array to CartItem
if (!$item instanceof Item)
$item = $this->createCartItem($item);
$this->events->fire($this->getInstanceName() . '.adding', array($item, $this));
$items = $this->items();
$items->put($id, $this->createCartItem($item));
$items->put($id, $item);
$this->save($items);
......
......@@ -23,14 +23,14 @@ class CurrencyCart extends Cart {
/**
* our object constructor
*
* @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session
* @param \Illuminate\Session\SessionManager $session
* @param \Illuminate\Contracts\Events\Dispatcher $events
* @param string $instanceName
* @param string $session_key
* @param string|Currency $currency Alpha IsoCode of the Currency of this Cart - only items with this currency are allowed and items without currency get this currency
* @param array $custom_item_rules overwrite existing item_rules
*/
public function __construct(\Symfony\Component\HttpFoundation\Session\SessionInterface $session, \Illuminate\Contracts\Events\Dispatcher $events, $instanceName, $session_key, $currency='EUR', $custom_item_rules = []) {
public function __construct($session, \Illuminate\Contracts\Events\Dispatcher $events, $instanceName, $session_key, $currency='EUR', $custom_item_rules = []) {
$this->currency = $currency instanceof Currency ? $currency : new Currency($currency);
parent::__construct($session, $events, $instanceName, $session_key, $custom_item_rules);
}
......@@ -104,7 +104,7 @@ class CurrencyCart extends Cart {
* @return $this
* @throws \Bnet\Cart\Exceptions\InvalidItemException
*/
public function add($id, $name = null, Money $price = null, $quantity = 1, $attributes = array(), $conditions = array()) {
public function add($id, $name = null, $price = null, $quantity = 1, $attributes = array(), $conditions = array()) {
if (!is_null($price) && !$price->currency()->equals($this->currency))
throw new \Bnet\Cart\Exceptions\CurrencyNotMachedException('given item-currency ['.$price->currency()->code.'] does not match to cart currency ['.$this->currency->code.']' );
return parent::add($id, $name, $price, $quantity, $attributes, $conditions);
......
<?php namespace Bnet\Cart;
use Illuminate\Support\ServiceProvider;
class CurrencyCartServiceProvider extends ServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Register the service provider.
*
* @return void
*/
public function register() {
$this->app['cart'] = $this->app->share(function ($app) {
$storage = $app['session'];
$events = $app['events'];
$instanceName = 'cart';
$session_key = '__cart';
return new CurrencyCart(
$storage,
$events,
$instanceName,
$session_key
);
});
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides() {
return array();
}
}
......@@ -25,25 +25,14 @@ class CurrencyItem extends Item {
protected $currency;
/**
* Create a new collection.
* Create a new Item with the given attributes.
*
* @param mixed $items
* @return void
* @param mixed $attributes
* @param string|Currency $currency
*/
public function __construct($items, $currency) {
public function __construct($attributes, $currency) {
$this->currency = $currency;
parent::__construct($items);
}
/**
* return the price amount for this item
* @return int|null
*/
public function price() {
return $this->price->amount();
$price = $this->price;
return $price instanceOf Money ? $price->amount() : $price;
parent::__construct($attributes);
}
/**
......@@ -52,7 +41,7 @@ class CurrencyItem extends Item {
* @return Money
*/
public function priceSum() {
return new Money(parent::priceSum(), $this->currency);
return $this->price->multiply($this->quantity);
}
/**
......@@ -61,7 +50,28 @@ class CurrencyItem extends Item {
* @return Money
*/
public function priceWithConditions() {
return new Money((int)parent::priceWithConditions(), $this->currency);
$originalPrice = $this->price->amount();
$newPrice = 0;
$processed = 0;
if ($this->hasConditions()) {
if (is_array($this->conditions)) {
foreach ($this->conditions as $condition) {
if ($condition->getTarget() === 'item') {
($processed > 0) ? $toBeCalculated = $newPrice : $toBeCalculated = $originalPrice;
$newPrice = $condition->applyCondition($toBeCalculated);
$processed++;
}
}
} else {
if ($this['conditions']->getTarget() === 'item') {
$newPrice = $this['conditions']->applyCondition($originalPrice);
}
}
return new Money((int)$newPrice, $this->currency);
}
return new Money((int)$originalPrice, $this->currency);
}
/**
......@@ -70,8 +80,7 @@ class CurrencyItem extends Item {
* @return Money
*/
public function priceSumWithConditions() {
$sum = $this->priceWithConditions()->amount() * $this->quantity;
return new Money((int)$sum, $this->currency);
return $this->priceWithConditions()->multiply($this->quantity);
}
}
\ No newline at end of file
......@@ -19,6 +19,18 @@ use Illuminate\Support\Collection;
*/
class Item extends Collection {
/**
* Create a new Item with the given attributes.
*
* @param mixed $attributes
* @return void
*/
public function __construct($attributes) {
parent::__construct($attributes);
}
/**
* get the sum of price
*
......
......@@ -200,7 +200,20 @@ class CurrencyCartTest extends PHPUnit_Framework_TestCase {
$item = $this->cart->get($itemIdToEvaluate);
$this->assertEquals('Renamed', $item['name'], 'Item name should be "Renamed"');
$this->assertEquals(105, $item['price']->amount(), 'Item price should be 105');
$this->assertEquals(5, $item['quantity'], 'Item quantity should be 2');
$this->assertEquals(5, $item['quantity'], 'Item quantity should be 8');
// add the first icon again - should update
$this->cart->add([
'id' => 456,
'name' => 'Sample Item 1',
'price' => new Money(6799),
'quantity' => 3,
'attributes' => array()
]);
$item = $this->cart->get($itemIdToEvaluate);
$this->assertEquals('Sample Item 1', $item['name'], 'Item name should be "Sample Item 1"');
$this->assertEquals(6799, $item['price']->amount(), 'Item price should be 6799');
$this->assertEquals(8, $item['quantity'], 'Item quantity should be 8');
}
public function test_cart_update_existing_item_with_quantity_as_array_and_not_relative() {
......
<?php
/**
* User: thorsten
* Date: 22.07.16
* Time: 13:50
*/
class CurrencyItemTest extends PHPUnit_Framework_TestCase {
public function testStaticFieldAccess() {
$money = new \Bnet\Money\Money(123456);
$item = new \Bnet\Cart\CurrencyItem([
'id' => 999,
'name' => 'Sample Item 1',
'price' => $money,
'quantity' => 3,
'blub' => 'x0',
'attributes' => array(
'test1' => 'Eins',
'test2' => 'Zwei',
)
], 'EUR');
$this->assertEquals('Sample Item 1', $item->name);
$this->assertEquals(999, $item->id);
$this->assertEquals(3, $item->quantity);
$this->assertTrue($money->equals($item->price));
$this->assertTrue($money->equals($item->price()));
$this->assertEquals(123456, $item->price->amount());
$this->assertEquals(3*123456, $item->priceSum()->amount());
// undefined param
$this->assertEquals('x0', $item->blub);
}
}
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