WIP Coupon

Implementation CouponInterface 1st class : RemoveXAmount
Implementation CouponRuleInterface 1st class : AvailableForTotalAmount
This commit is contained in:
gmorel
2013-08-21 20:03:03 +02:00
parent df08158cff
commit c02e286d09
56 changed files with 3919 additions and 854 deletions

View File

@@ -1,25 +1,25 @@
<?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/>. */
/* */
/*************************************************************************************/
/**********************************************************************************/
/* */
/* 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\Coupon\Rule;
@@ -34,5 +34,31 @@ namespace Thelia\Coupon\Rule;
*/
class AvailableForDate extends AvailableForPeriod
{
/**
* Generate current Rule validator from adapter
*
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @throws \Symfony\Component\Intl\Exception\NotImplementedException
* @return $this
*/
protected function setValidators(CouponAdapterInterface $adapter)
{
parent::setValidators($adapter); // TODO: Change the autogenerated stub
}
/**
* 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
*/
protected function setParametersToValidate(CouponAdapterInterface $adapter)
{
parent::setParametersToValidate($adapter); // TODO: Change the autogenerated stub
}
}

View File

@@ -1,25 +1,25 @@
<?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/>. */
/* */
/*************************************************************************************/
/**********************************************************************************/
/* */
/* 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\Coupon\Rule;
@@ -34,5 +34,31 @@ namespace Thelia\Coupon\Rule;
*/
class AvailableForLocationX extends CouponRuleAbstract
{
/**
* Generate current Rule validator from adapter
*
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @throws \Symfony\Component\Intl\Exception\NotImplementedException
* @return $this
*/
protected function setValidators(CouponAdapterInterface $adapter)
{
parent::setValidators($adapter); // TODO: Change the autogenerated stub
}
/**
* 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
*/
protected function setParametersToValidate(CouponAdapterInterface $adapter)
{
parent::setParametersToValidate($adapter); // TODO: Change the autogenerated stub
}
}

View File

@@ -1,25 +1,25 @@
<?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/>. */
/* */
/*************************************************************************************/
/**********************************************************************************/
/* */
/* 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\Coupon\Rule;
@@ -34,5 +34,31 @@ namespace Thelia\Coupon\Rule;
*/
class AvailableForPeriod extends CouponRuleAbstract
{
/**
* Generate current Rule validator from adapter
*
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @throws \Symfony\Component\Intl\Exception\NotImplementedException
* @return $this
*/
protected function setValidators(CouponAdapterInterface $adapter)
{
parent::setValidators($adapter); // TODO: Change the autogenerated stub
}
/**
* 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
*/
protected function setParametersToValidate(CouponAdapterInterface $adapter)
{
parent::setParametersToValidate($adapter); // TODO: Change the autogenerated stub
}
}

View File

@@ -1,25 +1,25 @@
<?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/>. */
/* */
/*************************************************************************************/
/**********************************************************************************/
/* */
/* 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\Coupon\Rule;
@@ -34,5 +34,31 @@ namespace Thelia\Coupon\Rule;
*/
class AvailableForRepeatedDate extends AvailableForDate
{
/**
* Generate current Rule validator from adapter
*
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @throws \Symfony\Component\Intl\Exception\NotImplementedException
* @return $this
*/
protected function setValidators(CouponAdapterInterface $adapter)
{
parent::setValidators($adapter); // TODO: Change the autogenerated stub
}
/**
* 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
*/
protected function setParametersToValidate(CouponAdapterInterface $adapter)
{
parent::setParametersToValidate($adapter); // TODO: Change the autogenerated stub
}
}

View File

@@ -1,25 +1,25 @@
<?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/>. */
/* */
/*************************************************************************************/
/**********************************************************************************/
/* */
/* 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\Coupon\Rule;
@@ -34,5 +34,31 @@ namespace Thelia\Coupon\Rule;
*/
class AvailableForRepeatedPeriod extends AvailableForPeriod
{
/**
* Generate current Rule validator from adapter
*
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @throws \Symfony\Component\Intl\Exception\NotImplementedException
* @return $this
*/
protected function setValidators(CouponAdapterInterface $adapter)
{
parent::setValidators($adapter); // TODO: Change the autogenerated stub
}
/**
* 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
*/
protected function setParametersToValidate(CouponAdapterInterface $adapter)
{
parent::setParametersToValidate($adapter); // TODO: Change the autogenerated stub
}
}

View File

