WIP Coupon

Refactor : creating dedicated reusable module for Constraints
Adding ConstraintManager
Secured :
- Effects : RemoveXPercent + RemoveXAmount
- Validators : all  except ModelParam (need CustomerModelParam, AreaModelParam, CountryModelParam ?)
- Conditions : AvailableForTotalAmount
This commit is contained in:
gmorel
2013-08-27 18:53:18 +02:00
parent e971179a60
commit c24e82758f
48 changed files with 1632 additions and 350 deletions

View File

@@ -0,0 +1,59 @@
<?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\Rule;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class AvailableForDate extends AvailableForPeriod
{
/**
* Check if backoffice inputs are relevant or not
*
* @return bool
*/
public function checkBackOfficeInput()
{
// TODO: Implement checkBackOfficeInput() method.
}
/**
* Check if Checkout inputs are relevant or not
*
* @return bool
*/
public function checkCheckoutInput()
{
// TODO: Implement checkCheckoutInput() method.
}
}

View File

@@ -0,0 +1,59 @@
<?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\Rule;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class AvailableForLocationX extends CouponRuleAbstract
{
/**
* Check if backoffice inputs are relevant or not
*
* @return bool
*/
public function checkBackOfficeInput()
{
// TODO: Implement checkBackOfficeInput() method.
}
/**
* Check if Checkout inputs are relevant or not
*
* @return bool
*/
public function checkCheckoutInput()
{
// TODO: Implement checkCheckoutInput() method.
}
}

View File

@@ -0,0 +1,57 @@
<?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\Rule;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class AvailableForPeriod extends CouponRuleAbstract
{
/**
* Check if backoffice inputs are relevant or not
*
* @return bool
*/
public function checkBackOfficeInput()
{
// TODO: Implement checkBackOfficeInput() method.
}
/**
* Check if Checkout inputs are relevant or not
*
* @return bool
*/
public function checkCheckoutInput()
{
// TODO: Implement checkCheckoutInput() method.
}
}

View File

@@ -0,0 +1,57 @@
<?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\Rule;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class AvailableForRepeatedDate extends AvailableForDate
{
/**
* Check if backoffice inputs are relevant or not
*
* @return bool
*/
public function checkBackOfficeInput()
{
// TODO: Implement checkBackOfficeInput() method.
}
/**
* Check if Checkout inputs are relevant or not
*
* @return bool
*/
public function checkCheckoutInput()
{
// TODO: Implement checkCheckoutInput() method.
}
}

View File

@@ -0,0 +1,73 @@
<?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\Rule;
use Thelia\Coupon\CouponAdapterInterface;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class AvailableForRepeatedPeriod extends AvailableForPeriod
{
/**
* Generate current Rule param to be validated from adapter
*
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @throws \Symfony\Component\Intl\Exception\NotImplementedException
* @return $this
*/
public function setParametersToValidate(CouponAdapterInterface $adapter)
{
// @todo implement
}
/**
* Check if backoffice inputs are relevant or not
*
* @return bool
*/
public function checkBackOfficeInput()
{
// TODO: Implement checkBackOfficeInput() method.
}
/**
* Check if Checkout inputs are relevant or not
*
* @return bool
*/
public function checkCheckoutInput()
{
// TODO: Implement checkCheckoutInput() method.
}
}

View File

