Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
L
laravelshoppingcart
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
package
laravelshoppingcart
Commits
00167799
Commit
00167799
authored
Mar 28, 2019
by
Thomas Bilk
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
put condition targets into constants
parent
e87ffd6e
Changes
10
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
180 additions
and
129 deletions
+180
-129
Cart.php
src/Cart/Cart.php
+4
-4
Condition.php
src/Cart/Condition.php
+4
-0
CurrencyCart.php
src/Cart/CurrencyCart.php
+8
-7
CurrencyItem.php
src/Cart/CurrencyItem.php
+2
-2
Item.php
src/Cart/Item.php
+4
-4
CartConditionsTest.php
tests/CartConditionsTest.php
+49
-49
CartTest.php
tests/CartTest.php
+4
-4
CurrencyCartConditionsTest.php
tests/CurrencyCartConditionsTest.php
+95
-52
CurrencyCartTest.php
tests/CurrencyCartTest.php
+2
-1
CurrencyItemTest.php
tests/CurrencyItemTest.php
+8
-6
No files found.
src/Cart/Cart.php
View file @
00167799
...
@@ -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
;
});
});
...
...
src/Cart/Condition.php
View file @
00167799
...
@@ -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
...
...
src/Cart/CurrencyCart.php
View file @
00167799
...
@@ -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
$
mone
y
->
add
(
$price
);
return
$
carr
y
->
add
(
$price
);
});
}
,
null
);
return
$condTotal
->
add
(
$itemConditionTotal
);
return
$condTotal
->
add
(
$itemConditionTotal
);
}
}
...
...
src/Cart/CurrencyItem.php
View file @
00167799
...
@@ -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
;
...
...
src/Cart/Item.php
View file @
00167799
...
@@ -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
;
})
})
...
...
tests/CartConditionsTest.php
View file @
00167799
This diff is collapsed.
Click to expand it.
tests/CartTest.php
View file @
00167799
...
@@ -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%'
,
]
]
]
]
...
...
tests/CurrencyCartConditionsTest.php
View file @
00167799
This diff is collapsed.
Click to expand it.
tests/CurrencyCartTest.php
View file @
00167799
...
@@ -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
),
])
])
]
]
...
...
tests/CurrencyItemTest.php
View file @
00167799
<?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'
),
]);
]);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment