Coupon : Condition module refactor
Less crappy unmaintainable javascript More logic in extendable php
This commit is contained in:
@@ -23,6 +23,9 @@
|
||||
|
||||
namespace Thelia\Condition;
|
||||
|
||||
use ArrayAccess;
|
||||
use Countable;
|
||||
use Iterator;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Thelia\Condition\Implementation\ConditionInterface;
|
||||
|
||||
@@ -33,43 +36,153 @@ use Thelia\Condition\Implementation\ConditionInterface;
|
||||
* @author Guillaume MOREL <gmorel@openstudio.fr>
|
||||
*
|
||||
*/
|
||||
class ConditionCollection
|
||||
class ConditionCollection implements Iterator, Countable, ArrayAccess
|
||||
{
|
||||
/** @var array Array of ConditionInterface */
|
||||
protected $conditions = array();
|
||||
|
||||
/**
|
||||
* Get Conditions
|
||||
* (PHP 5 >= 5.0.0)
|
||||
* Return the current element
|
||||
* @link http://php.net/manual/en/iterator.current.php
|
||||
*
|
||||
* @return array Array of ConditionInterface
|
||||
* @return mixed Can return any type.
|
||||
*/
|
||||
public function getConditions()
|
||||
public function current()
|
||||
{
|
||||
return $this->conditions;
|
||||
$var = current($this->conditions);
|
||||
|
||||
return $var;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a ConditionInterface to the Collection
|
||||
* (PHP 5 >= 5.0.0)
|
||||
* Move forward to next element
|
||||
* @link http://php.net/manual/en/iterator.next.php
|
||||
*
|
||||
* @param ConditionInterface $condition Condition
|
||||
*
|
||||
* @return $this
|
||||
* @return void Any returned value is ignored.
|
||||
*/
|
||||
public function add(ConditionInterface $condition)
|
||||
public function next()
|
||||
{
|
||||
$this->conditions[] = $condition;
|
||||
|
||||
return $this;
|
||||
next($this->conditions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if there is at least one condition in the collection
|
||||
* (PHP 5 >= 5.0.0)
|
||||
* Return the key of the current element
|
||||
* @link http://php.net/manual/en/iterator.key.php
|
||||
*
|
||||
* @return bool
|
||||
* @return mixed scalar on success, or null on failure.
|
||||
*/
|
||||
public function isEmpty()
|
||||
public function key()
|
||||
{
|
||||
return (empty($this->conditions));
|
||||
$var = key($this->conditions);
|
||||
|
||||
return $var;
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)
|
||||
* Checks if current position is valid
|
||||
* @link http://php.net/manual/en/iterator.valid.php
|
||||
*
|
||||
* @return boolean The return value will be casted to boolean and then evaluated.
|
||||
* Returns true on success or false on failure.
|
||||
*/
|
||||
public function valid()
|
||||
{
|
||||
$key = key($this->conditions);
|
||||
$var = ($key !== null && $key !== false);
|
||||
|
||||
return $var;
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)
|
||||
* Rewind the Iterator to the first element
|
||||
* @link http://php.net/manual/en/iterator.rewind.php
|
||||
*
|
||||
* @return void Any returned value is ignored.
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
reset($this->conditions);
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.1.0)
|
||||
* Count elements of an object
|
||||
* @link http://php.net/manual/en/countable.count.php
|
||||
*
|
||||
* @return int The custom count as an integer.
|
||||
* The return value is cast to an integer.
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return count($this->conditions);
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)
|
||||
* Whether a offset exists
|
||||
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
|
||||
* @param mixed $offset
|
||||
* An offset to check for.
|
||||
*
|
||||
* @return boolean true on success or false on failure.
|
||||
* The return value will be casted to boolean if non-boolean was returned.
|
||||
*/
|
||||
public function offsetExists($offset)
|
||||
{
|
||||
return isset($this->conditions[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)
|
||||
* Offset to retrieve
|
||||
* @link http://php.net/manual/en/arrayaccess.offsetget.php
|
||||
* @param mixed $offset
|
||||
* The offset to retrieve.
|
||||
*
|
||||
* @return mixed Can return all value types.
|
||||
*/
|
||||
public function offsetGet($offset)
|
||||
{
|
||||
return isset($this->conditions[$offset]) ? $this->conditions[$offset] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)
|
||||
* Offset to set
|
||||
* @link http://php.net/manual/en/arrayaccess.offsetset.php
|
||||
* @param mixed $offset
|
||||
* The offset to assign the value to.
|
||||
* @param mixed $value
|
||||
* The value to set.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
if (is_null($offset)) {
|
||||
$this->conditions[] = $value;
|
||||
} else {
|
||||
$this->conditions[$offset] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)
|
||||
* Offset to unset
|
||||
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
|
||||
* @param mixed $offset
|
||||
* The offset to unset.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
unset($this->conditions[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,12 +194,11 @@ class ConditionCollection
|
||||
{
|
||||
$arrayToSerialize = array();
|
||||
/** @var ConditionInterface $condition */
|
||||
foreach ($this->getConditions() as $condition) {
|
||||
foreach ($this as $condition) {
|
||||
$arrayToSerialize[] = $condition->getSerializableCondition();
|
||||
}
|
||||
|
||||
return json_encode($arrayToSerialize);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user