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,82 @@
<?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 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 ConstraintManager
{
/** @var CouponAdapterInterface Provide necessary value from Thelia*/
protected $adapter;
/** @var array CouponRuleCollection to process*/
protected $rules = null;
/**
* Constructor
*
* @param CouponAdapterInterface $adapter Provide necessary value from Thelia
* @param CouponRuleCollection $rules Rules associated with the Constraint
*/
function __construct(CouponAdapterInterface $adapter, CouponRuleCollection $rules)
{
$this->adapter = $adapter;
$this->rule = $rules;
}
/**
* Check if the current Coupon is matching its conditions (Rules)
* Thelia variables are given by the CouponAdapterInterface
*
* @return bool
*/
public function isMatching()
{
$isMatching = true;
/** @var CouponRuleInterface $rule */
foreach ($this->rules->getRules() as $rule) {
if (!$rule->isMatching($this->adapter)) {
$isMatching = false;
}
}
return $isMatching;
}
}

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;
}
}

View File

@@ -0,0 +1,49 @@
<?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\Validator;
/**
* Comparable interface
* Allows to compare two value objects to each other for similarity.
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
*/
interface ComparableInterface
{
/**
* Compare the current object to the passed $other.
*
* Returns 0 if they are semantically equal, 1 if the other object
* is less than the current one, or -1 if its more than the current one.
*
* This method should not check for identity using ===, only for semantically equality for example
* when two different DateTime instances point to the exact same Date + TZ.
*
* @param mixed $other Object
*
* @return int
*/
public function compareTo($other);
}

View File

@@ -0,0 +1,120 @@
<?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\Validator;
use Thelia\Coupon\CouponAdapterInterface;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Represent a DateTime
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class DateParam extends RuleParameterAbstract
{
/** @var \DateTime Date */
protected $dateTime = null;
/**
* Constructor
*
* @param CouponAdapterInterface $adapter Provide necessary value from Thelia
* @param \DateTime $dateTime DateTime
*/
public function __construct(CouponAdapterInterface $adapter, \DateTime $dateTime)
{
$this->dateTime = $dateTime;
$this->adapter = $adapter;
}
/**
* Get DateTime
*
* @return \DateTime
*/
public function getDateTime()
{
return clone $this->dateTime;
}
/**
* Compare the current object to the passed $other.
*
* Returns 0 if they are semantically equal, 1 if the other object
* is less than the current one, or -1 if its more than the current one.
*
* This method should not check for identity using ===, only for semantically equality for example
* when two different DateTime instances point to the exact same Date + TZ.
*
* @param mixed $other Object
*
* @throws \InvalidArgumentException
* @return int
*/
public function compareTo($other)
{
if (!$other instanceof \DateTime) {
throw new \InvalidArgumentException('DateParam can compare only DateTime');
}
$ret = -1;
if ($this->dateTime == $other) {
$ret = 0;
} elseif ($this->dateTime > $other) {
$ret = 1;
} else {
$ret = -1;
}
return $ret;
}
/**
* Get Parameter value to test against
*
* @return \Datetime
*/
public function getValue()
{
return clone $this->dateTime;
}
/**
* Get I18n tooltip
*
* @return string
*/
public function getToolTip()
{
return $this->adapter
->getTranslator()
->trans('A date (ex: YYYY-MM-DD HH:MM:SS)', null, 'constraint');
}
}

View File