@@ -1,38 +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/>. */
/* */
/*************************************************************************************/
/**********************************************************************************/
/* */
/* 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\Coupon\Rule;
use Symfony\Component\Intl\Exception\NotImplementedException;
use Thelia\Coupon\CouponAdapterInterface;
use Thelia\Coupon\Parameter\PriceParam;
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 Coupon
* @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 PriceParam Price Validator */
protected $priceValidator = null;
/**
* Constructor
*
* @param array $validators Parameters validating $paramsToValidate against
* @param array $validated Parameters to be paramsToValidate
*/
public function __construct(array $validators, array $validated = null)
{
parent::__construct($validators, $validated);
$this->priceValidator = $validators[self::PARAM1_PRICE][self::VALUE];
}
/**
* 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][self::VALUE])
||!$this->validators[self::PARAM1_PRICE][self::VALUE] instanceof PriceParam
) {
throw new InvalidRuleValueException(get_class(), self::PARAM1_PRICE);
}
$this->checkBackOfficeInputsOperators();
/** @var PriceParam $price */
$price = $this->validators[self::PARAM1_PRICE][self::VALUE];
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 int $price Price to check
*
* @throws InvalidRuleValueException if Value is not allowed
* @return bool
*/
protected function isPriceValid($price)
{
$priceValidator = $this->priceValidator;
try {
$priceValidator->compareTo($price);
} catch(\InvalidArgumentException $e) {
throw new InvalidRuleValueException(get_class(), self::PARAM1_PRICE);
}
return true;
}
/**
* Generate current Rule validator from adapter
*
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @throws \Symfony\Component\Intl\Exception\NotImplementedException
* @return $this
*/
protected function setValidators(CouponAdapterInterface $adapter)
{
$adapter->getRule($this);
}
/**
* 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->getCheckoutTotalPrice()
);
return $this;
}
}

View File

@@ -1,25 +1,25 @@
<?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/>. */
/* */
/*************************************************************************************/
/**********************************************************************************/
/* */
/* 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\Coupon\Rule;
@@ -34,5 +34,31 @@ namespace Thelia\Coupon\Rule;
*/
class AvailableForTotalAmountForCategoryY extends AvailableForTotalAmount
{
/**
* Generate current Rule validator from adapter
*
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @throws \Symfony\Component\Intl\Exception\NotImplementedException
* @return $this
*/
protected function setValidators(CouponAdapterInterface $adapter)
{
parent::setValidators($adapter); // TODO: Change the autogenerated stub
}
/**
* 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
*/
protected function setParametersToValidate(CouponAdapterInterface $adapter)
{
parent::setParametersToValidate($adapter); // TODO: Change the autogenerated stub
}
}

View File

@@ -1,25 +1,25 @@
<?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/>. */
/* */
/*************************************************************************************/
/**********************************************************************************/
/* */
/* 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\Coupon\Rule;
@@ -39,39 +39,31 @@ use Thelia\Type\IntType;
class AvailableForXArticles extends CouponRuleAbstract
{
/**
* @inheritdoc
* Generate current Rule validator from adapter
*
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @throws \Symfony\Component\Intl\Exception\NotImplementedException
* @return $this
*/
public function checkBackOfficeIntput()
protected function setValidators(CouponAdapterInterface $adapter)
{
$ret = false;
$validator = new IntType();
$firstParam = reset($this->validators);
if ($firstParam) {
$ret = $validator->isValid($firstParam);
}
return $ret;
parent::setValidators($adapter); // TODO: Change the autogenerated stub
}
public function checkCheckoutInput()
/**
* 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
*/
protected function setParametersToValidate(CouponAdapterInterface $adapter)
{
$ret = false;
$validator = new IntType();
$firstParam = reset($this->validated);
if ($firstParam) {
$ret = $validator->isValid($firstParam);
}
return $ret;
}
public function isMatching()
{
if ($this->checkBackOfficeIntput() && $this->checkCheckoutInput()) {
$firstValidatorsParam = reset($this->validators);
$firstValidatedParam = reset($this->validated);
// if($firstValidatedParam >= $firstValidatedParam)
}
parent::setParametersToValidate($adapter); // TODO: Change the autogenerated stub
}
}

View File

