Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes Call to a member function applyCondition() on array #370

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
],
"require": {
"php": ">=7.3",
"illuminate/support": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0",
"illuminate/validation": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0",
"illuminate/translation": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0"
"illuminate/support": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
"illuminate/validation": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0",
"illuminate/translation": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*|5.5.*|5.6.*|5.7.*|5.8.*|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0"
},
"require-dev": {
"mockery/mockery": "^1.4.2",
"phpunit/phpunit": "^9.0",
"phpunit/phpunit": "^10.0",
"symfony/var-dumper": "5.1.5.*@dev"
},
"autoload": {
Expand Down
6 changes: 3 additions & 3 deletions src/Darryldecode/Cart/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Cart

/**
* This holds the currently added item id in cart for association
*
*
* @var
*/
protected $currentItemId;
Expand Down Expand Up @@ -297,9 +297,9 @@ public function addItemCondition($productId, $itemCondition)
$itemConditionTempHolder = $product['conditions'];

if (is_array($itemConditionTempHolder)) {
array_push($itemConditionTempHolder, $itemCondition);
array_push($itemConditionTempHolder, $itemCondition->toArray());
} else {
$itemConditionTempHolder = $itemCondition;
$itemConditionTempHolder = $itemCondition->toArray();
}

$this->update($productId, array(
Expand Down
78 changes: 41 additions & 37 deletions src/Darryldecode/Cart/CartCondition.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?php namespace Darryldecode\Cart;

use Darryldecode\Cart\Exceptions\InvalidConditionException;
use Darryldecode\Cart\Helpers\Helpers;
use Darryldecode\Cart\Validators\CartConditionValidator;
use Illuminate\Contracts\Support\Arrayable;

/**
* Created by PhpStorm.
* User: darryl
* Date: 1/15/2015
* Time: 9:02 PM
*/

class CartCondition {
class CartCondition implements Arrayable
{

/**
* @var array
Expand All @@ -25,19 +27,16 @@ class CartCondition {
public $parsedRawValue;

/**
* @param array $args (name, type, target, value)
* @param array $args (name, type, target, value)
* @throws InvalidConditionException
*/
public function __construct(array $args)
{
$this->args = $args;

if( Helpers::isMultiArray($args) )
{
Throw new InvalidConditionException('Multi dimensional array is not supported.');
}
else
{
if (Helpers::isMultiArray($args)) {
throw new InvalidConditionException('Multi dimensional array is not supported.');
} else {
$this->validate($this->args);
}
}
Expand Down Expand Up @@ -96,7 +95,7 @@ public function getValue()
/**
* Set the order to apply this condition. If no argument order is applied we return 0 as
* indicator that no assignment has been made
* @param int $order
* @param int $order
* @return Integer
*/
public function setOrder($order = 1)
Expand Down Expand Up @@ -134,6 +133,7 @@ public function applyCondition($totalOrSubTotalOrPrice)
*/
public function getCalculatedValue($totalOrSubTotalOrPrice)
{
dd($this->getValue());
$this->apply($totalOrSubTotalOrPrice, $this->getValue());

return $this->parsedRawValue;
Expand All @@ -153,26 +153,20 @@ protected function apply($totalOrSubTotalOrPrice, $conditionValue)
// has a minus or plus sign so we can decide what to do with the
// percentage, whether to add or subtract it to the total/subtotal/price
// if we can't find any plus/minus sign, we will assume it as plus sign
if( $this->valueIsPercentage($conditionValue) )
{
if( $this->valueIsToBeSubtracted($conditionValue) )
{
$value = Helpers::normalizePrice( $this->cleanValue($conditionValue) );
if ($this->valueIsPercentage($conditionValue)) {
if ($this->valueIsToBeSubtracted($conditionValue)) {
$value = Helpers::normalizePrice($this->cleanValue($conditionValue));

$this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100);

$result = floatval($totalOrSubTotalOrPrice - $this->parsedRawValue);
}
else if ( $this->valueIsToBeAdded($conditionValue) )
{
$value = Helpers::normalizePrice( $this->cleanValue($conditionValue) );
} elseif ($this->valueIsToBeAdded($conditionValue)) {
$value = Helpers::normalizePrice($this->cleanValue($conditionValue));

$this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100);

$result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue);
}
else
{
} else {
$value = Helpers::normalizePrice($conditionValue);

$this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100);
Expand All @@ -183,22 +177,16 @@ protected function apply($totalOrSubTotalOrPrice, $conditionValue)

// if the value has no percent sign on it, the operation will not be a percentage
// next is we will check if it has a minus/plus sign so then we can just deduct it to total/subtotal/price
else
{
if( $this->valueIsToBeSubtracted($conditionValue) )
{
$this->parsedRawValue = Helpers::normalizePrice( $this->cleanValue($conditionValue) );
else {
if ($this->valueIsToBeSubtracted($conditionValue)) {
$this->parsedRawValue = Helpers::normalizePrice($this->cleanValue($conditionValue));

$result = floatval($totalOrSubTotalOrPrice - $this->parsedRawValue);
}
else if ( $this->valueIsToBeAdded($conditionValue) )
{
$this->parsedRawValue = Helpers::normalizePrice( $this->cleanValue($conditionValue) );
} elseif ($this->valueIsToBeAdded($conditionValue)) {
$this->parsedRawValue = Helpers::normalizePrice($this->cleanValue($conditionValue));

$result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue);
}
else
{
} else {
$this->parsedRawValue = Helpers::normalizePrice($conditionValue);

$result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue);
Expand Down Expand Up @@ -250,7 +238,7 @@ protected function valueIsToBeAdded($value)
*/
protected function cleanValue($value)
{
return str_replace(array('%','-','+'),'',$value);
return str_replace(array('%', '-', '+'), '', $value);
}

/**
Expand All @@ -269,9 +257,25 @@ protected function validate($args)

$validator = CartConditionValidator::make($args, $rules);

if( $validator->fails() )
{
if ($validator->fails()) {
throw new InvalidConditionException($validator->messages()->first());
}
}

public function toArray(): array
{
return collect([
'name' => $this->getName(),
'type' => $this->getType(),
'target' => $this->getTarget(),
'value' => $this->getValue(),
'attributes' => $this->getAttributes(),
'order' => $this->getOrder(),
])->toArray();
}

public static function fromArray(array $condition): static
{
return new static($condition);
}
}
3 changes: 2 additions & 1 deletion src/Darryldecode/Cart/ItemCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ public function getPriceWithConditions($formatted = true)
if ($this->hasConditions()) {
if (is_array($this->conditions)) {
foreach ($this->conditions as $condition) {
$conditionObj = CartCondition::fromArray($condition);
($processed > 0) ? $toBeCalculated = $newPrice : $toBeCalculated = $originalPrice;
$newPrice = $condition->applyCondition($toBeCalculated);
$newPrice = $conditionObj->applyCondition($toBeCalculated);
$processed++;
}
} else {
Expand Down