@@ -0,0 +1,121 @@
<?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\Validator;
use Thelia\Coupon\CouponAdapterInterface;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Represent an Integer
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class IntegerParam extends RuleParameterAbstract
{
/** @var int Integer to compare with */
protected $integer = 0;
/**
* Constructor
*
* @param CouponAdapterInterface $adapter Provide necessary value from Thelia
* @param int $integer Integer
*/
public function __construct(CouponAdapterInterface $adapter, $integer)
{
$this->integer = $integer;
$this->adapter = $adapter;
}
/**
* Get integer
*
* @return int
*/
public function getInteger()
{
return $this->integer;
}
/**
* Compare the current object to the passed $other.
*
* Returns 0 if they are semantically equal, 1 if the other object
* is less than the current one, or -1 if its more than the current one.
*
* This method should not check for identity using ===, only for semantically equality for example
* when two different DateTime instances point to the exact same Date + TZ.
*
* @param mixed $other Object
*
* @throws \InvalidArgumentException
* @return int
*/
public function compareTo($other)
{
if (!is_integer($other)) {
throw new \InvalidArgumentException('IntegerParam can compare only int');
}
$ret = -1;
if ($this->integer == $other) {
$ret = 0;
} elseif ($this->integer > $other) {
$ret = 1;
} else {
$ret = -1;
}
return $ret;
}
/**
* Get Parameter value to test against
*
* @return int
*/
public function getValue()
{
return $this->integer;
}
/**
* Get I18n tooltip
*
* @return string
*/
public function getToolTip()
{
return $this->adapter
->getTranslator()
->trans('A number (ex: 42)', null, 'constraint');
}
}

View File

@@ -0,0 +1,165 @@
<?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\Validator;
use Thelia\Coupon\CouponAdapterInterface;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Represent an DateTime period
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class IntervalParam extends RuleParameterAbstract
{
/** @var \DatePeriod Date period */
protected $datePeriod = null;
/** @var \DateTime Start date */
protected $start = null;
/** @var \DateInterval Interval date */
protected $interval = null;
/**
* Constructor
*
* @param CouponAdapterInterface $adapter Provide necessary value from Thelia
* @param \DateTime $start Start interval
* @param \DateInterval $interval Period
*/
public function __construct(CouponAdapterInterface $adapter, \DateTime $start, \DateInterval $interval)
{
$this->datePeriod = new \DatePeriod($start, $interval, 1);
$this->adapter = $adapter;
$this->start = $start;
$this->interval = $interval;
}
/**
* Get Interval
*
* @return \DateInterval
*/
public function getInterval()
{
return $this->interval;
}
/**
* Get start date
*
* @return \DateTime
*/
public function getStart()
{
return $this->start;
}
/**
* Get DatePeriod
*
* @return \DatePeriod
*/
public function getDatePeriod()
{
return clone $this->datePeriod;
}
/**
* Compare the current object to the passed $other.
*
* Returns 0 if they are semantically equal, 1 if the other object
* is less than the current one, or -1 if its more than the current one.
*
* This method should not check for identity using ===, only for semantically equality for example
* when two different DateTime instances point to the exact same Date + TZ.
*
* @param mixed $other Object
*
* @throws \InvalidArgumentException
* @return int
*/
public function compareTo($other)
{
if (!$other instanceof \DateTime) {
throw new \InvalidArgumentException('IntervalParam can compare only DateTime');
}
/** @var \DateTime Start Date */
$startDate = null;
/** @var \DateTime End Date */
$endDate = null;
foreach ($this->datePeriod as $key => $value) {
if ($key == 0) {
$startDate = $value;
}
if ($key == 1) {
$endDate = $value;
}
}
$ret = -1;
if ($startDate <= $other && $other <= $endDate) {
$ret = 0;
} elseif ($startDate > $other) {
$ret = 1;
} else {
$ret = -1;
}
return $ret;
}
/**
* Get Parameter value to test against
*
* @return \DatePeriod
*/
public function getValue()
{
return clone $this->datePeriod;
}
/**
* Get I18n tooltip
*
* @return string
*/
public function getToolTip()
{
return $this->adapter
->getTranslator()
->trans('An interval between two dates', null, 'constraint');
}
}

View File