@@ -1,28 +1,33 @@
<?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/>. */
/* */
/*************************************************************************************/
/**********************************************************************************/
/* */
/* 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\Coupon\Rule;
use Symfony\Component\Intl\Exception\NotImplementedException;
use Thelia\Coupon\CouponAdapterInterface;
use Thelia\Coupon\Parameter\ComparableInterface;
use Thelia\Exception\InvalidRuleOperatorException;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
@@ -34,54 +39,124 @@ namespace Thelia\Coupon\Rule;
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
class CouponRuleAbstract implements CuponRuleInterface
abstract class CouponRuleAbstract implements CouponRuleInterface
{
/** @var array Parameters validating $validated against */
/** 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 $validated = array();
protected $paramsToValidate = array();
/**
* Constructor
* Ex:
* Param 1 :
* $validators['price']['operator'] = Operators::INFERIOR
* ['value'] = new IntegerParam(10)
*
* @param array $validators Parameters validating $validated against
* @param array $validated Parameters to be validated
* Param 2 :
* $paramsToValidate['price'] = 9
*
* @param array $validators Parameters validating $paramsToValidate against
* @param array $validated Parameters to be paramsToValidate
*/
public function __construct(array $validators, array $validated)
public function __construct(array $validators, array $validated = null)
{
$this->validators = $validators;
$this->validated = $validated;
$this->paramsToValidate = $validated;
}
/**
* Check if backoffice inputs are relevant or not
*
* @return bool
*/
public function checkBackOfficeIntput()
{
// TODO: Implement checkBackOfficeIntput() method.
}
/**
* Check if Checkout inputs are relevant or not
*
* @return bool
*/
public function checkCheckoutInput()
{
// TODO: Implement checkCheckoutInput() method.
}
/**
* Check if the current Checkout matchs this condition
* Check if the current Checkout matches this condition
*
* @return bool
*/
public function isMatching()
{
// TODO: Implement isMatching() method.
$this->checkBackOfficeInput();
$this->checkCheckoutInput();
$isMatching = true;
foreach ($this->validators as $param => $validator) {
$a = $this->paramsToValidate[$param];
$operator = $validator[self::OPERATOR];
/** @var ComparableInterface $b */
$b = $validator[self::VALUE];
if (!Operators::isValidAccordingToOperator($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()
{
foreach ($this->validators as $key => $param) {
if (!isset($param[self::OPERATOR])
||!in_array($param[self::OPERATOR], $this->availableOperators)
) {
throw new InvalidRuleOperatorException(get_class(), $key);
}
}
return true;
}
/**
* Generate current Rule validator from adapter
*
* @param CouponAdapterInterface $adapter allowing to gather
* all necessary Thelia variables
*
* @throws \Symfony\Component\Intl\Exception\NotImplementedException
* @return $this
*/
protected function setValidators(CouponAdapterInterface $adapter)
{
throw new NotImplementedException(
'CouponRuleInterface::setValidators needs to be implemented'
);
}
/**
* 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
*/
protected function setParametersToValidate(CouponAdapterInterface $adapter)
{
throw new NotImplementedException(
'CouponRuleInterface::setValidators needs to be implemented'
);
}
}

View File

@@ -1,28 +1,30 @@
<?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/>. */
/* */
/*************************************************************************************/
/**********************************************************************************/
/* */
/* 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\Coupon\Rule;
use Thelia\Coupon\CouponAdapterInterface;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
@@ -34,14 +36,14 @@ namespace Thelia\Coupon\Rule;
* @author Guillaume MOREL <gmorel@openstudio.fr>
*
*/
interface CuponRuleInterface
interface CouponRuleInterface
{
/**
* Check if backoffice inputs are relevant or not
*
* @return bool
*/
public function checkBackOfficeIntput();
public function checkBackOfficeInput();
/**
* Check if Checkout inputs are relevant or not
@@ -51,10 +53,39 @@ interface CuponRuleInterface
public function checkCheckoutInput();
/**
* Check if the current Checkout matchs this condition
* Check if the current Checkout matches this condition
*
* @return bool
*/
public function isMatching();
/**
* Return all available Operators for this Rule
*
* @return array Operators::CONST
*/
public function getAvailableOperators();
// /**
// * Generate current Rule validator from adapter
// * Ex :
// * $validator = array(
// *
// * @param CouponAdapterInterface $adapter allowing to gather
// * all necessary Thelia variables
// *
// * @return array Validators : array of ComparableInterface
// */
// public function getValidators(CouponAdapterInterface $adapter);
//
// /**
// * Retrieve all param to validate from adapter
// *
// * @param CouponAdapterInterface $adapter allowing to gather
// * all necessary Thelia variables
// *
// * @return array Validators : array of ComparableInterface
// */
// public function getParamToValidate(CouponAdapterInterface $adapter);
}

View File

@@ -0,0 +1,109 @@
<?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\Coupon\Rule;
use Thelia\Coupon\Parameter\ComparableInterface;
/**
* Created by JetBrains PhpStorm.
* Date: 8/19/13
* Time: 3:24 PM
*
* Represent available Operations in rule checking
*
* @package Coupon
* @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 isValidAccordingToOperator($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;
}
}