public function __construct(array $args) { if ($this->validate($args)) { $this->id = $args['id']; if (isset($args['name'])) { $this->name = $args['name']; } $this->product = $args['product']; if (isset($args['price'])) { $this->price = Helpers::normalizePrice($args['price']); } if (isset($args['points'])) { $this->points = $args['points']; } $this->quantity = $args['quantity']; if (isset($args['attributes'])) { $this->attributes = new ItemAttributeCollection($args['attributes']); } $this->conditions = new ConditionCollection('item-conditions'); if (isset($args['conditions'])) { foreach ($args['conditions'] as $condition) { $this->conditions->addCondition($condition); } } } }
/** * apply condition * * @param $totalOrSubTotalOrPrice * @param $conditionValue * @return float */ protected function apply($totalOrSubTotalOrPrice, $conditionValue) { // if value has a percentage sign on it, we will get first // its percentage then we will evaluate again if the value // 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)); $this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100); $result = floatval($totalOrSubTotalOrPrice - $this->parsedRawValue); } else { if ($this->valueIsToBeAdded($conditionValue)) { $value = Helpers::normalizePrice($this->cleanValue($conditionValue)); $this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100); $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); } else { $value = Helpers::normalizePrice($conditionValue); $this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100); $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); } } } 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)); $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); } else { $this->parsedRawValue = Helpers::normalizePrice($conditionValue); $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); } } } return $result; }