WIP
- Refactor Coupon : is Customer matching Coupon rules
This commit is contained in:
@@ -25,9 +25,7 @@ namespace Thelia\Constraint;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
||||
use Symfony\Component\Serializer\Encoder\XmlEncoder;
|
||||
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
use Thelia\Constraint\Rule\AvailableForTotalAmountManager;
|
||||
use Thelia\Constraint\Rule\CouponRuleInterface;
|
||||
use Thelia\Constraint\Rule\SerializableRule;
|
||||
use Thelia\Coupon\CouponAdapterInterface;
|
||||
@@ -45,7 +43,7 @@ use Thelia\Coupon\CouponRuleCollection;
|
||||
* @author Guillaume MOREL <gmorel@openstudio.fr>
|
||||
*
|
||||
*/
|
||||
class ConstraintManager
|
||||
class ConstraintFactory
|
||||
{
|
||||
/** @var ContainerInterface Service Container */
|
||||
protected $container = null;
|
||||
@@ -67,28 +65,6 @@ class ConstraintManager
|
||||
$this->adapter = $container->get('thelia.adapter');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current Coupon is matching its conditions (Rules)
|
||||
* Thelia variables are given by the CouponAdapterInterface
|
||||
*
|
||||
* @param CouponRuleCollection $collection A collection of rules
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isMatching(CouponRuleCollection $collection)
|
||||
{
|
||||
$isMatching = true;
|
||||
|
||||
/** @var CouponRuleInterface $rule */
|
||||
foreach ($collection->getRules() as $rule) {
|
||||
if (!$rule->isMatching($this->adapter)) {
|
||||
$isMatching = false;
|
||||
}
|
||||
}
|
||||
|
||||
return $isMatching;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize a collection of rules
|
||||
*
|
||||
@@ -128,8 +104,8 @@ class ConstraintManager
|
||||
foreach ($unserializedRules as $rule) {
|
||||
if ($this->container->has($rule->ruleServiceId)) {
|
||||
/** @var CouponRuleInterface $couponRule */
|
||||
$couponRule = $this->container->get($rule->ruleServiceId);
|
||||
$couponRule->populateFromForm(
|
||||
$couponRule = $this->build(
|
||||
$rule->ruleServiceId,
|
||||
(array) $rule->operators,
|
||||
(array) $rule->values
|
||||
);
|
||||
@@ -140,4 +116,28 @@ class ConstraintManager
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Build a Coupon Rule from form
|
||||
*
|
||||
* @param string $ruleServiceId Rule class name
|
||||
* @param array $operators Rule Operator (<, >, = )
|
||||
* @param array $values Values setting this Rule
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* @return CouponRuleInterface Ready to use Rule or false
|
||||
*/
|
||||
public function build($ruleServiceId, array $operators, array $values)
|
||||
{
|
||||
if (!$this->container->has($ruleServiceId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var CouponRuleInterface $rule */
|
||||
$rule = $this->container->get($ruleServiceId);
|
||||
$rule->setValidatorsFromForm($operators, $values);
|
||||
|
||||
return $rule;
|
||||
}
|
||||
}
|
||||
133
core/lib/Thelia/Constraint/ConstraintValidator.php
Normal file
133
core/lib/Thelia/Constraint/ConstraintValidator.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Constraint;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
||||
use Thelia\Constraint\Rule\AvailableForTotalAmountManager;
|
||||
use Thelia\Constraint\Rule\CouponRuleInterface;
|
||||
use Thelia\Constraint\Rule\Operators;
|
||||
use Thelia\Coupon\CouponRuleCollection;
|
||||
|
||||
|
||||
/**
|
||||
* Created by JetBrains PhpStorm.
|
||||
* Date: 8/19/13
|
||||
* Time: 3:24 PM
|
||||
*
|
||||
* Validate Constraints
|
||||
*
|
||||
* @package Constraint
|
||||
* @author Guillaume MOREL <gmorel@openstudio.fr>
|
||||
*
|
||||
*/
|
||||
class ConstraintValidator
|
||||
{
|
||||
|
||||
/**
|
||||
* Check if a Customer meets SerializableRule
|
||||
*
|
||||
* @param CouponRuleCollection $rules Rules to check against the Customer
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function test(CouponRuleCollection $rules)
|
||||
{
|
||||
$isMatching = true;
|
||||
/** @var CouponRuleInterface $rule */
|
||||
foreach ($rules->getRules() as $rule) {
|
||||
if (!$rule->isMatching()) {
|
||||
$isMatching = false;
|
||||
}
|
||||
}
|
||||
|
||||
return $isMatching;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Do variable comparison
|
||||
*
|
||||
* @param mixed $v1 Variable 1
|
||||
* @param string $o Operator
|
||||
*
|
||||
* @param mixed $v2 Variable 2
|
||||
* @throws \Exception
|
||||
* @return bool
|
||||
*/
|
||||
public function variableOpComparison($v1, $o, $v2) {
|
||||
if ($o == Operators::DIFFERENT) {
|
||||
return ($v1 != $v2);
|
||||
} // could put this elsewhere...
|
||||
// $operators = str_split($o);
|
||||
// foreach($o as $operator) {
|
||||
switch ($o) { // return will exit switch, foreach loop, function
|
||||
case Operators::SUPERIOR : // >
|
||||
if ($v1 > $v2) {
|
||||
return true;
|
||||
} else {
|
||||
continue;
|
||||
} break;
|
||||
case Operators::SUPERIOR_OR_EQUAL : // >=
|
||||
if ($v1 >= $v2) {
|
||||
return true;
|
||||
} else {
|
||||
continue;
|
||||
} break;
|
||||
case Operators::INFERIOR : // <
|
||||
if ($v1 < $v2) {
|
||||
return true;
|
||||
} else {
|
||||
continue;
|
||||
} break;
|
||||
case Operators::INFERIOR_OR_EQUAL : // <=
|
||||
if ($v1 <= $v2) {
|
||||
return true;
|
||||
} else {
|
||||
continue;
|
||||
} break;
|
||||
case Operators::EQUAL : // ==
|
||||
if ($v1 == $v2) {
|
||||
return true;
|
||||
} else {
|
||||
continue;
|
||||
} break;
|
||||
case Operators::IN:
|
||||
if (in_array($v1, $v2)) { // in
|
||||
return true;
|
||||
} else {
|
||||
continue;
|
||||
} break;
|
||||
case Operators::OUT:
|
||||
if (!in_array($v1, $v2)) { // not in
|
||||
return true;
|
||||
} else {
|
||||
continue;
|
||||
} break;
|
||||
default: throw new \Exception('Unrecognized operator ' . $o);
|
||||
}
|
||||
// }
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -25,12 +25,14 @@ namespace Thelia\Constraint\Rule;
|
||||
|
||||
use Symfony\Component\Intl\Exception\NotImplementedException;
|
||||
use Symfony\Component\Translation\Translator;
|
||||
use Thelia\Constraint\ConstraintValidator;
|
||||
use Thelia\Coupon\CouponAdapterInterface;
|
||||
use Thelia\Constraint\Validator\PriceParam;
|
||||
use Thelia\Constraint\Validator\RuleValidator;
|
||||
use Thelia\Exception\InvalidRuleException;
|
||||
use Thelia\Exception\InvalidRuleOperatorException;
|
||||
use Thelia\Exception\InvalidRuleValueException;
|
||||
use Thelia\Type\FloatType;
|
||||
|
||||
/**
|
||||
* Created by JetBrains PhpStorm.
|
||||
@@ -44,26 +46,33 @@ use Thelia\Exception\InvalidRuleValueException;
|
||||
* @author Guillaume MOREL <gmorel@openstudio.fr>
|
||||
*
|
||||
*/
|
||||
class AvailableForTotalAmount extends CouponRuleAbstract
|
||||
class AvailableForTotalAmountManager extends CouponRuleAbstract
|
||||
{
|
||||
/** Rule 1st parameter : price */
|
||||
CONST PARAM1_PRICE = 'price';
|
||||
CONST INPUT1 = 'price';
|
||||
|
||||
/** Rule 1st parameter : currency */
|
||||
CONST PARAM1_CURRENCY = 'currency';
|
||||
CONST INPUT2 = 'currency';
|
||||
|
||||
/** @var string Service Id from Resources/config.xml */
|
||||
protected $serviceId = 'thelia.constraint.rule.available_for_total_amount';
|
||||
|
||||
/** @var array Available Operators (Operators::CONST) */
|
||||
protected $availableOperators = array(
|
||||
Operators::INFERIOR,
|
||||
Operators::EQUAL,
|
||||
Operators::SUPERIOR,
|
||||
self::INPUT1 => array(
|
||||
Operators::INFERIOR,
|
||||
Operators::INFERIOR_OR_EQUAL,
|
||||
Operators::EQUAL,
|
||||
Operators::SUPERIOR_OR_EQUAL,
|
||||
Operators::SUPERIOR
|
||||
),
|
||||
self::INPUT2 => array(
|
||||
Operators::EQUAL,
|
||||
)
|
||||
);
|
||||
|
||||
/** @var RuleValidator Price Validator */
|
||||
protected $priceValidator = null;
|
||||
// /** @var RuleValidator Price Validator */
|
||||
// protected $priceValidator = null;
|
||||
|
||||
/**
|
||||
* Check if backoffice inputs are relevant or not
|
||||
@@ -96,34 +105,146 @@ class AvailableForTotalAmount extends CouponRuleAbstract
|
||||
return $this->isPriceValid($price->getPrice(), $price->getCurrency());
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Check if Checkout inputs are relevant or not
|
||||
// *
|
||||
// * @throws InvalidRuleValueException if Value is not allowed
|
||||
// * @return bool
|
||||
// */
|
||||
// public function checkCheckoutInput()
|
||||
// {
|
||||
// $currency = $this->adapter->getCheckoutCurrency();
|
||||
// if (empty($currency)) {
|
||||
// throw new InvalidRuleValueException(
|
||||
// get_class(), self::PARAM1_CURRENCY
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// $price = $this->adapter->getCartTotalPrice();
|
||||
// if (empty($price)) {
|
||||
// throw new InvalidRuleValueException(
|
||||
// get_class(), self::PARAM1_PRICE
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// $this->paramsToValidate = array(
|
||||
// self::PARAM1_PRICE => $this->adapter->getCartTotalPrice(),
|
||||
// self::PARAM1_CURRENCY => $this->adapter->getCheckoutCurrency()
|
||||
// );
|
||||
//
|
||||
// return $this->isPriceValid($price, $currency);
|
||||
// }
|
||||
|
||||
/**
|
||||
* Check if Checkout inputs are relevant or not
|
||||
* Check validators relevancy and store them
|
||||
*
|
||||
* @throws InvalidRuleValueException if Value is not allowed
|
||||
* @return bool
|
||||
* @param array $operators Operators the Admin set in BackOffice
|
||||
* @param array $values Values the Admin set in BackOffice
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* @return $this
|
||||
*/
|
||||
public function checkCheckoutInput()
|
||||
public function setValidatorsFromForm(array $operators, array $values)
|
||||
{
|
||||
$currency = $this->adapter->getCheckoutCurrency();
|
||||
if (empty($currency)) {
|
||||
throw new InvalidRuleValueException(
|
||||
get_class(), self::PARAM1_CURRENCY
|
||||
);
|
||||
}
|
||||
|
||||
$price = $this->adapter->getCartTotalPrice();
|
||||
if (empty($price)) {
|
||||
throw new InvalidRuleValueException(
|
||||
get_class(), self::PARAM1_PRICE
|
||||
);
|
||||
}
|
||||
|
||||
$this->paramsToValidate = array(
|
||||
self::PARAM1_PRICE => $this->adapter->getCartTotalPrice(),
|
||||
self::PARAM1_CURRENCY => $this->adapter->getCheckoutCurrency()
|
||||
$this->setValidators(
|
||||
$operators[self::INPUT1],
|
||||
$values[self::INPUT1],
|
||||
$operators[self::INPUT2],
|
||||
$values[self::INPUT2]
|
||||
);
|
||||
|
||||
return $this->isPriceValid($price, $currency);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check validators relevancy and store them
|
||||
*
|
||||
* @param string $priceOperator Price Operator ex <
|
||||
* @param float $priceValue Price set to meet condition
|
||||
* @param string $currencyOperator Currency Operator ex =
|
||||
* @param string $currencyValue Currency set to meet condition
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* @return $this
|
||||
*/
|
||||
protected function setValidators($priceOperator, $priceValue, $currencyOperator, $currencyValue)
|
||||
{
|
||||
$isOperator1Legit = $this->isOperatorLegit(
|
||||
$priceOperator,
|
||||
$this->availableOperators[self::INPUT1]
|
||||
);
|
||||
if (!$isOperator1Legit) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Operator for price field is not legit'
|
||||
);
|
||||
}
|
||||
|
||||
$isOperator1Legit = $this->isOperatorLegit(
|
||||
$currencyOperator,
|
||||
$this->availableOperators[self::INPUT2]
|
||||
);
|
||||
if (!$isOperator1Legit) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Operator for currency field is not legit'
|
||||
);
|
||||
}
|
||||
|
||||
$floatType = new FloatType();
|
||||
if (!$floatType->isValid($priceValue) || $priceValue <= 0) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Value for price field is not legit'
|
||||
);
|
||||
}
|
||||
|
||||
// @todo check currency is legit or not
|
||||
|
||||
$this->operators = array(
|
||||
self::INPUT1 => $priceOperator,
|
||||
self::INPUT2 => $currencyOperator,
|
||||
);
|
||||
$this->values = array(
|
||||
self::INPUT1 => $priceValue,
|
||||
self::INPUT2 => $currencyValue,
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if Customer meets conditions
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isMatching()
|
||||
{
|
||||
$isOperator1Legit = $this->isOperatorLegit(
|
||||
$this->operators[self::INPUT1],
|
||||
$this->availableOperators[self::INPUT1]
|
||||
);
|
||||
$isOperator2Legit = $this->isOperatorLegit(
|
||||
$this->operators[self::INPUT2],
|
||||
$this->availableOperators[self::INPUT2]
|
||||
);
|
||||
|
||||
if (!$isOperator1Legit || !$isOperator2Legit) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$constrainValidator = new ConstraintValidator();
|
||||
$constraint1 =$constrainValidator->variableOpComparison(
|
||||
$this->adapter->getCartTotalPrice(),
|
||||
$this->operators[self::INPUT1],
|
||||
$this->values[self::INPUT1]
|
||||
);
|
||||
$constraint2 =$constrainValidator->variableOpComparison(
|
||||
$this->adapter->getCheckoutCurrency(),
|
||||
$this->operators[self::INPUT2],
|
||||
$this->values[self::INPUT2]
|
||||
);
|
||||
if ($constraint1 && $constraint2) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -154,20 +275,20 @@ class AvailableForTotalAmount extends CouponRuleAbstract
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate current Rule param to be validated from adapter
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
protected function setParametersToValidate()
|
||||
{
|
||||
$this->paramsToValidate = array(
|
||||
self::PARAM1_PRICE => $this->adapter->getCartTotalPrice(),
|
||||
self::PARAM1_CURRENCY => $this->adapter->getCheckoutCurrency()
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
// /**
|
||||
// * Generate current Rule param to be validated from adapter
|
||||
// *
|
||||
// * @return $this
|
||||
// */
|
||||
// protected function setParametersToValidate()
|
||||
// {
|
||||
// $this->paramsToValidate = array(
|
||||
// self::PARAM1_PRICE => $this->adapter->getCartTotalPrice(),
|
||||
// self::PARAM1_CURRENCY => $this->adapter->getCheckoutCurrency()
|
||||
// );
|
||||
//
|
||||
// return $this;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Get I18n name
|
||||
@@ -207,38 +328,38 @@ class AvailableForTotalAmount extends CouponRuleAbstract
|
||||
return $toolTip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate a Rule from a form admin
|
||||
*
|
||||
* @param array $operators Rule Operator set by the Admin
|
||||
* @param array $values Rule Values set by the Admin
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* @return $this
|
||||
*/
|
||||
public function populateFromForm(array $operators, array $values)
|
||||
{
|
||||
if ($values[self::PARAM1_PRICE] === null
|
||||
|| $values[self::PARAM1_CURRENCY] === null
|
||||
) {
|
||||
throw new \InvalidArgumentException(
|
||||
'The Rule ' . get_class() . 'needs at least a quantity set (' . self::PARAM1_PRICE . ', ' . self::PARAM1_CURRENCY . ')'
|
||||
);
|
||||
}
|
||||
|
||||
$this->priceValidator = new RuleValidator(
|
||||
$operators[self::PARAM1_PRICE],
|
||||
new PriceParam(
|
||||
$this->translator,
|
||||
$values[self::PARAM1_PRICE],
|
||||
$values[self::PARAM1_CURRENCY]
|
||||
)
|
||||
);
|
||||
|
||||
$this->validators = array(self::PARAM1_PRICE => $this->priceValidator);
|
||||
|
||||
return $this;
|
||||
}
|
||||
// /**
|
||||
// * Populate a Rule from a form admin
|
||||
// *
|
||||
// * @param array $operators Rule Operator set by the Admin
|
||||
// * @param array $values Rule Values set by the Admin
|
||||
// *
|
||||
// * @throws \InvalidArgumentException
|
||||
// * @return $this
|
||||
// */
|
||||
// public function populateFromForm(array $operators, array $values)
|
||||
// {
|
||||
// if ($values[self::PARAM1_PRICE] === null
|
||||
// || $values[self::PARAM1_CURRENCY] === null
|
||||
// ) {
|
||||
// throw new \InvalidArgumentException(
|
||||
// 'The Rule ' . get_class() . 'needs at least a quantity set (' . self::PARAM1_PRICE . ', ' . self::PARAM1_CURRENCY . ')'
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// $this->priceValidator = new RuleValidator(
|
||||
// $operators[self::PARAM1_PRICE],
|
||||
// new PriceParam(
|
||||
// $this->translator,
|
||||
// $values[self::PARAM1_PRICE],
|
||||
// $values[self::PARAM1_CURRENCY]
|
||||
// )
|
||||
// );
|
||||
//
|
||||
// $this->validators = array(self::PARAM1_PRICE => $this->priceValidator);
|
||||
//
|
||||
// return $this;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Return a serializable Rule
|
||||
@@ -249,14 +370,9 @@ class AvailableForTotalAmount extends CouponRuleAbstract
|
||||
{
|
||||
$serializableRule = new SerializableRule();
|
||||
$serializableRule->ruleServiceId = $this->serviceId;
|
||||
$serializableRule->operators = array(
|
||||
self::PARAM1_PRICE => $this->priceValidator->getOperator()
|
||||
);
|
||||
$serializableRule->operators = $this->operators;
|
||||
|
||||
$serializableRule->values = array(
|
||||
self::PARAM1_PRICE => $this->priceValidator->getParam()->getPrice(),
|
||||
self::PARAM1_CURRENCY => $this->priceValidator->getParam()->getCurrency()
|
||||
);
|
||||
$serializableRule->values = $this->values;
|
||||
|
||||
return $serializableRule;
|
||||
}
|
||||
@@ -25,10 +25,13 @@ namespace Thelia\Constraint\Rule;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Symfony\Component\Translation\Translator;
|
||||
use Thelia\Constraint\ConstraintValidator;
|
||||
use Thelia\Constraint\Validator\QuantityParam;
|
||||
use Thelia\Constraint\Validator\RuleValidator;
|
||||
use Thelia\Coupon\CouponAdapterInterface;
|
||||
use Thelia\Exception\InvalidRuleException;
|
||||
use Thelia\Exception\InvalidRuleValueException;
|
||||
use Thelia\Type\FloatType;
|
||||
|
||||
/**
|
||||
* Created by JetBrains PhpStorm.
|
||||
@@ -41,19 +44,23 @@ use Thelia\Exception\InvalidRuleValueException;
|
||||
* @author Guillaume MOREL <gmorel@openstudio.fr>
|
||||
*
|
||||
*/
|
||||
class AvailableForXArticles extends CouponRuleAbstract
|
||||
class AvailableForXArticlesManager extends CouponRuleAbstract
|
||||
{
|
||||
/** Rule 1st parameter : quantity */
|
||||
CONST PARAM1_QUANTITY = 'quantity';
|
||||
CONST INPUT1 = 'quantity';
|
||||
|
||||
/** @var string Service Id from Resources/config.xml */
|
||||
protected $serviceId = 'thelia.constraint.rule.available_for_x_articles';
|
||||
|
||||
/** @var array Available Operators (Operators::CONST) */
|
||||
protected $availableOperators = array(
|
||||
Operators::INFERIOR,
|
||||
Operators::EQUAL,
|
||||
Operators::SUPERIOR,
|
||||
self::INPUT1 => array(
|
||||
Operators::INFERIOR,
|
||||
Operators::INFERIOR_OR_EQUAL,
|
||||
Operators::EQUAL,
|
||||
Operators::SUPERIOR_OR_EQUAL,
|
||||
Operators::SUPERIOR
|
||||
)
|
||||
);
|
||||
|
||||
/** @var QuantityParam Quantity Validator */
|
||||
@@ -107,24 +114,100 @@ class AvailableForXArticles extends CouponRuleAbstract
|
||||
return $this;
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Check if Checkout inputs are relevant or not
|
||||
// *
|
||||
// * @throws \Thelia\Exception\InvalidRuleValueException
|
||||
// * @return bool
|
||||
// */
|
||||
// public function checkCheckoutInput()
|
||||
// {
|
||||
// if (!isset($this->paramsToValidate)
|
||||
// || empty($this->paramsToValidate)
|
||||
// ||!isset($this->paramsToValidate[self::PARAM1_QUANTITY])
|
||||
// ) {
|
||||
// throw new InvalidRuleValueException(get_class(), self::PARAM1_QUANTITY);
|
||||
// }
|
||||
//
|
||||
// $price = $this->paramsToValidate[self::PARAM1_QUANTITY];
|
||||
//
|
||||
// return $this->isQuantityValid($price);
|
||||
// }
|
||||
|
||||
/**
|
||||
* Check if Checkout inputs are relevant or not
|
||||
* Check validators relevancy and store them
|
||||
*
|
||||
* @throws \Thelia\Exception\InvalidRuleValueException
|
||||
* @return bool
|
||||
* @param array $operators Operators the Admin set in BackOffice
|
||||
* @param array $values Values the Admin set in BackOffice
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* @return $this
|
||||
*/
|
||||
public function checkCheckoutInput()
|
||||
public function setValidatorsFromForm(array $operators, array $values)
|
||||
{
|
||||
if (!isset($this->paramsToValidate)
|
||||
|| empty($this->paramsToValidate)
|
||||
||!isset($this->paramsToValidate[self::PARAM1_QUANTITY])
|
||||
) {
|
||||
throw new InvalidRuleValueException(get_class(), self::PARAM1_QUANTITY);
|
||||
$this->setValidators(
|
||||
$operators[self::INPUT1],
|
||||
$values[self::INPUT1]
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check validators relevancy and store them
|
||||
*
|
||||
* @param string $quantityOperator Quantity Operator ex <
|
||||
* @param int $quantityValue Quantity set to meet condition
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* @return $this
|
||||
*/
|
||||
protected function setValidators($quantityOperator, $quantityValue)
|
||||
{
|
||||
$isOperator1Legit = $this->isOperatorLegit(
|
||||
$quantityOperator,
|
||||
$this->availableOperators[self::INPUT1]
|
||||
);
|
||||
if (!$isOperator1Legit) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Operator for quantity field is not legit'
|
||||
);
|
||||
}
|
||||
|
||||
$price = $this->paramsToValidate[self::PARAM1_QUANTITY];
|
||||
if (!is_int($quantityValue) || $quantityValue <= 0) {
|
||||
throw new \InvalidArgumentException(
|
||||
'Value for quantity field is not legit'
|
||||
);
|
||||
}
|
||||
|
||||
return $this->isQuantityValid($price);
|
||||
$this->operators = array(
|
||||
self::INPUT1 => $quantityOperator,
|
||||
);
|
||||
$this->values = array(
|
||||
self::INPUT1 => $quantityValue,
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if Customer meets conditions
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isMatching()
|
||||
{
|
||||
$constrainValidator = new ConstraintValidator();
|
||||
$constraint1 =$constrainValidator->variableOpComparison(
|
||||
$this->adapter->getNbArticlesInCart(),
|
||||
$this->operators[self::INPUT1],
|
||||
$this->values[self::INPUT1]
|
||||
);
|
||||
|
||||
if ($constraint1) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -184,35 +267,35 @@ class AvailableForXArticles extends CouponRuleAbstract
|
||||
return $toolTip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate a Rule from a form admin
|
||||
*
|
||||
* @param array $operators Rule Operator set by the Admin
|
||||
* @param array $values Rule Values set by the Admin
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
* @return $this
|
||||
*/
|
||||
public function populateFromForm(array $operators, array $values)
|
||||
{
|
||||
if ($values[self::PARAM1_QUANTITY] === null) {
|
||||
throw new InvalidArgumentException(
|
||||
'The Rule ' . get_class() . 'needs at least a quantity set (' . self::PARAM1_QUANTITY. ')'
|
||||
);
|
||||
}
|
||||
|
||||
$this->quantityValidator = new RuleValidator(
|
||||
$operators[self::PARAM1_QUANTITY],
|
||||
new QuantityParam(
|
||||
$this->adapter,
|
||||
$values[self::PARAM1_QUANTITY]
|
||||
)
|
||||
);
|
||||
|
||||
$this->validators = array(self::PARAM1_QUANTITY => $this->quantityValidator);
|
||||
|
||||
return $this;
|
||||
}
|
||||
// /**
|
||||
// * Populate a Rule from a form admin
|
||||
// *
|
||||
// * @param array $operators Rule Operator set by the Admin
|
||||
// * @param array $values Rule Values set by the Admin
|
||||
// *
|
||||
// * @throws InvalidArgumentException
|
||||
// * @return $this
|
||||
// */
|
||||
// public function populateFromForm(array $operators, array $values)
|
||||
// {
|
||||
// if ($values[self::PARAM1_QUANTITY] === null) {
|
||||
// throw new InvalidArgumentException(
|
||||
// 'The Rule ' . get_class() . 'needs at least a quantity set (' . self::PARAM1_QUANTITY. ')'
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// $this->quantityValidator = new RuleValidator(
|
||||
// $operators[self::PARAM1_QUANTITY],
|
||||
// new QuantityParam(
|
||||
// $this->adapter,
|
||||
// $values[self::PARAM1_QUANTITY]
|
||||
// )
|
||||
// );
|
||||
//
|
||||
// $this->validators = array(self::PARAM1_QUANTITY => $this->quantityValidator);
|
||||
//
|
||||
// return $this;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Return a serializable Rule
|
||||
@@ -67,6 +67,12 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
|
||||
/** @var Translator Service Translator */
|
||||
protected $translator = null;
|
||||
|
||||
/** @var array Operators set by Admin in BackOffice */
|
||||
protected $operators = array();
|
||||
|
||||
/** @var array Values set by Admin in BackOffice */
|
||||
protected $values = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
@@ -78,59 +84,61 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
|
||||
$this->translator = $adapter->getTranslator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check validator relevancy and store them
|
||||
*
|
||||
* @param array $validators Array of RuleValidator
|
||||
* validating $paramsToValidate against
|
||||
*
|
||||
* @return $this
|
||||
* @throws InvalidRuleException
|
||||
*/
|
||||
protected function setValidators(array $validators)
|
||||
{
|
||||
foreach ($validators as $validator) {
|
||||
if (!$validator instanceof RuleValidator) {
|
||||
throw new InvalidRuleException(get_class());
|
||||
}
|
||||
if (!in_array($validator->getOperator(), $this->availableOperators)) {
|
||||
throw new InvalidRuleOperatorException(
|
||||
get_class(),
|
||||
$validator->getOperator()
|
||||
);
|
||||
}
|
||||
}
|
||||
$this->validators = $validators;
|
||||
// /**
|
||||
// * Check validator relevancy and store them
|
||||
// *
|
||||
// * @param array $validators Array of RuleValidator
|
||||
// * validating $paramsToValidate against
|
||||
// *
|
||||
// * @return $this
|
||||
// * @throws InvalidRuleException
|
||||
// */
|
||||
// protected function setValidators(array $validators)
|
||||
// {
|
||||
// foreach ($validators as $validator) {
|
||||
// if (!$validator instanceof RuleValidator) {
|
||||
// throw new InvalidRuleException(get_class());
|
||||
// }
|
||||
// if (!in_array($validator->getOperator(), $this->availableOperators)) {
|
||||
// throw new InvalidRuleOperatorException(
|
||||
// get_class(),
|
||||
// $validator->getOperator()
|
||||
// );
|
||||
// }
|
||||
// }
|
||||
// $this->validators = $validators;
|
||||
//
|
||||
// return $this;
|
||||
// }
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current Checkout matches this condition
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isMatching()
|
||||
{
|
||||
$this->checkBackOfficeInput();
|
||||
$this->checkCheckoutInput();
|
||||
|
||||
$isMatching = true;
|
||||
/** @var $validator RuleValidator*/
|
||||
foreach ($this->validators as $param => $validator) {
|
||||
$a = $this->paramsToValidate[$param];
|
||||
$operator = $validator->getOperator();
|
||||
/** @var ComparableInterface, RuleParameterAbstract $b */
|
||||
$b = $validator->getParam();
|
||||
|
||||
if (!Operators::isValid($a, $operator, $b)) {
|
||||
$isMatching = false;
|
||||
}
|
||||
}
|
||||
|
||||
return $isMatching;
|
||||
|
||||
}
|
||||
// /**
|
||||
// * Check if the current Checkout matches this condition
|
||||
// *
|
||||
// * @return bool
|
||||
// */
|
||||
// public function isMatching()
|
||||
// {
|
||||
// $this->checkBackOfficeInput();
|
||||
// $this->checkCheckoutInput();
|
||||
//
|
||||
// $isMatching = true;
|
||||
// /** @var $validator RuleValidator*/
|
||||
// foreach ($this->validators as $param => $validator) {
|
||||
// $a = $this->paramsToValidate[$param];
|
||||
// $operator = $validator->getOperator();
|
||||
// /** @var ComparableInterface, RuleParameterAbstract $b */
|
||||
// $b = $validator->getParam();
|
||||
//
|
||||
// if (!Operators::isValid($a, $operator, $b)) {
|
||||
// $isMatching = false;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// return $isMatching;
|
||||
//
|
||||
// }
|
||||
|
||||
/**
|
||||
* Return all available Operators for this Rule
|
||||
@@ -162,16 +170,16 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate current Rule param to be validated from adapter
|
||||
*
|
||||
* @throws \Thelia\Exception\NotImplementedException
|
||||
* @return $this
|
||||
*/
|
||||
protected function setParametersToValidate()
|
||||
{
|
||||
throw new \Thelia\Exception\NotImplementedException();
|
||||
}
|
||||
// /**
|
||||
// * Generate current Rule param to be validated from adapter
|
||||
// *
|
||||
// * @throws \Thelia\Exception\NotImplementedException
|
||||
// * @return $this
|
||||
// */
|
||||
// protected function setParametersToValidate()
|
||||
// {
|
||||
// throw new \Thelia\Exception\NotImplementedException();
|
||||
// }
|
||||
|
||||
/**
|
||||
* Return all validators
|
||||
@@ -194,6 +202,17 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
|
||||
return $this->serviceId;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Validate if Operator given is available for this Coupon
|
||||
*
|
||||
* @param string $operator Operator to validate ex <
|
||||
* @param array $availableOperators Available operators
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function isOperatorLegit($operator, array $availableOperators)
|
||||
{
|
||||
return in_array($operator, $availableOperators);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -46,6 +46,13 @@ interface CouponRuleInterface
|
||||
*/
|
||||
function __construct(CouponAdapterInterface $adapter);
|
||||
|
||||
/**
|
||||
* Get Rule Service id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getServiceId();
|
||||
|
||||
/**
|
||||
* Check if backoffice inputs are relevant or not
|
||||
*
|
||||
@@ -53,15 +60,33 @@ interface CouponRuleInterface
|
||||
*/
|
||||
public function checkBackOfficeInput();
|
||||
|
||||
/**
|
||||
* Check if Checkout inputs are relevant or not
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function checkCheckoutInput();
|
||||
// /**
|
||||
// * Check if Checkout inputs are relevant or not
|
||||
// *
|
||||
// * @return bool
|
||||
// */
|
||||
// public function checkCheckoutInput();
|
||||
|
||||
/**
|
||||
* Check if the current Checkout matches this condition
|
||||
* Check validators relevancy and store them
|
||||
*
|
||||
* @param array $operators Operators the Admin set in BackOffice
|
||||
* @param array $values Values the Admin set in BackOffice
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
* @return $this
|
||||
*/
|
||||
public function setValidatorsFromForm(array $operators, array $values);
|
||||
|
||||
// /**
|
||||
// * Check if the current Checkout matches this condition
|
||||
// *
|
||||
// * @return bool
|
||||
// */
|
||||
// public function isMatching();
|
||||
|
||||
/**
|
||||
* Test if Customer meets conditions
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
@@ -96,15 +121,15 @@ interface CouponRuleInterface
|
||||
*/
|
||||
public function getValidators();
|
||||
|
||||
/**
|
||||
* Populate a Rule from a form admin
|
||||
*
|
||||
* @param array $operators Rule Operator set by the Admin
|
||||
* @param array $values Rule Values set by the Admin
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function populateFromForm(array$operators, array $values);
|
||||
// /**
|
||||
// * Populate a Rule from a form admin
|
||||
// *
|
||||
// * @param array $operators Rule Operator set by the Admin
|
||||
// * @param array $values Rule Values set by the Admin
|
||||
// *
|
||||
// * @return bool
|
||||
// */
|
||||
// public function populateFromForm(array$operators, array $values);
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -51,62 +51,66 @@ abstract class Operators
|
||||
CONST SUPERIOR = '>';
|
||||
/** Param1 is different to Param2 */
|
||||
CONST DIFFERENT = '!=';
|
||||
/** Param1 is in Param2 */
|
||||
CONST IN = 'in';
|
||||
/** Param1 is not in Param2 */
|
||||
CONST OUT = 'out';
|
||||
|
||||
/**
|
||||
* Check if a parameter is valid against a ComparableInterface from its operator
|
||||
*
|
||||
* @param mixed $a Parameter to validate
|
||||
* @param string $operator Operator to validate against
|
||||
* @param ComparableInterface $b Comparable to validate against
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isValid($a, $operator, ComparableInterface $b)
|
||||
{
|
||||
$ret = false;
|
||||
|
||||
try {
|
||||
$comparison = $b->compareTo($a);
|
||||
} catch (\Exception $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch ($operator) {
|
||||
case self::INFERIOR:
|
||||
if ($comparison == 1) {
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case self::INFERIOR_OR_EQUAL:
|
||||
if ($comparison == 1 || $comparison == 0) {
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case self::EQUAL:
|
||||
if ($comparison == 0) {
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case self::SUPERIOR_OR_EQUAL:
|
||||
if ($comparison == -1 || $comparison == 0) {
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case self::SUPERIOR:
|
||||
if ($comparison == -1) {
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
case self::DIFFERENT:
|
||||
if ($comparison != 0) {
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
// /**
|
||||
// * Check if a parameter is valid against a ComparableInterface from its operator
|
||||
// *
|
||||
// * @param mixed $a Parameter to validate
|
||||
// * @param string $operator Operator to validate against
|
||||
// * @param ComparableInterface $b Comparable to validate against
|
||||
// *
|
||||
// * @return bool
|
||||
// */
|
||||
// public static function isValid($a, $operator, ComparableInterface $b)
|
||||
// {
|
||||
// $ret = false;
|
||||
//
|
||||
// try {
|
||||
// $comparison = $b->compareTo($a);
|
||||
// } catch (\Exception $e) {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// switch ($operator) {
|
||||
// case self::INFERIOR:
|
||||
// if ($comparison == 1) {
|
||||
// return true;
|
||||
// }
|
||||
// break;
|
||||
// case self::INFERIOR_OR_EQUAL:
|
||||
// if ($comparison == 1 || $comparison == 0) {
|
||||
// return true;
|
||||
// }
|
||||
// break;
|
||||
// case self::EQUAL:
|
||||
// if ($comparison == 0) {
|
||||
// return true;
|
||||
// }
|
||||
// break;
|
||||
// case self::SUPERIOR_OR_EQUAL:
|
||||
// if ($comparison == -1 || $comparison == 0) {
|
||||
// return true;
|
||||
// }
|
||||
// break;
|
||||
// case self::SUPERIOR:
|
||||
// if ($comparison == -1) {
|
||||
// return true;
|
||||
// }
|
||||
// break;
|
||||
// case self::DIFFERENT:
|
||||
// if ($comparison != 0) {
|
||||
// return true;
|
||||
// }
|
||||
// break;
|
||||
// default:
|
||||
// }
|
||||
//
|
||||
// return $ret;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Get operator translation
|
||||
@@ -162,6 +166,20 @@ abstract class Operators
|
||||
'constraint'
|
||||
);
|
||||
break;
|
||||
case self::IN:
|
||||
$ret = $translator->trans(
|
||||
'in',
|
||||
array(),
|
||||
'constraint'
|
||||
);
|
||||
break;
|
||||
case self::OUT:
|
||||
$ret = $translator->trans(
|
||||
'not in',
|
||||
array(),
|
||||
'constraint'
|
||||
);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user