@@ -0,0 +1,115 @@
<?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\Validator;
use InvalidArgumentException;
use Thelia\Coupon\CouponAdapterInterface;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Represent a Model
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class ModelParam extends IntegerParam
{
/** @var string Model Class name */
protected $modelClass = null;
/** @var ModelCriteria */
protected $queryBuilder = null;
/**
* Constructor
*
* @param CouponAdapterInterface $adapter Provide necessary value from Thelia
* @param int $integer Integer
* @param string $modelClass Model class name
*
* @throws InvalidArgumentException
*/
public function __construct(CouponAdapterInterface $adapter, $integer, $modelClass)
{
if ($integer < 0) {
$integer = 0;
}
$this->integer = $integer;
$this->adapter = $adapter;
$this->modelClass = $modelClass;
$queryClassName = $modelClass . 'Query';
try {
$this->queryBuilder = $queryClassName::create();
} catch (\Exception $e) {
throw new InvalidArgumentException('ModelParam can only compare Models');
}
}
/**
* Compare the current object to the passed $other.
*
* Returns 0 if they are semantically equal, 1 if the other object
* is less than the current one, or -1 if its more than the current one.
*
* This method should not check for identity using ===, only for semantically equality for example
* when two different DateTime instances point to the exact same Date + TZ.
*
* @param mixed $other Object
*
* @throws InvalidArgumentException
* @return int
*/
public function compareTo($other)
{
if (!is_integer($other) || $other < 0) {
throw new InvalidArgumentException(
'IntegerParam can compare only positive int'
);
}
return parent::compareTo($other);
}
/**
* Get I18n tooltip
*
* @return string
*/
public function getToolTip()
{
return $this->adapter
->getTranslator()
->trans(
'A Model',
null,
'constraint'
);
}
}

View File

@@ -0,0 +1,145 @@
<?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\Validator;
use Thelia\Coupon\CouponAdapterInterface;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Represent a Price
* Positive value with currency
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class PriceParam extends RuleParameterAbstract
{
/** @var float Positive Float to compare with */
protected $price = null;
/** @var string Currency Code ISO 4217 EUR|USD|GBP */
protected $currency = null;
/**
* Constructor
*
* @param CouponAdapterInterface $adapter Provide necessary value from Thelia
* @param float $price Positive float
* @param string $currency Currency Code ISO 4217 EUR|USD|GBP
*/
public function __construct(CouponAdapterInterface $adapter, $price, $currency)
{
$this->price = $price;
$this->currency = $currency;
$this->adapter = $adapter;
}
/**
* Get currency code
*
* @return string
*/
public function getCurrency()
{
return $this->currency;
}
/**
* Get price
*
* @return float
*/
public function getPrice()
{
return $this->price;
}
/**
* Compare the current object to the passed $other.
*
* Returns 0 if they are semantically equal, 1 if the other object
* is less than the current one, or -1 if its more than the current one.
*
* This method should not check for identity using ===, only for semantically equality for example
* when two different DateTime instances point to the exact same Date + TZ.
*
* @param mixed $other Object
*
* @throws \InvalidArgumentException
* @return int
*/
public function compareTo($other)
{
if (!is_float($other)) {
throw new \InvalidArgumentException(
'PriceParam can compare only positive float'
);
}
$epsilon = 0.00001;
$ret = -1;
if (abs($this->price - $other) < $epsilon) {
$ret = 0;
} elseif ($this->price > $other) {
$ret = 1;
} else {
$ret = -1;
}
return $ret;
}
/**
* Get Parameter value to test against
*
* @return float
*/
public function getValue()
{
return $this->price;
}
/**
* Get I18n tooltip
*
* @return string
*/
public function getToolTip()
{
return $this->adapter
->getTranslator()
->trans(
'A price in %currency% (ex: 14.50)',
array(
'%currency%' => $this->currency
),
'constraint'
);
}
}

View File

@@ -0,0 +1,99 @@
<?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\Validator;
use Thelia\Coupon\CouponAdapterInterface;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Represent a Quantity
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class QuantityParam extends IntegerParam
{
/**
* Constructor
*
* @param CouponAdapterInterface $adapter Provide necessary value from Thelia
* @param int $integer Integer
*/
public function __construct(CouponAdapterInterface $adapter, $integer)
{
if ($integer < 0) {
$integer = 0;
}
$this->integer = $integer;
$this->adapter = $adapter;
}
/**
* Compare the current object to the passed $other.
*
* Returns 0 if they are semantically equal, 1 if the other object
* is less than the current one, or -1 if its more than the current one.
*
* This method should not check for identity using ===, only for semantically equality for example
* when two different DateTime instances point to the exact same Date + TZ.
*
* @param mixed $other Object
*
* @throws \InvalidArgumentException
* @return int
*/
public function compareTo($other)
{
if (!is_integer($other) || $other < 0) {
throw new \InvalidArgumentException(
'IntegerParam can compare only positive int'
);
}
return parent::compareTo($other);
}
/**
* Get I18n tooltip
*
* @return string
*/
public function getToolTip()
{
return $this->adapter
->getTranslator()
->trans(
'A positive quantity (ex: 42)',
null,
'constraint'
);
}
}

