WIP : Refactor contraint/rule becomes conditions (more generic)

This commit is contained in:
gmorel
2013-09-25 12:58:12 +02:00
parent 451f79be1a
commit cb389f126c
10 changed files with 423 additions and 313 deletions

View File

@@ -0,0 +1,144 @@
<?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\Condition;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Thelia\Condition\Operators;
use Thelia\Coupon\ConditionCollection;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Validate Conditions
*
* @package Condition
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class ConditionEvaluator
{
/**
* Check if an Event matches SerializableCondition
*
* @param ConditionCollection $conditions Conditions to check against the Event
*
* @return bool
*/
public function isMatching(ConditionCollection $conditions)
{
$isMatching = true;
/** @var ConditionManagerInterface $condition */
foreach ($conditions->getConditions() as $condition) {
if (!$condition->isMatching()) {
$isMatching = false;
}
}
return $isMatching;
}
/**
* Do variable comparison
*
* @param mixed $v1 Variable 1
* @param string $o Operator ex : Operators::DIFFERENT
* @param mixed $v2 Variable 2
*
* @throws \Exception
* @return bool
*/
public function variableOpComparison($v1, $o, $v2)
{
if ($o == Operators::DIFFERENT) {
return ($v1 != $v2);
}
switch ($o) {
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:
// in
if (in_array($v1, $v2)) {
return true;
} else {
continue;
}
break;
case Operators::OUT:
// not in
if (!in_array($v1, $v2)) {
return true;
} else {
continue;
}
break;
default:
throw new \Exception('Unrecognized operator ' . $o);
}
return false;
}
}

View File

@@ -0,0 +1,169 @@
<?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\Condition;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Thelia\Coupon\AdapterInterface;
use Thelia\Coupon\ConditionCollection;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Manage how Condition could interact with the current application state (Thelia)
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class ConditionFactory
{
/** @var ContainerInterface Service Container */
protected $container = null;
/** @var AdapterInterface Provide necessary value from Thelia */
protected $adapter;
/** @var array ConditionCollection to process*/
protected $conditions = null;
/**
* Constructor
*
* @param ContainerInterface $container Service container
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->adapter = $container->get('thelia.adapter');
}
/**
* Serialize a collection of conditions
*
* @param ConditionCollection $collection A collection of conditions
*
* @return string A ready to be stored Condition collection
*/
public function serializeCouponRuleCollection(ConditionCollection $collection)
{
if ($collection->isEmpty()) {
/** @var ConditionManagerInterface $conditionNone */
$conditionNone = $this->container->get(
'thelia.constraint.rule.available_for_everyone'
);
$collection->add($conditionNone);
}
$serializableConditions = array();
$conditions = $collection->getConditions();
if ($conditions !== null) {
/** @var $condition ConditionManagerInterface */
foreach ($conditions as $condition) {
// Remove all rule if the "no condition" condition is found
// if ($condition->getServiceId() == 'thelia.constraint.rule.available_for_everyone') {
// return base64_encode(json_encode(array($condition->getSerializableRule())));
// }
$serializableConditions[] = $condition->getSerializableCondition();
}
}
return base64_encode(json_encode($serializableConditions));
}
/**
* Unserialize a collection of conditions
*
* @param string $serializedConditions Serialized Conditions
*
* @return ConditionCollection Conditions ready to be processed
*/
public function unserializeCouponRuleCollection($serializedConditions)
{
$unserializedConditions = json_decode(base64_decode($serializedConditions));
$collection = new ConditionCollection();
if (!empty($unserializedConditions) && !empty($unserializedConditions)) {
/** @var SerializableCondition $condition */
foreach ($unserializedConditions as $condition) {
if ($this->container->has($condition->conditionServiceId)) {
/** @var ConditionManagerInterface $conditionManager */
$conditionManager = $this->build(
$condition->conditionServiceId,
(array) $condition->operators,
(array) $condition->values
);
$collection->add(clone $conditionManager);
}
}
}
return $collection;
}
/**
* Build a Condition from form
*
* @param string $conditionServiceId Condition class name
* @param array $operators Condition Operator (<, >, = )
* @param array $values Values setting this Condition
*
* @throws \InvalidArgumentException
* @return ConditionManagerInterface Ready to use Condition or false
*/
public function build($conditionServiceId, array $operators, array $values)
{
if (!$this->container->has($conditionServiceId)) {
return false;
}
/** @var ConditionManagerInterface $condition */
$condition = $this->container->get($conditionServiceId);
$condition->setValidatorsFromForm($operators, $values);
return $condition;
}
/**
* Get Condition inputs from serviceId
*
* @param string $conditionServiceId ConditionManager class name
*
* @return array Ready to be drawn rule inputs
*/
public function getInputs($conditionServiceId)
{
if (!$this->container->has($conditionServiceId)) {
return false;
}
/** @var ConditionManagerInterface $condition */
$condition = $this->container->get($conditionServiceId);
return $condition->getValidators();
}
}

