From 539cce542f94a77975c73835c140ef5624e8fc7d Mon Sep 17 00:00:00 2001 From: Etienne Roudeix Date: Mon, 24 Jun 2013 10:04:06 +0200 Subject: [PATCH] Loop arguments --- .../Thelia/Core/Template/Element/BaseLoop.php | 3 +- .../Core/Template/Loop/Argument/Argument.php | 11 ++-- .../Loop/Argument/ArgumentCollection.php | 6 +-- .../Thelia/Core/Template/Loop/Category.php | 5 -- .../Template/Smarty/Plugins/TheliaLoop.php | 53 ++++++++++--------- 5 files changed, 39 insertions(+), 39 deletions(-) diff --git a/core/lib/Thelia/Core/Template/Element/BaseLoop.php b/core/lib/Thelia/Core/Template/Element/BaseLoop.php index b6085ad7c..519aae4bd 100755 --- a/core/lib/Thelia/Core/Template/Element/BaseLoop.php +++ b/core/lib/Thelia/Core/Template/Element/BaseLoop.php @@ -25,6 +25,7 @@ namespace Thelia\Core\Template\Element; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Thelia\Core\Template\Loop\Argument\ArgumentCollection; /** * @@ -96,7 +97,7 @@ abstract class BaseLoop * ) * ); * - * @return array + * @return ArgumentCollection */ abstract public function defineArgs(); diff --git a/core/lib/Thelia/Core/Template/Loop/Argument/Argument.php b/core/lib/Thelia/Core/Template/Loop/Argument/Argument.php index 1be69e18f..d2b6cdbb2 100644 --- a/core/lib/Thelia/Core/Template/Loop/Argument/Argument.php +++ b/core/lib/Thelia/Core/Template/Loop/Argument/Argument.php @@ -29,12 +29,13 @@ namespace Thelia\Core\Template\Loop\Argument; */ class Argument { - protected $name; - protected $type; - protected $mandatory; - protected $default; + public $name; + public $type; + public $default; + public $mandatory; + public $empty; - public function __construct($name, \Thelia\Type\TypeCollection $type, $mandatory = false, $default = null) + public function __construct($name, \Thelia\Type\TypeCollection $type, $default = null, $mandatory = false, $empty = true) { $this->name = $name; $this->type = $type; diff --git a/core/lib/Thelia/Core/Template/Loop/Argument/ArgumentCollection.php b/core/lib/Thelia/Core/Template/Loop/Argument/ArgumentCollection.php index 84cf958db..d3422c1e9 100644 --- a/core/lib/Thelia/Core/Template/Loop/Argument/ArgumentCollection.php +++ b/core/lib/Thelia/Core/Template/Loop/Argument/ArgumentCollection.php @@ -65,7 +65,7 @@ class ArgumentCollection implements \Iterator * (PHP 5 >= 5.0.0)
* Return the current element * @link http://php.net/manual/en/iterator.current.php - * @return \Thelia\Core\Template\Element\LoopResultRow + * @return Argument */ public function current() { @@ -80,7 +80,7 @@ class ArgumentCollection implements \Iterator */ public function next() { - ++$this->arguments; + $this->position++; } /** @@ -91,7 +91,7 @@ class ArgumentCollection implements \Iterator */ public function key() { - return $this->arguments; + return $this->position; } /** diff --git a/core/lib/Thelia/Core/Template/Loop/Category.php b/core/lib/Thelia/Core/Template/Loop/Category.php index 645eabd64..2541fc982 100755 --- a/core/lib/Thelia/Core/Template/Loop/Category.php +++ b/core/lib/Thelia/Core/Template/Loop/Category.php @@ -105,7 +105,6 @@ class Category extends BaseLoop { new TypeCollection( new Type\AnyType() ), - false, 0 ), new Argument( @@ -113,7 +112,6 @@ class Category extends BaseLoop { new TypeCollection( new Type\AnyType() ), - false, 1 ), new Argument( @@ -133,7 +131,6 @@ class Category extends BaseLoop { new TypeCollection( new Type\AnyType() ), - false, 0 ), new Argument( @@ -147,7 +144,6 @@ class Category extends BaseLoop { new TypeCollection( new Type\AnyType() ), - false, 10 ), new Argument( @@ -155,7 +151,6 @@ class Category extends BaseLoop { new TypeCollection( new Type\AnyType() ), - false, 0 ) ); diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/TheliaLoop.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/TheliaLoop.php index d8b1abd9a..1810ad117 100755 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/TheliaLoop.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/TheliaLoop.php @@ -23,6 +23,7 @@ namespace Thelia\Core\Template\Smarty\Plugins; +use Thelia\Core\Template\Element\BaseLoop; use Thelia\Core\Template\Smarty\SmartyPluginInterface; use Thelia\Core\Template\Smarty\SmartyPluginDescriptor; @@ -202,7 +203,7 @@ class TheliaLoop implements SmartyPluginInterface { * @param unknown $smartyParam * @throws \InvalidArgumentException */ - protected function getLoopArgument($loop, $smartyParam) + protected function getLoopArgument(BaseLoop $loop, $smartyParam) { $defaultItemsParams = array('required' => true); @@ -212,35 +213,37 @@ class TheliaLoop implements SmartyPluginInterface { $faultActor = array(); $faultDetails = array(); - foreach($loop->defineArgs() as $name => $param){ - if(is_integer($name)){ - $name = $param; - $param = $defaultItemsParams; - } + $argumentsCollection = $loop->defineArgs(); + $argumentsCollection->rewind(); - if(is_string($param) && array_key_exists($param, $shortcutItemParams)){ - $param = $shortcutItemParams[$param]; - } + while ($argumentsCollection->valid()) { - if(!is_array($param)){ - $param = array('default' => $param); - } + $argument = $argumentsCollection->current(); + $argumentsCollection->next(); - $value = isset($smartyParam[$name]) ? $smartyParam[$name] : null; + $value = isset($smartyParam[$argument->name]) ? $smartyParam[$argument->name] : null; - if($value == null){ - if(isset($param['default'])){ - $value = $param['default']; - } - else if($param['required'] === true){ - $faultActor[] = $name; - $faultDetails[] = sprintf('"%s" parameter is missing', $name); - continue; - } - } + /* check if mandatory */ + if($value === null && $argument->mandatory) { + $faultActor[] = $argument->name; + $faultDetails[] = sprintf('"%s" parameter is missing', $argument->name); + continue; + } - $loop->{$name} = $value; - } + /* check if empty */ + if($value === '' && !$argument->empty) { + $faultActor[] = $argument->name; + $faultDetails[] = sprintf('"%s" parameter cannot be empty', $argument->name); + continue; + } + + /* check default */ + if($value === null) { + $value = $argument->default; + } + + $loop->{$argument->name} = $value; + } if(!empty($faultActor)){