View File

@@ -0,0 +1,117 @@
<?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\Validator;
use Thelia\Coupon\CouponAdapterInterface;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Represent A repeated Date across the time
* Ex :
* A date repeated every 1 months 5 times
* ---------*---*---*---*---*---*---------------------------> time
* 1 2 3 4 5 6
* 1 : $this->from Start date of the repetition
* *--- : $this->interval Duration of a whole cycle
* x5 : $this->recurrences How many repeated cycle, 1st excluded
* x6 : How many occurrence
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class RepeatedDateParam extends RepeatedParam
{
/**
* Constructor
*
* @param CouponAdapterInterface $adapter Provide necessary value from Thelia
*/
public function __construct(CouponAdapterInterface $adapter)
{
$this->defaultConstructor();
$this->adapter = $adapter;
}
/**
* Compare the current object to the passed $other.
*
* Returns 0 if they are semantically equal, 1 if the other object
* is less than the current one, or -1 if its more than the current one.
*
* This method should not check for identity using ===, only for semantically equality for example
* when two different DateTime instances point to the exact same Date + TZ.
*
* @param mixed $other Object
*
* @throws \InvalidArgumentException
* @return int
*/
public function compareTo($other)
{
if (!$other instanceof \DateTime) {
throw new \InvalidArgumentException('RepeatedDateParam can compare only DateTime');
}
$ret = -1;
$dates = array();
/** @var $value \DateTime */
foreach ($this->datePeriod as $value) {
$dates[$value->getTimestamp()] = $value;
}
foreach ($dates as $date) {
if ($date == $other) {
return 0;
}
}
return $ret;
}
/**
* Get Parameter value to test against
*
* @return \DatePeriod
*/
public function getValue()
{
return clone $this->datePeriod;
}
/**
* Get I18n tooltip
*
* @return string
*/
public function getToolTip()
{
return $this->adapter
->getTranslator()
->trans('A date (ex: YYYY-MM-DD HH:MM:SS)', null, 'constraint');
}
}

View File

@@ -0,0 +1,151 @@
<?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\Validator;
use Thelia\Coupon\CouponAdapterInterface;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Represent A repeated DateInterval across the time
* Ex :
* A duration of 1 month repeated every 2 months 5 times
* ---------****----****----****----****----****----****-----------------> time
* 1 2 3 4 5 6
* 1 : $this->from Start date of the repetition
* ****---- : $this->interval Duration of a whole cycle
* x5 : $this->recurrences How many repeated cycle, 1st excluded
* x6 : How many occurrence
* **** : $this->durationInDays Duration of a period
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class RepeatedIntervalParam extends RepeatedParam
{
/** @var int duration of the param */
protected $durationInDays = 1;
/**
* Get how many day a Param is lasting
*
* @return int
*/
public function getDurationInDays()
{
return $this->durationInDays;
}
/**
* Set how many day a Param is lasting
*
* @param int $durationInDays How many day a Param is lasting
*
* @return $this
*/
public function setDurationInDays($durationInDays = 1)
{
$this->durationInDays = $durationInDays;
return $this;
}
/**
* Constructor
*
* @param CouponAdapterInterface $adapter Provide necessary value from Thelia
*/
public function __construct(CouponAdapterInterface $adapter)
{
$this->defaultConstructor();
$this->adapter = $adapter;
}
/**
* Compare the current object to the passed $other.
*
* Returns 0 if they are semantically equal, 1 if the other object
* is less than the current one, or -1 if its more than the current one.
*
* This method should not check for identity using ===, only for semantically equality for example
* when two different DateTime instances point to the exact same Date + TZ.
*
* @param mixed $other Object
*
* @throws \InvalidArgumentException
* @return int
*/
public function compareTo($other)
{
if (!$other instanceof \DateTime) {
throw new \InvalidArgumentException('RepeatedIntervalParam can compare only DateTime');
}
$ret = -1;
$dates = array();
/** @var $value \DateTime */
foreach ($this->datePeriod as $value) {
$dates[$value->getTimestamp()]['startDate'] = $value;
$endDate = new \DateTime();
$dates[$value->getTimestamp()]['endDate'] = $endDate->setTimestamp(
$value->getTimestamp() + ($this->durationInDays * 60 *60 *24)
);
}
foreach ($dates as $date) {
if ($date['startDate'] <= $other && $other <= $date['endDate']) {
return 0;
}
}
return $ret;
}
/**
* Get Parameter value to test against
*
* @return \DatePeriod
*/
public function getValue()
{
return clone $this->datePeriod;
}
/**
* Get I18n tooltip
*
* @return string
*/
public function getToolTip()
{
return $this->adapter
->getTranslator()
->trans('A date (ex: YYYY-MM-DD HH:MM:SS)', null, 'constraint');
}
}