View File

@@ -21,17 +21,12 @@
/* */
/**********************************************************************************/
namespace Thelia\Constraint\Rule;
namespace Thelia\Condition;
use Symfony\Component\Intl\Exception\NotImplementedException;
use Thelia\Constraint\ConstraintValidator;
use Thelia\Core\Translation\Translator;
use Thelia\Coupon\CouponAdapterInterface;
use Thelia\Constraint\Validator\ComparableInterface;
use Thelia\Constraint\Validator\RuleValidator;
use Thelia\Exception\InvalidRuleException;
use Thelia\Exception\InvalidRuleOperatorException;
use Thelia\Exception\InvalidRuleValueException;
use Thelia\Coupon\AdapterInterface;
use Thelia\Exception\InvalidConditionValueException;
use Thelia\Model\Currency;
use Thelia\Type\FloatType;
@@ -46,7 +41,7 @@ use Thelia\Type\FloatType;
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
abstract class CouponRuleAbstract implements CouponRuleInterface
abstract class ConditionManagerAbstract implements ConditionManagerInterface
{
// /** Operator key in $validators */
// CONST OPERATOR = 'operator';
@@ -65,7 +60,7 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
// /** @var array Parameters to be validated */
// protected $paramsToValidate = array();
/** @var CouponAdapterInterface Provide necessary value from Thelia */
/** @var AdapterInterface Provide necessary value from Thelia */
protected $adapter = null;
/** @var Translator Service Translator */
@@ -77,19 +72,19 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
/** @var array Values set by Admin in BackOffice */
protected $values = array();
/** @var ConstraintValidator Constaints validator */
protected $constraintValidator = null;
/** @var ConditionEvaluator Conditions validator */
protected $conditionValidator = null;
/**
* Constructor
*
* @param CouponAdapterInterface $adapter Service adapter
* @param AdapterInterface $adapter Service adapter
*/
function __construct(CouponAdapterInterface $adapter)
public function __construct(AdapterInterface $adapter)
{
$this->adapter = $adapter;
$this->translator = $adapter->getTranslator();
$this->constraintValidator = $adapter->getConstraintValidator();
$this->conditionValidator = $adapter->getConditionValidator();
}
// /**
@@ -108,7 +103,7 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
// throw new InvalidRuleException(get_class());
// }
// if (!in_array($validator->getOperator(), $this->availableOperators)) {
// throw new InvalidRuleOperatorException(
// throw new InvalidConditionOperatorException(
// get_class(),
// $validator->getOperator()
// );
@@ -149,7 +144,7 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
// }
/**
* Return all available Operators for this Rule
* Return all available Operators for this Condition
*
* @return array Operators::CONST
*/
@@ -161,7 +156,7 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
// /**
// * Check if Operators set for this Rule in the BackOffice are legit
// *
// * @throws InvalidRuleOperatorException if Operator is not allowed
// * @throws InvalidConditionOperatorException if Operator is not allowed
// * @return bool
// */
// protected function checkBackOfficeInputsOperators()
@@ -172,7 +167,7 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
// if (!isset($operator)
// ||!in_array($operator, $this->availableOperators)
// ) {
// throw new InvalidRuleOperatorException(get_class(), $key);
// throw new InvalidConditionOperatorException(get_class(), $key);
// }
// }
// return true;
@@ -233,7 +228,7 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
}
/**
* Get Rule Service id
* Get ConditionManager Service id
*
* @return string
*/
@@ -243,7 +238,7 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
}
/**
* Validate if Operator given is available for this Coupon
* Validate if Operator given is available for this Condition
*
* @param string $operator Operator to validate ex <
* @param array $availableOperators Available operators
@@ -256,19 +251,19 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
}
/**
* Return a serializable Rule
* Return a serializable Condition
*
* @return SerializableRule
* @return SerializableCondition
*/
public function getSerializableRule()
public function getSerializableCondition()
{
$serializableRule = new SerializableRule();
$serializableRule->ruleServiceId = $this->serviceId;
$serializableRule->operators = $this->operators;
$serializableCondition = new SerializableCondition();
$serializableCondition->conditionServiceId = $this->serviceId;
$serializableCondition->operators = $this->operators;
$serializableRule->values = $this->values;
$serializableCondition->values = $this->values;
return $serializableRule;
return $serializableCondition;
}
@@ -278,20 +273,20 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
* @param string $currencyValue Currency EUR|USD|..
*
* @return bool
* @throws \Thelia\Exception\InvalidRuleValueException
* @throws \Thelia\Exception\InvalidConditionValueException
*/
protected function IsCurrencyValid($currencyValue)
{
$availableCurrencies = $this->adapter->getAvailableCurrencies();
/** @var Currency $currency */
$currencyFound = false;
foreach ($availableCurrencies as $key => $currency) {
foreach ($availableCurrencies as $currency) {
if ($currencyValue == $currency->getCode()) {
$currencyFound = true;
}
}
if (!$currencyFound) {
throw new InvalidRuleValueException(
throw new InvalidConditionValueException(
get_class(), 'currency'
);
}
@@ -305,13 +300,13 @@ abstract class CouponRuleAbstract implements CouponRuleInterface
* @param float $priceValue Price value to check
*
* @return bool
* @throws \Thelia\Exception\InvalidRuleValueException
* @throws \Thelia\Exception\InvalidConditionValueException
*/
protected function isPriceValid($priceValue)
{
$floatType = new FloatType();
if (!$floatType->isValid($priceValue) || $priceValue <= 0) {
throw new InvalidRuleValueException(
throw new InvalidConditionValueException(
get_class(), 'price'
);
}

View File

@@ -21,30 +21,30 @@
/* */
/**********************************************************************************/
namespace Thelia\Constraint\Rule;
namespace Thelia\Condition;
use Thelia\Core\Translation\Translator;
use Thelia\Coupon\CouponAdapterInterface;
use Thelia\Coupon\AdapterInterface;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Represents a condition of whether the Rule is applied or not
* Manage how the application checks its state in order to check if it matches the implemented condition
*
* @package Constraint
* @package Condition
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
interface CouponRuleInterface
interface ConditionManagerInterface
{
/**
* Constructor
*
* @param CouponAdapterInterface $adapter Service adapter
* @param AdapterInterface $adapter Service adapter
*/
function __construct(CouponAdapterInterface $adapter);
function __construct(AdapterInterface $adapter);
/**
* Get Rule Service id
@@ -86,14 +86,14 @@ interface CouponRuleInterface
// public function isMatching();
/**
* Test if Customer meets conditions
* Test if the current application state matches conditions
*
* @return bool
*/
public function isMatching();
/**
* Return all available Operators for this Rule
* Return all available Operators for this condition
*
* @return array Operators::CONST
*/
@@ -133,14 +133,10 @@ interface CouponRuleInterface
/**
* Return a serializable Rule
* Return a serializable Condition
*
* @return SerializableRule
* @return SerializableCondition
*/
public function getSerializableRule();
public function getSerializableCondition();
}

View File

@@ -21,17 +21,10 @@
/* */
/**********************************************************************************/
namespace Thelia\Constraint\Rule;
namespace Thelia\Condition\Implementation;
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;
use Thelia\Condition\ConditionManagerAbstract;
/**
* Created by JetBrains PhpStorm.
@@ -40,11 +33,11 @@ use Thelia\Type\FloatType;
*
* Allow every one, perform no check
*
* @package Constraint
* @package Condition
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class AvailableForEveryoneManager extends CouponRuleAbstract
class MatchForEveryoneManager extends ConditionManagerAbstract
{
/** @var string Service Id from Resources/config.xml */
protected $serviceId = 'thelia.constraint.rule.available_for_everyone';
@@ -102,7 +95,7 @@ class AvailableForEveryoneManager extends CouponRuleAbstract
return $this->translator->trans(
'Everybody can use it (no condition)',
array(),
'constraint'
'condition'
);
}
@@ -116,7 +109,7 @@ class AvailableForEveryoneManager extends CouponRuleAbstract
$toolTip = $this->translator->trans(
'Will return always true',
array(),
'constraint'
'condition'
);
return $toolTip;

View File

@@ -21,38 +21,33 @@
/* */
/**********************************************************************************/
namespace Thelia\Constraint\Rule;
namespace Thelia\Condition\Implementation;
use Symfony\Component\Intl\Exception\NotImplementedException;
use Symfony\Component\Translation\Translator;
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\Condition\ConditionManagerAbstract;
use Thelia\Condition\Operators;
use Thelia\Exception\InvalidConditionOperatorException;
use Thelia\Model\Currency;
use Thelia\Model\CurrencyQuery;
use Thelia\Type\FloatType;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Rule AvailableForTotalAmount
* Condition AvailableForTotalAmount
* Check if a Checkout total amount match criteria
*
* @package Constraint
* @package Condition
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class AvailableForTotalAmountManager extends CouponRuleAbstract
class MatchForTotalAmountManager extends ConditionManagerAbstract
{
/** Rule 1st parameter : price */
/** Condition 1st parameter : price */
CONST INPUT1 = 'price';
/** Rule 1st parameter : currency */
/** Condition 1st parameter : currency */
CONST INPUT2 = 'currency';
/** @var string Service Id from Resources/config.xml */
@@ -101,7 +96,7 @@ class AvailableForTotalAmountManager extends CouponRuleAbstract
* @param string $currencyOperator Currency Operator ex =
* @param string $currencyValue Currency set to meet condition
*
* @throws \InvalidArgumentException
* @throws \Thelia\Exception\InvalidConditionOperatorException
* @return $this
*/
protected function setValidators($priceOperator, $priceValue, $currencyOperator, $currencyValue)
@@ -111,7 +106,7 @@ class AvailableForTotalAmountManager extends CouponRuleAbstract
$this->availableOperators[self::INPUT1]
);
if (!$isOperator1Legit) {
throw new InvalidRuleOperatorException(
throw new InvalidConditionOperatorException(
get_class(), 'price'
);
}
@@ -121,7 +116,7 @@ class AvailableForTotalAmountManager extends CouponRuleAbstract
$this->availableOperators[self::INPUT2]
);
if (!$isOperator1Legit) {
throw new InvalidRuleOperatorException(
throw new InvalidConditionOperatorException(
get_class(), 'price'
);
}
@@ -164,12 +159,12 @@ class AvailableForTotalAmountManager extends CouponRuleAbstract
return false;
}
$constraint1 = $this->constraintValidator->variableOpComparison(
$constraint1 = $this->conditionValidator->variableOpComparison(
$this->adapter->getCartTotalPrice(),
$this->operators[self::INPUT1],
$this->values[self::INPUT1]
);
$constraint2 = $this->constraintValidator->variableOpComparison(
$constraint2 = $this->conditionValidator->variableOpComparison(
$this->adapter->getCheckoutCurrency(),
$this->operators[self::INPUT2],
$this->values[self::INPUT2]
@@ -177,6 +172,7 @@ class AvailableForTotalAmountManager extends CouponRuleAbstract
if ($constraint1 && $constraint2) {
return true;
}
return false;
}
@@ -190,7 +186,7 @@ class AvailableForTotalAmountManager extends CouponRuleAbstract
return $this->translator->trans(
'Cart total amount',
array(),
'constraint'
'condition'
);
}
@@ -212,7 +208,7 @@ class AvailableForTotalAmountManager extends CouponRuleAbstract
'%amount%' => $this->values[self::INPUT1],
'%currency%' => $this->values[self::INPUT2]
),
'constraint'
'condition'
);
return $toolTip;
@@ -235,12 +231,12 @@ class AvailableForTotalAmountManager extends CouponRuleAbstract
$name1 = $this->translator->trans(
'Price',
array(),
'constraint'
'condition'
);
$name2 = $this->translator->trans(
'Currency',
array(),
'constraint'
'condition'
);
return array(

View File

@@ -21,18 +21,13 @@
/* */
/**********************************************************************************/
namespace Thelia\Constraint\Rule;
namespace Thelia\Condition\Implementation;
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\InvalidRuleOperatorException;
use Thelia\Exception\InvalidRuleValueException;
use Thelia\Type\FloatType;
use Thelia\Condition\ConditionManagerAbstract;
use Thelia\Condition\Operators;
use Thelia\Exception\InvalidConditionOperatorException;
use Thelia\Exception\InvalidConditionValueException;
/**
* Created by JetBrains PhpStorm.
@@ -41,13 +36,13 @@ use Thelia\Type\FloatType;
*
* Check a Checkout against its Product number
*
* @package Constraint
* @package Condition
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class AvailableForXArticlesManager extends CouponRuleAbstract
class MatchForXArticlesManager extends ConditionManagerAbstract
{
/** Rule 1st parameter : quantity */
/** Condition 1st parameter : quantity */
CONST INPUT1 = 'quantity';
/** @var string Service Id from Resources/config.xml */
@@ -89,7 +84,8 @@ class AvailableForXArticlesManager extends CouponRuleAbstract
* @param string $quantityOperator Quantity Operator ex <
* @param int $quantityValue Quantity set to meet condition
*
* @throws \InvalidArgumentException
* @throws \Thelia\Exception\InvalidConditionValueException
* @throws \Thelia\Exception\InvalidConditionOperatorException
* @return $this
*/
protected function setValidators($quantityOperator, $quantityValue)
@@ -99,13 +95,13 @@ class AvailableForXArticlesManager extends CouponRuleAbstract
$this->availableOperators[self::INPUT1]
);
if (!$isOperator1Legit) {
throw new InvalidRuleOperatorException(
throw new InvalidConditionOperatorException(
get_class(), 'quantity'
);
}
if ((int) $quantityValue <= 0) {
throw new InvalidRuleValueException(
throw new InvalidConditionValueException(
get_class(), 'quantity'
);
}
@@ -127,15 +123,16 @@ class AvailableForXArticlesManager extends CouponRuleAbstract
*/
public function isMatching()
{
$constraint1 = $this->constraintValidator->variableOpComparison(
$condition1 = $this->conditionValidator->variableOpComparison(
$this->adapter->getNbArticlesInCart(),
$this->operators[self::INPUT1],
$this->values[self::INPUT1]
);
if ($constraint1) {
if ($condition1) {
return true;
}
return false;
}
@@ -149,7 +146,7 @@ class AvailableForXArticlesManager extends CouponRuleAbstract
return $this->translator->trans(
'Number of articles in cart',
array(),
'constraint'
'condition'
);
}
@@ -170,7 +167,7 @@ class AvailableForXArticlesManager extends CouponRuleAbstract
'%operator%' => $i18nOperator,
'%quantity%' => $this->values[self::INPUT1]
),
'constraint'
'condition'
);
return $toolTip;
@@ -186,7 +183,7 @@ class AvailableForXArticlesManager extends CouponRuleAbstract
$name1 = $this->translator->trans(
'Quantity',
array(),
'constraint'
'condition'
);
return array(
@@ -200,5 +197,4 @@ class AvailableForXArticlesManager extends CouponRuleAbstract
)
);
}
}
}