@@ -0,0 +1,223 @@
<?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\Rule;
use Symfony\Component\Intl\Exception\NotImplementedException;
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;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Rule AvailableForTotalAmount
* Check if a Checkout total amount match criteria
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class AvailableForTotalAmount extends CouponRuleAbstract
{
/** Rule 1st parameter : price */
CONST PARAM1_PRICE = 'price';
/** @var array Available Operators (Operators::CONST) */
protected $availableOperators = array(
Operators::INFERIOR,
Operators::EQUAL,
Operators::SUPERIOR,
);
/** @var RuleValidator Price Validator */
protected $priceValidator = null;
/**
* Constructor
*
* @param array $validators Array of RuleValidator
* validating $paramsToValidate against
*
* @throws \Thelia\Exception\InvalidRuleException
*/
public function __construct(array $validators)
{
parent::__construct($validators);
if (isset($validators[self::PARAM1_PRICE])
&& $validators[self::PARAM1_PRICE] instanceof RuleValidator
) {
$this->priceValidator = $validators[self::PARAM1_PRICE];
} else {
throw new InvalidRuleException(get_class());
}
}
/**
* Check if backoffice inputs are relevant or not
*
* @throws InvalidRuleOperatorException if Operator is not allowed
* @throws InvalidRuleValueException if Value is not allowed
* @return bool
*/
public function checkBackOfficeInput()
{
if (!isset($this->validators)
|| empty($this->validators)
||!isset($this->validators[self::PARAM1_PRICE])
||!isset($this->validators[self::PARAM1_PRICE])
) {
throw new InvalidRuleValueException(get_class(), self::PARAM1_PRICE);
}
/** @var RuleValidator $ruleValidator */
$ruleValidator = $this->validators[self::PARAM1_PRICE];
/** @var PriceParam $price */
$price = $ruleValidator->getParam();
if (!$price instanceof PriceParam) {
throw new InvalidRuleValueException(get_class(), self::PARAM1_PRICE);
}
$this->checkBackOfficeInputsOperators();
return $this->isPriceValid($price->getPrice());
}
/**
* Check if Checkout inputs are relevant or not
*
* @throws InvalidRuleValueException if Value is not allowed
* @return bool
*/
public function checkCheckoutInput()
{
if (!isset($this->paramsToValidate)
|| empty($this->paramsToValidate)
||!isset($this->paramsToValidate[self::PARAM1_PRICE])
) {
throw new InvalidRuleValueException(get_class(), self::PARAM1_PRICE);
}
$quantity = $this->paramsToValidate[self::PARAM1_PRICE];
return $this->isPriceValid($quantity);
}
/**
* Check if a price is valid
*
* @param float $price Price to check
*
* @throws InvalidRuleValueException if Value is not allowed
* @return bool
*/
protected function isPriceValid($price)
{
$priceValidator = $this->priceValidator;
try {
$priceValidator->getParam()->compareTo($price);
} catch(\InvalidArgumentException $e) {
throw new InvalidRuleValueException(get_class(), self::PARAM1_PRICE);
}
return true;
}
/**
* Generate current Rule param to be validated from adapter
*
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @return $this
*/
protected function setParametersToValidate(CouponAdapterInterface $adapter)
{
$this->paramsToValidate = array(
self::PARAM1_PRICE => $adapter->getCartTotalPrice()
);
return $this;
}
/**
* Return all validators
* Serialization purpose
*
* @return array
*/
public function getValidators()
{
return $this->validators;
}
/**
* Get I18n name
*
* @return string
*/
public function getName()
{
return $this->adapter
->getTranslator()
->trans('Cart total amount', null, 'constraint');
}
/**
* Get I18n tooltip
*
* @return string
*/
public function getToolTip()
{
$i18nOperator = Operators::getI18n(
$this->adapter, $this->priceValidator->getOperator()
);
$toolTip = $this->adapter
->getTranslator()
->trans(
'If cart total amount is %operator% %amount% %currency%',
array(
'%operator%' => $i18nOperator,
'%amount%' => $this->priceValidator->getParam()->getPrice(),
'%currency%' => $this->priceValidator->getParam()->getCurrency()
),
'constraint'
);
return $toolTip;
}
}

View File

@@ -0,0 +1,38 @@
<?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\Rule;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class AvailableForTotalAmountForCategoryY extends AvailableForTotalAmount
{
}

View File

@@ -0,0 +1,60 @@
<?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\Rule;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Check a Checkout against its Product number
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class AvailableForXArticles extends CouponRuleAbstract
{
/**
* Check if backoffice inputs are relevant or not
*
* @return bool
*/
public function checkBackOfficeInput()
{
// TODO: Implement checkBackOfficeInput() method.
}
/**
* Check if Checkout inputs are relevant or not
*
* @return bool
*/
public function checkCheckoutInput()
{
// TODO: Implement checkCheckoutInput() method.
}
}

View File