View File

@@ -0,0 +1,297 @@
<?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\Validator;
use DateInterval;
use DatePeriod;
use DateTime;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Allow to set the way a parameter can be repeated across the time
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
abstract class RepeatedParam extends RuleParameterAbstract
{
/** @var DateTime The start date of the period. */
protected $from = null;
/** @var DateInterval The interval between recurrences within the period. */
protected $interval = null;
/** @var int Nb time the object will be repeated (1st occurrence excluded). */
protected $recurrences = null;
/** @var DatePeriod dates recurring at regular intervals, over a given period */
protected $datePeriod = null;
/** @var int Frequency the object will be repeated */
protected $frequency = null;
/** @var int $nbRepetition Time the object will be repeated */
protected $nbRepetition = null;
/**
* Get frequency
*
* @return int
*/
public function getFrequency()
{
return $this->frequency;
}
/**
* Get Interval
*
* @return \DateInterval
*/
public function getInterval()
{
return $this->interval;
}
/**
* Get number of time it will be repeated
*
* @return int
*/
public function getNbRepetition()
{
return $this->nbRepetition;
}
/**
* Get number of recurrences
*
* @return int
*/
public function getRecurrences()
{
return $this->recurrences;
}
/**
* Generate default repetition
* Every 1 week 100 times from now
*
* @return $this
*/
protected function defaultConstructor()
{
$this->from = new \DateTime();
$this->interval = new \DateInterval('P1W'); // 1 week
$this->recurrences = 100;
$this->generateDatePeriod();
return $this;
}
/**
* Generate DatePeriod from class attributes
* Will repeat every DatePeriod
*
* @return $this
*/
protected function generateDatePeriod()
{
$this->datePeriod = new DatePeriod(
$this->from,
$this->interval,
$this->recurrences
);
return $this;
}
/**
* Set the Object to be repeated every days
* Ex : $obj->repeatEveryDay() will occur once
* $obj->repeatEveryDay(10) will occur once
* $obj->repeatEveryDay(10, 0) will occur once
* $obj->repeatEveryDay(10, 4) will occur every 10 days 5 times
*
* @param int $frequency Frequency the object will be repeated
* @param int $nbRepetition Time the object will be repeated
*
* @return $this
*/
public function repeatEveryDay($frequency = 1, $nbRepetition = 0)
{
$this->_repeatEveryPeriod($period = 'D', $frequency, $nbRepetition);
return $this;
}
/**
* Set the Object to be repeated every week
* Ex : $obj->repeatEveryWeek() will occur once
* $obj->repeatEveryWeek(10) will occur once
* $obj->repeatEveryWeek(10, 0) will occur once
* $obj->repeatEveryWeek(10, 4) will occur every 10 weeks (70days) 5 times
*
* @param int $frequency Frequency the object will be repeated
* @param int $nbRepetition Time the object will be repeated
*
* @return $this
*/
public function repeatEveryWeek($frequency = 1, $nbRepetition = 0)
{
$this->_repeatEveryPeriod($period = 'W', $frequency, $nbRepetition);
return $this;
}
/**
* Set the Object to be repeated every month
* Ex : $obj->repeatEveryWeek() will occur once
* $obj->repeatEveryWeek(10) will occur once
* $obj->repeatEveryWeek(10, 0) will occur once
* $obj->repeatEveryWeek(10, 4) will occur every 10 month (70days) 5times
*
* @param int $frequency Frequency the object will be repeated
* @param int $nbRepetition Time the object will be repeated
*
* @return $this
*/
public function repeatEveryMonth($frequency = 1, $nbRepetition = 0)
{
$this->_repeatEveryPeriod($period = 'M', $frequency, $nbRepetition);
return $this;
}
/**
* Set the Object to be repeated every year
* Ex : $obj->repeatEveryWeek() will occur once
* $obj->repeatEveryWeek(10) will occur once
* $obj->repeatEveryWeek(10, 0) will occur once
* $obj->repeatEveryWeek(10, 4) will occur every 10 year 5 times
*
* @param int $frequency Frequency the object will be repeated
* @param int $nbRepetition Time the object will be repeated
*
* @return $this
*/
public function repeatEveryYear($frequency = 1, $nbRepetition = 0)
{
$this->_repeatEveryPeriod($period = 'Y', $frequency, $nbRepetition);
return $this;
}
/**
* Set the Object to be repeated every Period
* Ex : $obj->repeatEveryPeriod('D') will occur once
* $obj->repeatEveryPeriod('W', 10) will occur once
* $obj->repeatEveryPeriod('W', 10, 0) will occur once
* $obj->repeatEveryPeriod('M', 10, 4) will occur every 10 month 5 times
*
* @param string $period Period Y|M||D|W
* @param int $frequency Frequency the object will be repeated
* @param int $nbRepetition Time the object will be repeated
*
* @return $this
*/
private function _repeatEveryPeriod($period, $frequency = 1, $nbRepetition = 0)
{
if (is_numeric($frequency) && $frequency > 0) {
$this->interval = new \DateInterval('P' . $frequency . $period);
}
if (is_numeric($nbRepetition) && $nbRepetition >= 0) {
$this->recurrences = $nbRepetition;
}
$this->generateDatePeriod();
return $this;
}
/**
* Set Start time
*
* @param \DateTime $from Start time
*
* @return $this
*/
public function setFrom($from)
{
$this->from = $from;
return $this;
}
/**
* Get Start time
*
* @return \DateTime
*/
public function getFrom()
{
return clone $this->from;
}
/**
* Set DatePeriod
*
* @param DatePeriod $datePeriod DatePeriod
*
* @return $this
*/
public function setDatePeriod(DatePeriod $datePeriod)
{
$this->datePeriod = $datePeriod;
return $this;
}
/**
* Get date DatePeriod
*
* @return \DatePeriod
*/
public function getDatePeriod()
{
return clone $this->datePeriod;
}
/**
* Get Parameter value to test against
*
* @return \DatePeriod
*/
public function getValue()
{
return clone $this->datePeriod;
}
}

