Commit 00167799 authored by Thomas Bilk's avatar Thomas Bilk

put condition targets into constants

parent e87ffd6e
...@@ -317,8 +317,8 @@ class Cart implements Jsonable, \JsonSerializable, Arrayable{ ...@@ -317,8 +317,8 @@ class Cart implements Jsonable, \JsonSerializable, Arrayable{
throw new InvalidConditionException('Argument 1 must be an instance of \'Bnet\Cart\CartCondition\''); throw new InvalidConditionException('Argument 1 must be an instance of \'Bnet\Cart\CartCondition\'');
// set target to cart if not set // set target to cart if not set
$condition->setTarget($condition->getTarget() ?: 'cart'); $condition->setTarget($condition->getTarget() ?: Condition::TARGET_CART);
if ($condition->getTarget() !== 'cart') if ($condition->getTarget() !== Condition::TARGET_CART)
throw new InvalidConditionException('target have to be cart for cart conditions'); throw new InvalidConditionException('target have to be cart for cart conditions');
$conditions = $this->getConditions(); $conditions = $this->getConditions();
...@@ -502,7 +502,7 @@ class Cart implements Jsonable, \JsonSerializable, Arrayable{ ...@@ -502,7 +502,7 @@ class Cart implements Jsonable, \JsonSerializable, Arrayable{
$condTotal = $this->getConditions($only_with_condition_type)->sum(function ($cond) use ($subTotal) { $condTotal = $this->getConditions($only_with_condition_type)->sum(function ($cond) use ($subTotal) {
/** @var Condition $cond */ /** @var Condition $cond */
return $cond->getTarget() === 'cart' return $cond->getTarget() === Condition::TARGET_CART
? $cond->applyCondition($subTotal) ? $cond->applyCondition($subTotal)
: 0; : 0;
}); });
...@@ -544,7 +544,7 @@ class Cart implements Jsonable, \JsonSerializable, Arrayable{ ...@@ -544,7 +544,7 @@ class Cart implements Jsonable, \JsonSerializable, Arrayable{
$condTotal = $cond->sum(function ($cond) use ($subTotal) { $condTotal = $cond->sum(function ($cond) use ($subTotal) {
/** @var Condition $cond */ /** @var Condition $cond */
return $cond->getTarget() === 'cart' return $cond->getTarget() === Condition::TARGET_CART
? $cond->applyCondition($subTotal) ? $cond->applyCondition($subTotal)
: 0; : 0;
}); });
......
...@@ -12,6 +12,10 @@ use Illuminate\Support\Collection; ...@@ -12,6 +12,10 @@ use Illuminate\Support\Collection;
*/ */
class Condition extends Collection { class Condition extends Collection {
const TARGET_CART = 'cart';
const TARGET_ITEM = 'item';
const TARGET_price = 'price';
/** /**
* List of validation rules for the condition * List of validation rules for the condition
* @var array * @var array
......
...@@ -64,7 +64,7 @@ class CurrencyCart extends Cart { ...@@ -64,7 +64,7 @@ class CurrencyCart extends Cart {
$condTotal = $this->getConditions($only_with_condition_type)->sum(function ($cond) use ($subTotal) { $condTotal = $this->getConditions($only_with_condition_type)->sum(function ($cond) use ($subTotal) {
/** @var Condition $cond */ /** @var Condition $cond */
$price =$cond->getTarget() === 'cart' $price =$cond->getTarget() === Condition::TARGET_CART
? $cond->applyCondition($subTotal) ? $cond->applyCondition($subTotal)
: 0; : 0;
return $price instanceof Money return $price instanceof Money
...@@ -106,9 +106,9 @@ class CurrencyCart extends Cart { ...@@ -106,9 +106,9 @@ class CurrencyCart extends Cart {
return $itemConditionTotal; return $itemConditionTotal;
/** @var Money $condTotal */ /** @var Money $condTotal */
$condTotal = $cond->reduce(function (Money $money = null, $cond) use ($subTotal) { $condTotal = $cond->reduce(function ($carry, $cond) use ($subTotal) {
/** @var Condition $cond */ /** @var Condition $cond */
$price = $cond->getTarget() === 'cart' $price = $cond->getTarget() === Condition::TARGET_CART
? $cond->applyCondition($subTotal->amount()) ? $cond->applyCondition($subTotal->amount())
: 0; : 0;
...@@ -116,11 +116,12 @@ class CurrencyCart extends Cart { ...@@ -116,11 +116,12 @@ class CurrencyCart extends Cart {
if (!$price instanceof Money) if (!$price instanceof Money)
$price = new Money((int)$price, $this->currency); $price = new Money((int)$price, $this->currency);
if (!$money) // set the $money at the first run /** @var Money $carry */
return $money = $price; if (!$carry) // set the $money at the first run
return $carry = $price;
else else
return $money->add($price); return $carry->add($price);
}); }, null);
return $condTotal->add($itemConditionTotal); return $condTotal->add($itemConditionTotal);
} }
......
...@@ -65,7 +65,7 @@ class CurrencyItem extends Item { ...@@ -65,7 +65,7 @@ class CurrencyItem extends Item {
$condition_price = $this->conditions->sum(function ($condition) use ($originalPrice, $type) { $condition_price = $this->conditions->sum(function ($condition) use ($originalPrice, $type) {
/** @var Condition $condition */ /** @var Condition $condition */
$price = ($condition && $condition->getTarget() === 'item' $price = ($condition && $condition->getTarget() === Condition::TARGET_ITEM
&& (is_null($type) || $type == $condition->getType())) && (is_null($type) || $type == $condition->getType()))
? $condition->applyCondition($originalPrice) ? $condition->applyCondition($originalPrice)
: 0; : 0;
...@@ -88,7 +88,7 @@ class CurrencyItem extends Item { ...@@ -88,7 +88,7 @@ class CurrencyItem extends Item {
$condition_price = $this->conditions->sum(function ($condition) use ($originalPrice, $type) { $condition_price = $this->conditions->sum(function ($condition) use ($originalPrice, $type) {
/** @var Condition $condition */ /** @var Condition $condition */
$price = ($condition && $condition->getTarget() === 'item' $price = ($condition && $condition->getTarget() === Condition::TARGET_ITEM
&& (is_null($type) || $type == $condition->getType())) && (is_null($type) || $type == $condition->getType()))
? $condition->applyConditionWithQuantity($originalPrice, $this->quantity) ? $condition->applyConditionWithQuantity($originalPrice, $this->quantity)
: 0; : 0;
......
...@@ -84,7 +84,7 @@ class Item extends Collection { ...@@ -84,7 +84,7 @@ class Item extends Collection {
$condition_price = $this->conditions->sum(function ($condition) use ($originalPrice, $type) { $condition_price = $this->conditions->sum(function ($condition) use ($originalPrice, $type) {
/** @var Condition $condition */ /** @var Condition $condition */
return ($condition && $condition->getTarget() === 'item' return ($condition && $condition->getTarget() === Condition::TARGET_ITEM
&& (is_null($type) || $type == $condition->getType())) && (is_null($type) || $type == $condition->getType()))
? $condition->applyCondition($originalPrice) ? $condition->applyCondition($originalPrice)
: 0; : 0;
...@@ -121,7 +121,7 @@ class Item extends Collection { ...@@ -121,7 +121,7 @@ class Item extends Collection {
$condition_price = $this->conditions->sum(function ($condition) use ($originalPrice, $type) { $condition_price = $this->conditions->sum(function ($condition) use ($originalPrice, $type) {
/** @var Condition $condition */ /** @var Condition $condition */
return ($condition && $condition->getTarget() === 'item' return ($condition && $condition->getTarget() === Condition::TARGET_ITEM
&& (is_null($type) || $type == $condition->getType())) && (is_null($type) || $type == $condition->getType()))
? $condition->applyConditionWithQuantity($originalPrice, $this->quantity) ? $condition->applyConditionWithQuantity($originalPrice, $this->quantity)
: 0; : 0;
...@@ -175,9 +175,9 @@ class Item extends Collection { ...@@ -175,9 +175,9 @@ class Item extends Collection {
$condition = new Condition($condition); $condition = new Condition($condition);
// ignore target==cart conditions // ignore target==cart conditions
if ($condition->getTarget() == 'cart') if ($condition->getTarget() == Condition::TARGET_CART)
return false; return false;
$condition->setTarget('item'); // set item as default it not set $condition->setTarget(Condition::TARGET_ITEM); // set item as default it not set
return $condition; return $condition;
}) })
......
This diff is collapsed.
...@@ -536,7 +536,7 @@ class CartTest extends PHPUnit_Framework_TestCase { ...@@ -536,7 +536,7 @@ class CartTest extends PHPUnit_Framework_TestCase {
$itemCondition = new Condition(array( $itemCondition = new Condition(array(
'name' => 'SALE 5%', 'name' => 'SALE 5%',
'type' => 'tax', 'type' => 'tax',
'target' => 'item', 'target' => Condition::TARGET_ITEM,
'value' => '-5%', 'value' => '-5%',
)); ));
$items = [ $items = [
...@@ -564,7 +564,7 @@ class CartTest extends PHPUnit_Framework_TestCase { ...@@ -564,7 +564,7 @@ class CartTest extends PHPUnit_Framework_TestCase {
$condition = new Condition(array( $condition = new Condition(array(
'name' => 'VAT 12.5%', 'name' => 'VAT 12.5%',
'type' => 'tax', 'type' => 'tax',
'target' => 'cart', 'target' => Condition::TARGET_CART,
'value' => '12.5%', 'value' => '12.5%',
)); ));
...@@ -574,7 +574,7 @@ class CartTest extends PHPUnit_Framework_TestCase { ...@@ -574,7 +574,7 @@ class CartTest extends PHPUnit_Framework_TestCase {
'SALE 5%' => [ 'SALE 5%' => [
'name' => 'SALE 5%', 'name' => 'SALE 5%',
'type' => 'tax', 'type' => 'tax',
'target' => 'item', 'target' => Condition::TARGET_ITEM,
'value' => '-5%', 'value' => '-5%',
] ]
]; ];
...@@ -603,7 +603,7 @@ class CartTest extends PHPUnit_Framework_TestCase { ...@@ -603,7 +603,7 @@ class CartTest extends PHPUnit_Framework_TestCase {
'VAT 12.5%' => [ 'VAT 12.5%' => [
'name' => 'VAT 12.5%', 'name' => 'VAT 12.5%',
'type' => 'tax', 'type' => 'tax',
'target' => 'cart', 'target' => Condition::TARGET_CART,
'value' => '12.5%', 'value' => '12.5%',
] ]
] ]
......
This diff is collapsed.
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
* Time: 9:59 PM * Time: 9:59 PM
*/ */
use Bnet\Cart\Condition;
use Bnet\Cart\CurrencyCart as Cart; use Bnet\Cart\CurrencyCart as Cart;
use Bnet\Money\Money; use Bnet\Money\Money;
use Mockery as m; use Mockery as m;
...@@ -503,7 +504,7 @@ class CurrencyCartTest extends PHPUnit_Framework_TestCase { ...@@ -503,7 +504,7 @@ class CurrencyCartTest extends PHPUnit_Framework_TestCase {
new \Bnet\Cart\CurrencyCondition([ new \Bnet\Cart\CurrencyCondition([
'name' => 'Absolute Tax Plus', 'name' => 'Absolute Tax Plus',
'type' => 'misc2', 'type' => 'misc2',
'target' => 'item', 'target' => Condition::TARGET_ITEM,
'value' => new \Bnet\Money\TaxedMoney(500, 'EUR', 20), 'value' => new \Bnet\Money\TaxedMoney(500, 'EUR', 20),
]) ])
] ]
......
<?php <?php
use Bnet\Cart\Condition;
/** /**
* User: thorsten * User: thorsten
* Date: 22.07.16 * Date: 22.07.16
...@@ -70,37 +72,37 @@ class CurrencyItemTest extends PHPUnit_Framework_TestCase { ...@@ -70,37 +72,37 @@ class CurrencyItemTest extends PHPUnit_Framework_TestCase {
$condition_percent_plus = new \Bnet\Cart\CurrencyCondition([ $condition_percent_plus = new \Bnet\Cart\CurrencyCondition([
'name' => 'Percent Plus', 'name' => 'Percent Plus',
'type' => 'misc2', 'type' => 'misc2',
'target' => 'item', 'target' => Condition::TARGET_ITEM,
'value' => '+10%', 'value' => '+10%',
]); ]);
$condition_percent_minus = new \Bnet\Cart\CurrencyCondition([ $condition_percent_minus = new \Bnet\Cart\CurrencyCondition([
'name' => 'Percent Minus', 'name' => 'Percent Minus',
'type' => 'misc2', 'type' => 'misc2',
'target' => 'item', 'target' => Condition::TARGET_ITEM,
'value' => '-10%', 'value' => '-10%',
]); ]);
$condition_with_tax_plus = new \Bnet\Cart\CurrencyCondition([ $condition_with_tax_plus = new \Bnet\Cart\CurrencyCondition([
'name' => 'Absolute Tax Plus', 'name' => 'Absolute Tax Plus',
'type' => 'misc2', 'type' => 'misc2',
'target' => 'item', 'target' => Condition::TARGET_ITEM,
'value' => new \Bnet\Money\TaxedMoney(500, 'EUR', 20), 'value' => new \Bnet\Money\TaxedMoney(500, 'EUR', 20),
]); ]);
$condition_with_tax_minus = new \Bnet\Cart\CurrencyCondition([ $condition_with_tax_minus = new \Bnet\Cart\CurrencyCondition([
'name' => 'Absolute Tax Minus', 'name' => 'Absolute Tax Minus',
'type' => 'misc2', 'type' => 'misc2',
'target' => 'item', 'target' => Condition::TARGET_ITEM,
'value' => new \Bnet\Money\TaxedMoney(-500, 'EUR', 20), 'value' => new \Bnet\Money\TaxedMoney(-500, 'EUR', 20),
]); ]);
$condition_no_tax_plus = new \Bnet\Cart\CurrencyCondition([ $condition_no_tax_plus = new \Bnet\Cart\CurrencyCondition([
'name' => 'Absolute Plus', 'name' => 'Absolute Plus',
'type' => 'misc2', 'type' => 'misc2',
'target' => 'item', 'target' => Condition::TARGET_ITEM,
'value' => new \Bnet\Money\Money(500, 'EUR'), 'value' => new \Bnet\Money\Money(500, 'EUR'),
]); ]);
$condition_no_tax_minus = new \Bnet\Cart\CurrencyCondition([ $condition_no_tax_minus = new \Bnet\Cart\CurrencyCondition([
'name' => 'Absolute Minus', 'name' => 'Absolute Minus',
'type' => 'misc2', 'type' => 'misc2',
'target' => 'item', 'target' => Condition::TARGET_ITEM,
'value' => new \Bnet\Money\Money(-500, 'EUR'), 'value' => new \Bnet\Money\Money(-500, 'EUR'),
]); ]);
......
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