@@ -0,0 +1,179 @@
<?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\Rule;
use Symfony\Component\Intl\Exception\NotImplementedException;
use Thelia\Coupon\CouponAdapterInterface;
use Thelia\Constraint\Validator\ComparableInterface;
use Thelia\Constraint\Validator\RuleValidator;
use Thelia\Exception\InvalidRuleException;
use Thelia\Exception\InvalidRuleOperatorException;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Assist in writing a condition of whether the Rule is applied or not
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
abstract class CouponRuleAbstract implements CouponRuleInterface
{
/** Operator key in $validators */
CONST OPERATOR = 'operator';
/** Value key in $validators */
CONST VALUE = 'value';
/** @var array Available Operators (Operators::CONST) */
protected $availableOperators = array();
/** @var array Parameters validating parameters against */
protected $validators = array();
/** @var array Parameters to be validated */
protected $paramsToValidate = array();
/**
* Constructor
* Ex:
* Param 1 :
* $priceValidator = new RuleValidator(
* Operators::INFERIOR,
* new IntegerParam(10)
* )
* $validators[AvailableForTotalAmount::PARAM1_PRICE] = $priceValidator
*
* Param 2 :
* $paramsToValidate[AvailableForTotalAmount::PARAM1_PRICE] = 9
*
* @param array $validators Array of RuleValidator
* validating $paramsToValidate against
*
* @throws InvalidRuleException
*/
public function __construct(array $validators)
{
$this->setValidators($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());
}
}
$this->validators = $validators;
return $this;
}
/**
* Check if the current Checkout matches this condition
*
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @return bool
*/
public function isMatching(CouponAdapterInterface $adapter)
{
$this->setParametersToValidate($adapter);
$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
*
* @return array Operators::CONST
*/
public function getAvailableOperators()
{
return $this->availableOperators;
}
/**
* Check if Operators set for this Rule in the BackOffice are legit
*
* @throws InvalidRuleOperatorException if Operator is not allowed
* @return bool
*/
protected function checkBackOfficeInputsOperators()
{
/** @var RuleValidator $param */
foreach ($this->validators as $key => $param) {
$operator = $param->getOperator();
if (!isset($operator)
||!in_array($operator, $this->availableOperators)
) {
throw new InvalidRuleOperatorException(get_class(), $key);
}
}
return true;
}
/**
* Generate current Rule param to be validated from adapter
*
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @throws \Thelia\Exception\NotImplementedException
* @return $this
*/
protected function setParametersToValidate(CouponAdapterInterface $adapter)
{
throw new \Thelia\Exception\NotImplementedException();
}
}

View File

@@ -0,0 +1,87 @@
<?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\Rule;
use Thelia\Coupon\CouponAdapterInterface;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Represents a condition of whether the Rule is applied or not
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
interface CouponRuleInterface
{
/**
* Check if backoffice inputs are relevant or not
*
* @return bool
*/
public function checkBackOfficeInput();
/**
* Check if Checkout inputs are relevant or not
*
* @return bool
*/
public function checkCheckoutInput();
/**
* Check if the current Checkout matches this condition
*
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @return bool
*/
public function isMatching(CouponAdapterInterface $adapter);
/**
* Return all available Operators for this Rule
*
* @return array Operators::CONST
*/
public function getAvailableOperators();
/**
* Get I18n name
*
* @return string
*/
public function getName();
/**
* Get I18n tooltip
*
* @return string
*/
public function getToolTip();
}

View File

@@ -0,0 +1,171 @@
<?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\Rule;
use Thelia\Constraint\Validator\ComparableInterface;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Represent available Operations in rule checking
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
abstract class Operators
{
/** Param1 is inferior to Param2 */
CONST INFERIOR = '<';
/** Param1 is inferior to Param2 */
CONST INFERIOR_OR_EQUAL = '<=';
/** Param1 is equal to Param2 */
CONST EQUAL = '==';
/** Param1 is superior to Param2 */
CONST SUPERIOR_OR_EQUAL = '>=';
/** Param1 is superior to Param2 */
CONST SUPERIOR = '>';
/** Param1 is different to Param2 */
CONST DIFFERENT = '!=';
/**
* 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
*
* @param CouponAdapterInterface $adapter Provide necessary value from Thelia
* @param string $operator Operator const
*
* @return string
*/
public static function getI18n(CouponAdapterInterface $adapter, $operator)
{
$translator = $adapter->getTranslator();
$ret = $operator;
switch ($operator) {
case self::INFERIOR:
$ret = $translator->trans(
'inferior to',
null,
'constraint'
);
break;
case self::INFERIOR_OR_EQUAL:
$ret = $translator->trans(
'inferior or equals to',
null,
'constraint'
);
break;
case self::EQUAL:
$ret = $translator->trans(
'equals to',
null,
'constraint'
);
break;
case self::SUPERIOR_OR_EQUAL:
$ret = $translator->trans(
'superior or equals to',
null,
'constraint'
);
break;
case self::SUPERIOR:
$ret = $translator->trans(
'superior to',
null,
'constraint'
);
break;
case self::DIFFERENT:
$ret = $translator->trans(
'different from',
null,
'constraint'
);
break;
default:
}
return $ret;
}
}