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:
@@ -131,4 +131,12 @@ interface CouponAdapterInterface
|
||||
*/
|
||||
public function getTranslator();
|
||||
|
||||
/**
|
||||
* Return the main currency
|
||||
* THe one used to set prices in BackOffice
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMainCurrency();
|
||||
|
||||
}
|
||||
@@ -179,7 +179,7 @@ class CouponBaseAdapter implements CouponAdapterInterface
|
||||
*/
|
||||
public function getContainer()
|
||||
{
|
||||
// TODO: Implement getCheckoutPostagePrice() method.
|
||||
// TODO: Implement getContainer() method.
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -191,4 +191,16 @@ class CouponBaseAdapter implements CouponAdapterInterface
|
||||
{
|
||||
return $this->getContainer()->get('thelia.translator');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the main currency
|
||||
* THe one used to set prices in BackOffice
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMainCurrency()
|
||||
{
|
||||
// TODO: Implement getMainCurrency() method.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ use Thelia\Coupon\Type\CouponInterface;
|
||||
*/
|
||||
class CouponManager
|
||||
{
|
||||
/** @var CouponAdapterInterface Provide necessary value from Thelia*/
|
||||
/** @var CouponAdapterInterface Provides necessary value from Thelia */
|
||||
protected $adapter;
|
||||
|
||||
/** @var array CouponInterface to process*/
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
namespace Thelia\Coupon;
|
||||
|
||||
use Symfony\Component\Serializer\Encoder\JsonEncoder;
|
||||
use Thelia\Coupon\Rule\CouponRuleInterface;
|
||||
use Thelia\Constraint\Rule\CouponRuleInterface;
|
||||
use Thelia\Exception\InvalidRuleException;
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Coupon\Rule;
|
||||
|
||||
/**
|
||||
* Created by JetBrains PhpStorm.
|
||||
* Date: 8/19/13
|
||||
* Time: 3:24 PM
|
||||
*
|
||||
* @package Coupon
|
||||
* @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.
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Coupon\Rule;
|
||||
|
||||
/**
|
||||
* Created by JetBrains PhpStorm.
|
||||
* Date: 8/19/13
|
||||
* Time: 3:24 PM
|
||||
*
|
||||
* @package Coupon
|
||||
* @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.
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Coupon\Rule;
|
||||
|
||||
/**
|
||||
* Created by JetBrains PhpStorm.
|
||||
* Date: 8/19/13
|
||||
* Time: 3:24 PM
|
||||
*
|
||||
* @package Coupon
|
||||
* @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.
|
||||
}
|
||||
}
|
||||
@@ -1,57 +0,0 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Coupon\Rule;
|
||||
|
||||
/**
|
||||
* Created by JetBrains PhpStorm.
|
||||
* Date: 8/19/13
|
||||
* Time: 3:24 PM
|
||||
*
|
||||
* @package Coupon
|
||||
* @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.
|
||||
}
|
||||
}
|
||||
@@ -1,73 +0,0 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Coupon\Rule;
|
||||
|
||||
use Thelia\Coupon\CouponAdapterInterface;
|
||||
|
||||
/**
|
||||
* Created by JetBrains PhpStorm.
|
||||
* Date: 8/19/13
|
||||
* Time: 3:24 PM
|
||||
*
|
||||
* @package Coupon
|
||||
* @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.
|
||||
}
|
||||
}
|
||||
@@ -1,186 +0,0 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Coupon\Rule;
|
||||
|
||||
use Symfony\Component\Intl\Exception\NotImplementedException;
|
||||
use Thelia\Coupon\CouponAdapterInterface;
|
||||
use Thelia\Coupon\Validator\PriceParam;
|
||||
use Thelia\Coupon\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 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 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Coupon\Rule;
|
||||
|
||||
/**
|
||||
* Created by JetBrains PhpStorm.
|
||||
* Date: 8/19/13
|
||||
* Time: 3:24 PM
|
||||
*
|
||||
* @package Coupon
|
||||
* @author Guillaume MOREL <gmorel@openstudio.fr>
|
||||
*
|
||||
*/
|
||||
class AvailableForTotalAmountForCategoryY extends AvailableForTotalAmount
|
||||
{
|
||||
|
||||
}
|
||||
@@ -1,60 +0,0 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Coupon\Rule;
|
||||
|
||||
/**
|
||||
* Created by JetBrains PhpStorm.
|
||||
* Date: 8/19/13
|
||||
* Time: 3:24 PM
|
||||
*
|
||||
* Check a Checkout against its Product number
|
||||
*
|
||||
* @package Coupon
|
||||
* @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.
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,179 +0,0 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Coupon\Rule;
|
||||
|
||||
use Symfony\Component\Intl\Exception\NotImplementedException;
|
||||
use Thelia\Coupon\CouponAdapterInterface;
|
||||
use Thelia\Coupon\Validator\ComparableInterface;
|
||||
use Thelia\Coupon\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 Coupon
|
||||
* @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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Coupon\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 Coupon
|
||||
* @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();
|
||||
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Coupon\Rule;
|
||||
|
||||
use Thelia\Coupon\Validator\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 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;
|
||||
}
|
||||
}
|
||||
@@ -24,8 +24,8 @@
|
||||
namespace Thelia\Coupon\Type;
|
||||
|
||||
use Symfony\Component\Intl\Exception\NotImplementedException;
|
||||
use Thelia\Constraint\ConstraintManager;
|
||||
use Thelia\Coupon\CouponAdapterInterface;
|
||||
use Thelia\Coupon\Rule\CouponRuleInterface;
|
||||
use Thelia\Coupon\CouponRuleCollection;
|
||||
use Thelia\Coupon\RuleOrganizerInterface;
|
||||
use Thelia\Exception\InvalidRuleException;
|
||||
@@ -43,12 +43,18 @@ use Thelia\Exception\InvalidRuleException;
|
||||
*/
|
||||
abstract class CouponAbstract implements CouponInterface
|
||||
{
|
||||
/** @var CouponAdapterInterface Provides necessary value from Thelia */
|
||||
protected $adapter = null;
|
||||
|
||||
/** @var RuleOrganizerInterface */
|
||||
protected $organizer = null;
|
||||
|
||||
/** @var CouponRuleCollection Array of CouponRuleInterface */
|
||||
protected $rules = null;
|
||||
|
||||
/** @var ConstraintManager CouponRuleInterface Manager*/
|
||||
protected $constraintManager = null;
|
||||
|
||||
/** @var string Coupon code (ex: XMAS) */
|
||||
protected $code = null;
|
||||
|
||||
@@ -194,6 +200,10 @@ abstract class CouponAbstract implements CouponInterface
|
||||
public function setRules(CouponRuleCollection $rules)
|
||||
{
|
||||
$this->rules = $rules;
|
||||
$this->constraintManager = new ConstraintManager(
|
||||
$this->adapter,
|
||||
$this->rules
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -209,16 +219,7 @@ abstract class CouponAbstract implements CouponInterface
|
||||
*/
|
||||
public function isMatching(CouponAdapterInterface $adapter)
|
||||
{
|
||||
$isMatching = true;
|
||||
|
||||
/** @var CouponRuleInterface $rule */
|
||||
foreach ($this->rules->getRules() as $rule) {
|
||||
if (!$rule->isMatching($adapter)) {
|
||||
$isMatching = false;
|
||||
}
|
||||
}
|
||||
|
||||
return $isMatching;
|
||||
return $this->constraintManager->isMatching();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -165,4 +165,18 @@ interface CouponInterface
|
||||
* @return bool
|
||||
*/
|
||||
public function isExpired();
|
||||
|
||||
/**
|
||||
* Get I18n name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName();
|
||||
|
||||
/**
|
||||
* Get I18n tooltip
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getToolTip();
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
|
||||
namespace Thelia\Coupon\Type;
|
||||
|
||||
use Thelia\Constraint\ConstraintManager;
|
||||
use Thelia\Coupon\Type\CouponAbstract;
|
||||
|
||||
/**
|
||||
@@ -41,20 +42,34 @@ class RemoveXAmount extends CouponAbstract
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $code Coupon code (ex: XMAS)
|
||||
* @param string $title Coupon title (ex: Coupon for XMAS)
|
||||
* @param string $shortDescription Coupon short description
|
||||
* @param string $description Coupon description
|
||||
* @param float $amount Coupon amount to deduce
|
||||
* @param bool $isCumulative If Coupon is cumulative
|
||||
* @param bool $isRemovingPostage If Coupon is removing postage
|
||||
* @param bool $isAvailableOnSpecialOffers If available on Product already
|
||||
* on special offer price
|
||||
* @param bool $isEnabled False if Coupon is disabled by admin
|
||||
* @param int $maxUsage How many usage left
|
||||
* @param \Datetime $expirationDate When the Code is expiring
|
||||
* @param CouponInterface $adapter Provides necessary value from Thelia
|
||||
* @param string $code Coupon code (ex: XMAS)
|
||||
* @param string $title Coupon title (ex: Coupon for XMAS)
|
||||
* @param string $shortDescription Coupon short description
|
||||
* @param string $description Coupon description
|
||||
* @param float $amount Coupon amount to deduce
|
||||
* @param bool $isCumulative If Coupon is cumulative
|
||||
* @param bool $isRemovingPostage If Coupon is removing postage
|
||||
* @param bool $isAvailableOnSpecialOffers If available on Product already
|
||||
* on special offer price
|
||||
* @param bool $isEnabled False if Coupon is disabled by admin
|
||||
* @param int $maxUsage How many usage left
|
||||
* @param \Datetime $expirationDate When the Code is expiring
|
||||
*/
|
||||
function __construct($code, $title, $shortDescription, $description, $amount, $isCumulative, $isRemovingPostage, $isAvailableOnSpecialOffers, $isEnabled, $maxUsage, \DateTime $expirationDate)
|
||||
function __construct(
|
||||
$adapter,
|
||||
$code,
|
||||
$title,
|
||||
$shortDescription,
|
||||
$description,
|
||||
$amount,
|
||||
$isCumulative,
|
||||
$isRemovingPostage,
|
||||
$isAvailableOnSpecialOffers,
|
||||
$isEnabled,
|
||||
$maxUsage,
|
||||
\DateTime $expirationDate
|
||||
)
|
||||
{
|
||||
$this->code = $code;
|
||||
$this->title = $title;
|
||||
@@ -70,6 +85,36 @@ class RemoveXAmount extends CouponAbstract
|
||||
$this->isEnabled = $isEnabled;
|
||||
$this->maxUsage = $maxUsage;
|
||||
$this->expirationDate = $expirationDate;
|
||||
$this->adapter = $adapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get I18n name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->adapter
|
||||
->getTranslator()
|
||||
->trans('Remove X amount to total cart', null, 'constraint');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get I18n tooltip
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getToolTip()
|
||||
{
|
||||
$toolTip = $this->adapter
|
||||
->getTranslator()
|
||||
->trans(
|
||||
'This coupon will remove the entered amount to the customer total checkout. If the discount is superior to the total checkout price the customer will only pay the postage. Unless if the coupon is set to remove postage too.',
|
||||
null,
|
||||
'constraint'
|
||||
);
|
||||
|
||||
return $toolTip;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,15 +43,33 @@ class RemoveXPercent extends CouponAbstract
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $code Coupon code (ex: XMAS)
|
||||
* @param string $title Coupon title (ex: Coupon for XMAS)
|
||||
* @param string $shortDescription Coupon short description
|
||||
* @param string $description Coupon description
|
||||
* @param float $percent Coupon % to deduce (ex:3.14 for 3.14%)
|
||||
* @param bool $isCumulative if Coupon is cumulative
|
||||
* @param bool $isRemovingPostage if Coupon is removing postage
|
||||
* @param CouponInterface $adapter Provides necessary value from Thelia
|
||||
* @param string $code Coupon code (ex: XMAS)
|
||||
* @param string $title Coupon title (ex: Coupon for XMAS)
|
||||
* @param string $shortDescription Coupon short description
|
||||
* @param string $description Coupon description
|
||||
* @param float $percent Coupon percentage to deduce
|
||||
* @param bool $isCumulative If Coupon is cumulative
|
||||
* @param bool $isRemovingPostage If Coupon is removing postage
|
||||
* @param bool $isAvailableOnSpecialOffers If available on Product already
|
||||
* on special offer price
|
||||
* @param bool $isEnabled False if Coupon is disabled by admin
|
||||
* @param int $maxUsage How many usage left
|
||||
* @param \Datetime $expirationDate When the Code is expiring
|
||||
*/
|
||||
function __construct($code, $title, $shortDescription, $description, $percent, $isCumulative, $isRemovingPostage)
|
||||
function __construct(
|
||||
$adapter,
|
||||
$code,
|
||||
$title,
|
||||
$shortDescription,
|
||||
$description,
|
||||
$percent,
|
||||
$isCumulative,
|
||||
$isRemovingPostage,
|
||||
$isAvailableOnSpecialOffers,
|
||||
$isEnabled,
|
||||
$maxUsage,
|
||||
\DateTime $expirationDate)
|
||||
{
|
||||
$this->code = $code;
|
||||
$this->title = $title;
|
||||
@@ -62,6 +80,12 @@ class RemoveXPercent extends CouponAbstract
|
||||
$this->isRemovingPostage = $isRemovingPostage;
|
||||
|
||||
$this->percent = $percent;
|
||||
|
||||
$this->isAvailableOnSpecialOffers = $isAvailableOnSpecialOffers;
|
||||
$this->isEnabled = $isEnabled;
|
||||
$this->maxUsage = $maxUsage;
|
||||
$this->expirationDate = $expirationDate;
|
||||
$this->adapter = $adapter;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,10 +107,40 @@ class RemoveXPercent extends CouponAbstract
|
||||
);
|
||||
}
|
||||
|
||||
$basePrice =$adapter
|
||||
->getCartTotalPrice();
|
||||
$basePrice = $adapter->getCartTotalPrice();
|
||||
|
||||
return $basePrice * (( 100 - $this->percent ) / 100);
|
||||
return $basePrice * (( $this->percent ) / 100);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get I18n name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->adapter
|
||||
->getTranslator()
|
||||
->trans('Remove X percent to total cart', null, 'constraint');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get I18n tooltip
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getToolTip()
|
||||
{
|
||||
$toolTip = $this->adapter
|
||||
->getTranslator()
|
||||
->trans(
|
||||
'This coupon will remove the entered percentage to the customer total checkout. If the discount is superior to the total checkout price the customer will only pay the postage. Unless if the coupon is set to remove postage too.',
|
||||
null,
|
||||
'constraint'
|
||||
);
|
||||
|
||||
return $toolTip;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Coupon\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);
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Coupon\Validator;
|
||||
|
||||
/**
|
||||
* Created by JetBrains PhpStorm.
|
||||
* Date: 8/19/13
|
||||
* Time: 3:24 PM
|
||||
*
|
||||
* Represent a DateTime
|
||||
*
|
||||
* @package Coupon
|
||||
* @author Guillaume MOREL <gmorel@openstudio.fr>
|
||||
*
|
||||
*/
|
||||
class DateParam extends RuleParameterAbstract
|
||||
{
|
||||
/** @var \DateTime Date */
|
||||
protected $dateTime = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \DateTime $dateTime DateTime
|
||||
*/
|
||||
public function __construct(\DateTime $dateTime)
|
||||
{
|
||||
$this->dateTime = $dateTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,105 +0,0 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Coupon\Validator;
|
||||
|
||||
/**
|
||||
* Created by JetBrains PhpStorm.
|
||||
* Date: 8/19/13
|
||||
* Time: 3:24 PM
|
||||
*
|
||||
* Represent an Integer
|
||||
*
|
||||
* @package Coupon
|
||||
* @author Guillaume MOREL <gmorel@openstudio.fr>
|
||||
*
|
||||
*/
|
||||
class IntegerParam extends RuleParameterAbstract
|
||||
{
|
||||
/** @var int Integer to compare with */
|
||||
protected $integer = 0;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param int $integer Integer
|
||||
*/
|
||||
public function __construct($integer)
|
||||
{
|
||||
$this->integer = $integer;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,118 +0,0 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Coupon\Validator;
|
||||
|
||||
/**
|
||||
* Created by JetBrains PhpStorm.
|
||||
* Date: 8/19/13
|
||||
* Time: 3:24 PM
|
||||
*
|
||||
* Represent an DateTime period
|
||||
*
|
||||
* @package Coupon
|
||||
* @author Guillaume MOREL <gmorel@openstudio.fr>
|
||||
*
|
||||
*/
|
||||
class IntervalParam extends RuleParameterAbstract
|
||||
{
|
||||
/** @var \DatePeriod Date period */
|
||||
protected $datePeriod = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param \DateTime $start Start interval
|
||||
* @param \DateInterval $interval Period
|
||||
*/
|
||||
public function __construct(\DateTime $start, \DateInterval $interval)
|
||||
{
|
||||
$this->datePeriod = new \DatePeriod($start, $interval, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
@@ -1,123 +0,0 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Coupon\Validator;
|
||||
|
||||
/**
|
||||
* Created by JetBrains PhpStorm.
|
||||
* Date: 8/19/13
|
||||
* Time: 3:24 PM
|
||||
*
|
||||
* Represent a Price
|
||||
* Positive value with currency
|
||||
*
|
||||
* @package Coupon
|
||||
* @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 float $price Positive float
|
||||
* @param string $currency Currency Code ISO 4217 EUR|USD|GBP
|
||||
*/
|
||||
public function __construct($price, $currency)
|
||||
{
|
||||
$this->price = $price;
|
||||
$this->currency = $currency;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Coupon\Validator;
|
||||
|
||||
/**
|
||||
* Created by JetBrains PhpStorm.
|
||||
* Date: 8/19/13
|
||||
* Time: 3:24 PM
|
||||
*
|
||||
* Represent a Quantity
|
||||
*
|
||||
* @package Coupon
|
||||
* @author Guillaume MOREL <gmorel@openstudio.fr>
|
||||
*
|
||||
*/
|
||||
class QuantityParam extends IntegerParam
|
||||
{
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param int $integer Integer
|
||||
*/
|
||||
public function __construct($integer)
|
||||
{
|
||||
if ($integer < 0) {
|
||||
$integer = 0;
|
||||
}
|
||||
$this->integer = $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) || $other < 0) {
|
||||
throw new \InvalidArgumentException(
|
||||
'IntegerParam can compare only positive int'
|
||||
);
|
||||
}
|
||||
|
||||
return parent::compareTo($other);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Coupon\Validator;
|
||||
|
||||
/**
|
||||
* 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 Coupon
|
||||
* @author Guillaume MOREL <gmorel@openstudio.fr>
|
||||
*
|
||||
*/
|
||||
class RepeatedDateParam extends RepeatedParam
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->defaultConstructor();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
@@ -1,134 +0,0 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Coupon\Validator;
|
||||
|
||||
/**
|
||||
* 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 Coupon
|
||||
* @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
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->defaultConstructor();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
@@ -1,251 +0,0 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Coupon\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 Coupon
|
||||
* @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;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Coupon\Validator;
|
||||
|
||||
use Thelia\Exception\NotImplementedException;
|
||||
|
||||
/**
|
||||
* Created by JetBrains PhpStorm.
|
||||
* Date: 8/19/13
|
||||
* Time: 3:24 PM
|
||||
*
|
||||
* Get a Param value
|
||||
*
|
||||
* @package Coupon
|
||||
* @author Guillaume MOREL <gmorel@openstudio.fr>
|
||||
*
|
||||
*/
|
||||
abstract class RuleParameterAbstract implements ComparableInterface
|
||||
{
|
||||
/**
|
||||
* Get Parameter value to test against
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getValue()
|
||||
{
|
||||
return new NotImplementedException();
|
||||
}
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Coupon\Validator;
|
||||
|
||||
/**
|
||||
* Created by JetBrains PhpStorm.
|
||||
* Date: 8/19/13
|
||||
* Time: 3:24 PM
|
||||
*
|
||||
* Allow to validate parameters
|
||||
*
|
||||
* @package Coupon
|
||||
* @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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user