View File

@@ -21,17 +21,16 @@
/* */
/**********************************************************************************/
namespace Thelia\Constraint\Rule;
namespace Thelia\Condition;
use Symfony\Component\Translation\Translator;
use Thelia\Constraint\Validator\ComparableInterface;
use Thelia\Core\Translation\Translator;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Represent available Operations in rule checking
* Represent available Operations in condition checking
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
@@ -72,56 +71,56 @@ abstract class Operators
$ret = $translator->trans(
'inferior to',
array(),
'constraint'
'condition'
);
break;
case self::INFERIOR_OR_EQUAL:
$ret = $translator->trans(
'inferior or equal to',
array(),
'constraint'
'condition'
);
break;
case self::EQUAL:
$ret = $translator->trans(
'equal to',
array(),
'constraint'
'condition'
);
break;
case self::SUPERIOR_OR_EQUAL:
$ret = $translator->trans(
'superior or equal to',
array(),
'constraint'
'condition'
);
break;
case self::SUPERIOR:
$ret = $translator->trans(
'superior to',
array(),
'constraint'
'condition'
);
break;
case self::DIFFERENT:
$ret = $translator->trans(
'different from',
array(),
'constraint'
'condition'
);
break;
case self::IN:
$ret = $translator->trans(
'in',
array(),
'constraint'
'condition'
);
break;
case self::OUT:
$ret = $translator->trans(
'not in',
array(),
'constraint'
'condition'
);
break;
default:

View File

@@ -21,32 +21,32 @@
/* */
/**********************************************************************************/
namespace Thelia\Constraint\Rule;
namespace Thelia\Condition;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* A rule set by an admin ready to be serialized and stored in DataBase
* A condition set by an admin ready to be serialized and stored in DataBase
*
* @package Constraint
* @package Condition
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class SerializableRule
class SerializableCondition
{
/** @var string Rule Service id */
public $ruleServiceId = null;
/** @var string Condition Service id */
public $conditionServiceId = null;
/** @var array Operators set by Admin for this Rule */
/** @var array Operators set by Admin for this Condition */
public $operators = array();
/** @var array Values set by Admin for this Rule */
/** @var array Values set by Admin for this Condition */
public $values = array();
/**
* Get Operators set by Admin for this Rule
* Get Operators set by Admin for this Condition
*
* @return array
*/
@@ -56,17 +56,17 @@ class SerializableRule
}
/**
* Get Rule Service id
* Get Condition Service id
*
* @return string
*/
public function getRuleServiceId()
public function getConditionServiceId()
{
return $this->ruleServiceId;
return $this->conditionServiceId;
}
/**
* Get Values set by Admin for this Rule
* Get Values set by Admin for this Condition
*
* @return array
*/
@@ -74,8 +74,4 @@ class SerializableRule
{
return $this->values;
}
}
}

