WIP
- Add Coupon, Rules, CouponManager, Adapter as Services - Refactor Coupon to use these services
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
namespace Thelia\Constraint\Rule;
|
||||
|
||||
use Symfony\Component\Intl\Exception\NotImplementedException;
|
||||
use Symfony\Component\Translation\Translator;
|
||||
use Thelia\Coupon\CouponAdapterInterface;
|
||||
use Thelia\Constraint\Validator\PriceParam;
|
||||
use Thelia\Constraint\Validator\RuleValidator;
|
||||
@@ -48,6 +49,9 @@ class AvailableForTotalAmount extends CouponRuleAbstract
|
||||
/** Rule 1st parameter : price */
|
||||
CONST PARAM1_PRICE = 'price';
|
||||
|
||||
/** Rule 1st parameter : currency */
|
||||
CONST PARAM1_CURRENCY = 'currency';
|
||||
|
||||
/** @var array Available Operators (Operators::CONST) */
|
||||
protected $availableOperators = array(
|
||||
Operators::INFERIOR,
|
||||
@@ -55,7 +59,7 @@ class AvailableForTotalAmount extends CouponRuleAbstract
|
||||
Operators::SUPERIOR,
|
||||
);
|
||||
|
||||
/** @var RuleValidator Price Validator */
|
||||
/** @var PriceParam Price Validator */
|
||||
protected $priceValidator = null;
|
||||
|
||||
/**
|
||||
@@ -185,9 +189,11 @@ class AvailableForTotalAmount extends CouponRuleAbstract
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->adapter
|
||||
->getTranslator()
|
||||
->trans('Cart total amount', null, 'constraint');
|
||||
return $this->adapter->get('thelia.translator')->trans(
|
||||
'Cart total amount',
|
||||
array(),
|
||||
'constraint'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -197,23 +203,73 @@ class AvailableForTotalAmount extends CouponRuleAbstract
|
||||
*/
|
||||
public function getToolTip()
|
||||
{
|
||||
/** @var Translator $translator */
|
||||
$translator = $this->get('thelia.translator');
|
||||
$i18nOperator = Operators::getI18n(
|
||||
$this->adapter, $this->priceValidator->getOperator()
|
||||
$translator, $this->priceValidator->getOperator()
|
||||
);
|
||||
|
||||
$toolTip = $this->adapter
|
||||
->getTranslator()
|
||||
->trans(
|
||||
'If cart total amount is %operator% %amount% %currency%',
|
||||
array(
|
||||
'%operator%' => $i18nOperator,
|
||||
'%amount%' => $this->priceValidator->getParam()->getPrice(),
|
||||
'%currency%' => $this->priceValidator->getParam()->getCurrency()
|
||||
),
|
||||
'constraint'
|
||||
);
|
||||
$toolTip = $translator->trans(
|
||||
'If cart total amount is <strong>%operator%</strong> %amount% %currency%',
|
||||
array(
|
||||
'%operator%' => $i18nOperator,
|
||||
'%amount%' => $this->priceValidator->getParam()->getPrice(),
|
||||
'%currency%' => $this->priceValidator->getParam()->getCurrency()
|
||||
),
|
||||
'constraint'
|
||||
);
|
||||
|
||||
return $toolTip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate a Rule from a form admin
|
||||
*
|
||||
* @param array $operators Rule Operator set by the Admin
|
||||
* @param array $values Rule Values set by the Admin
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
* @return $this
|
||||
*/
|
||||
public function populateFromForm(array $operators, array $values)
|
||||
{
|
||||
if ($values[self::PARAM1_PRICE] === null
|
||||
|| $values[self::PARAM1_CURRENCY] === null
|
||||
) {
|
||||
throw new InvalidArgumentException(
|
||||
'The Rule ' . get_class() . 'needs at least a quantity set (' . self::PARAM1_PRICE . ', ' . self::PARAM1_CURRENCY . ')'
|
||||
);
|
||||
}
|
||||
|
||||
$this->priceValidator = new PriceParam(
|
||||
$this->adapter,
|
||||
$values[self::PARAM1_PRICE],
|
||||
$values[self::PARAM1_CURRENCY]
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a serializable Rule
|
||||
*
|
||||
* @return SerializableRule
|
||||
*/
|
||||
public function getSerializableRule()
|
||||
{
|
||||
$serializableRule = new SerializableRule();
|
||||
$serializableRule->operators = array(
|
||||
self::PARAM1_PRICE => $this->priceValidator->getOperator()
|
||||
);
|
||||
|
||||
$serializableRule->values = array(
|
||||
self::PARAM1_PRICE => $this->priceValidator->getPrice(),
|
||||
self::PARAM1_CURRENCY => $this->priceValidator->getCurrency()
|
||||
);
|
||||
|
||||
return $serializableRule;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -23,6 +23,8 @@
|
||||
|
||||
namespace Thelia\Constraint\Rule;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Symfony\Component\Translation\Translator;
|
||||
use Thelia\Constraint\Validator\QuantityParam;
|
||||
use Thelia\Constraint\Validator\RuleValidator;
|
||||
use Thelia\Coupon\CouponAdapterInterface;
|
||||
@@ -51,7 +53,7 @@ class AvailableForXArticles extends CouponRuleAbstract
|
||||
Operators::SUPERIOR,
|
||||
);
|
||||
|
||||
/** @var RuleValidator Quantity Validator */
|
||||
/** @var QuantityParam Quantity Validator */
|
||||
protected $quantityValidator = null;
|
||||
|
||||
/**
|
||||
@@ -64,7 +66,7 @@ class AvailableForXArticles extends CouponRuleAbstract
|
||||
*
|
||||
* @throws InvalidRuleException
|
||||
*/
|
||||
public function __construct(CouponAdapterInterface $adapter, array $validators)
|
||||
public function __construct(CouponAdapterInterface $adapter, array $validators = null)
|
||||
{
|
||||
parent::__construct($adapter, $validators);
|
||||
|
||||
@@ -160,7 +162,7 @@ class AvailableForXArticles extends CouponRuleAbstract
|
||||
$quantityValidator = $this->quantityValidator;
|
||||
try {
|
||||
$quantityValidator->getParam()->compareTo($quantity);
|
||||
} catch(\InvalidArgumentException $e) {
|
||||
} catch(InvalidArgumentException $e) {
|
||||
throw new InvalidRuleValueException(get_class(), self::PARAM1_QUANTITY);
|
||||
}
|
||||
|
||||
@@ -174,9 +176,14 @@ class AvailableForXArticles extends CouponRuleAbstract
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->adapter
|
||||
->getTranslator()
|
||||
->trans('Number of articles in cart', null, 'constraint');
|
||||
/** @var Translator $translator */
|
||||
$translator = $this->adapter->get('thelia.translator');
|
||||
|
||||
return $translator->trans(
|
||||
'Number of articles in cart',
|
||||
array(),
|
||||
'constraint'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -186,22 +193,68 @@ class AvailableForXArticles extends CouponRuleAbstract
|
||||
*/
|
||||
public function getToolTip()
|
||||
{
|
||||
/** @var Translator $translator */
|
||||
$translator = $this->adapter->get('thelia.translator');
|
||||
|
||||
$i18nOperator = Operators::getI18n(
|
||||
$this->adapter, $this->priceValidator->getOperator()
|
||||
$translator, $this->priceValidator->getOperator()
|
||||
);
|
||||
|
||||
$toolTip = $this->adapter
|
||||
->getTranslator()
|
||||
->trans(
|
||||
'If cart products quantity is %operator% %quantity%',
|
||||
array(
|
||||
'%operator%' => $i18nOperator,
|
||||
'%quantity%' => $this->quantityValidator->getParam()->getInteger(),
|
||||
),
|
||||
'constraint'
|
||||
);
|
||||
$toolTip = $translator->trans(
|
||||
'If cart products quantity is <strong>%operator%</strong> %quantity%',
|
||||
array(
|
||||
'%operator%' => $i18nOperator,
|
||||
'%quantity%' => $this->quantityValidator->getParam()->getInteger(),
|
||||
),
|
||||
'constraint'
|
||||
);
|
||||
|
||||
return $toolTip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate a Rule from a form admin
|
||||
*
|
||||
* @param array $operators Rule Operator set by the Admin
|
||||
* @param array $values Rule Values set by the Admin
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
* @return $this
|
||||
*/
|
||||
public function populateFromForm(array $operators, array $values)
|
||||
{
|
||||
if ($values[self::PARAM1_QUANTITY] === null) {
|
||||
throw new InvalidArgumentException(
|
||||
'The Rule ' . get_class() . 'needs at least a quantity set (' . self::PARAM1_QUANTITY. ')'
|
||||
);
|
||||
}
|
||||
|
||||
$this->quantityValidator = new QuantityParam(
|
||||
$this->adapter,
|
||||
$values[self::PARAM1_QUANTITY]
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a serializable Rule
|
||||
*
|
||||
* @return SerializableRule
|
||||
*/
|
||||
public function getSerializableRule()
|
||||
{
|
||||
$serializableRule = new SerializableRule();
|
||||
$serializableRule->operators = array(
|
||||
self::PARAM1_QUANTITY => $this->quantityValidator->getOperator()
|
||||
);
|
||||
|
||||
$serializableRule->values = array(
|
||||
self::PARAM1_QUANTITY => $this->quantityValidator->getInteger()
|
||||
);
|
||||
|
||||
return $serializableRule;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -23,6 +23,7 @@
|
||||
|
||||
namespace Thelia\Constraint\Rule;
|
||||
|
||||
use Symfony\Component\Translation\Translator;
|
||||
use Thelia\Coupon\CouponAdapterInterface;
|
||||
|
||||
/**
|
||||
@@ -81,4 +82,31 @@ interface CouponRuleInterface
|
||||
*/
|
||||
public function getToolTip();
|
||||
|
||||
/**
|
||||
* Get validators
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getValidators();
|
||||
|
||||
/**
|
||||
* Populate a Rule from a form admin
|
||||
*
|
||||
* @param array $operators Rule Operator set by the Admin
|
||||
* @param array $values Rule Values set by the Admin
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function populateFromForm(array$operators, array $values);
|
||||
|
||||
|
||||
/**
|
||||
* Return a serializable Rule
|
||||
*
|
||||
* @return SerializableRule
|
||||
*/
|
||||
public function getSerializableRule();
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
|
||||
namespace Thelia\Constraint\Rule;
|
||||
|
||||
use Symfony\Component\Translation\Translator;
|
||||
use Thelia\Constraint\Validator\ComparableInterface;
|
||||
|
||||
/**
|
||||
@@ -110,56 +111,54 @@ abstract class Operators
|
||||
/**
|
||||
* Get operator translation
|
||||
*
|
||||
* @param CouponAdapterInterface $adapter Provide necessary value from Thelia
|
||||
* @param string $operator Operator const
|
||||
* @param Translator $translator Provide necessary value from Thelia
|
||||
* @param string $operator Operator const
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getI18n(CouponAdapterInterface $adapter, $operator)
|
||||
public static function getI18n(Translator $translator, $operator)
|
||||
{
|
||||
$translator = $adapter->getTranslator();
|
||||
|
||||
$ret = $operator;
|
||||
switch ($operator) {
|
||||
case self::INFERIOR:
|
||||
$ret = $translator->trans(
|
||||
'inferior to',
|
||||
null,
|
||||
array(),
|
||||
'constraint'
|
||||
);
|
||||
break;
|
||||
case self::INFERIOR_OR_EQUAL:
|
||||
$ret = $translator->trans(
|
||||
'inferior or equals to',
|
||||
null,
|
||||
array(),
|
||||
'constraint'
|
||||
);
|
||||
break;
|
||||
case self::EQUAL:
|
||||
$ret = $translator->trans(
|
||||
'equals to',
|
||||
null,
|
||||
array(),
|
||||
'constraint'
|
||||
);
|
||||
break;
|
||||
case self::SUPERIOR_OR_EQUAL:
|
||||
$ret = $translator->trans(
|
||||
'superior or equals to',
|
||||
null,
|
||||
array(),
|
||||
'constraint'
|
||||
);
|
||||
break;
|
||||
case self::SUPERIOR:
|
||||
$ret = $translator->trans(
|
||||
'superior to',
|
||||
null,
|
||||
array(),
|
||||
'constraint'
|
||||
);
|
||||
break;
|
||||
case self::DIFFERENT:
|
||||
$ret = $translator->trans(
|
||||
'different from',
|
||||
null,
|
||||
array(),
|
||||
'constraint'
|
||||
);
|
||||
break;
|
||||
|
||||
49
core/lib/Thelia/Constraint/Rule/SerializableRule.php
Normal file
49
core/lib/Thelia/Constraint/Rule/SerializableRule.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/**********************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/**********************************************************************************/
|
||||
|
||||
namespace Thelia\Constraint\Rule;
|
||||
|
||||
/**
|
||||
* Created by JetBrains PhpStorm.
|
||||
* Date: 8/19/13
|
||||
* Time: 3:24 PM
|
||||
*
|
||||
* A rule set by an admin ready to be serialized and stored in DataBase
|
||||
*
|
||||
* @package Constraint
|
||||
* @author Guillaume MOREL <gmorel@openstudio.fr>
|
||||
*
|
||||
*/
|
||||
class SerializableRule
|
||||
{
|
||||
/** @var string Rule Service id */
|
||||
public $ruleClassName = null;
|
||||
|
||||
/** @var array Operators set by Admin for this Rule */
|
||||
public $operators = array();
|
||||
|
||||
/** @var array Values set by Admin for this Rule */
|
||||
public $values = array();
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user