View File

@@ -0,0 +1,66 @@
<?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\Validator;
use Thelia\Coupon\CouponAdapterInterface;
use Thelia\Exception\NotImplementedException;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Get a Param value
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
abstract class RuleParameterAbstract implements ComparableInterface
{
/** @var CouponAdapterInterface Provide necessary value from Thelia*/
protected $adapter;
/**
* Get Parameter value to test against
*
* @return mixed
*/
public function getValue()
{
return new NotImplementedException();
}
/**
* Get I18n tooltip
*
* @return string
*/
public function getToolTip()
{
return new NotImplementedException();
}
}

View File

@@ -0,0 +1,77 @@
<?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\Validator;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Allow to validate parameters
*
* @package Constraint
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class RuleValidator
{
/** @var string Operator ex: Operators::INFERIOR */
protected $operator = null;
/** @var ComparableInterface Validator */
protected $param = null;
/**
* Constructor
*
* @param string $operator Operator ex: Operators::INFERIOR
* @param ComparableInterface $param Validator ex: PriceParam
*/
function __construct($operator, ComparableInterface $param)
{
$this->operator = $operator;
$this->param = $param;
}
/**
* Get Validator Operator
*
* @return string
*/
public function getOperator()
{
return $this->operator;
}
/**
* Get Validator Param
*
* @return ComparableInterface
*/
public function getParam()
{
return $this->param;
}
}