View File

@@ -1,174 +0,0 @@
<?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\AvailableForEveryoneManager;
use Thelia\Constraint\Rule\AvailableForTotalAmountManager;
use Thelia\Constraint\Rule\CouponRuleInterface;
use Thelia\Constraint\Rule\SerializableRule;
use Thelia\Coupon\CouponAdapterInterface;
use Thelia\Coupon\CouponRuleCollection;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Manage how Constraint could interact
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class ConstraintFactory
{
/** @var ContainerInterface Service Container */
protected $container = null;
/** @var CouponAdapterInterface Provide necessary value from Thelia*/
protected $adapter;
/** @var array CouponRuleCollection to process*/
protected $rules = null;
/**
* Constructor
*
* @param ContainerInterface $container Service container
*/
function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->adapter = $container->get('thelia.adapter');
}
/**
* Serialize a collection of rules
*
* @param CouponRuleCollection $collection A collection of rules
*
* @return string A ready to be stored Rule collection
*/
public function serializeCouponRuleCollection(CouponRuleCollection $collection)
{
if ($collection->isEmpty()) {
/** @var CouponRuleInterface $ruleNoCondition */
$ruleNoCondition = $this->container->get(
'thelia.constraint.rule.available_for_everyone'
);
$collection->add($ruleNoCondition);
}
$serializableRules = array();
$rules = $collection->getRules();
if ($rules !== null) {
/** @var $rule CouponRuleInterface */
foreach ($rules as $rule) {
// Remove all rule if the "no condition" rule is found
// if ($rule->getServiceId() == 'thelia.constraint.rule.available_for_everyone') {
// return base64_encode(json_encode(array($rule->getSerializableRule())));
// }
$serializableRules[] = $rule->getSerializableRule();
}
}
return base64_encode(json_encode($serializableRules));
}
/**
* Unserialize a collection of rules
*
* @param string $serializedRules Serialized Rules
*
* @return CouponRuleCollection Rules ready to be processed
*/
public function unserializeCouponRuleCollection($serializedRules)
{
$unserializedRules = json_decode(base64_decode($serializedRules));
$collection = new CouponRuleCollection();
if (!empty($serializedRules) && !empty($unserializedRules)) {
/** @var SerializableRule $rule */
foreach ($unserializedRules as $rule) {
if ($this->container->has($rule->ruleServiceId)) {
/** @var CouponRuleInterface $couponRule */
$couponRule = $this->build(
$rule->ruleServiceId,
(array) $rule->operators,
(array) $rule->values
);
$collection->add(clone $couponRule);
}
}
}
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;
}
/**
* Get Coupon Rule inputs from serviceId
*
* @param string $ruleServiceId Rule class name
*
* @return array Ready to be drawn rule inputs
*/
public function getInputs($ruleServiceId)
{
if (!$this->container->has($ruleServiceId)) {
return false;
}
/** @var CouponRuleInterface $rule */
$rule = $this->container->get($ruleServiceId);
return $rule->getValidators();
}
}