Loop arguments

This commit is contained in:
Etienne Roudeix
2013-06-24 10:04:06 +02:00
parent 73be9e9677
commit 539cce542f
5 changed files with 39 additions and 39 deletions

View File

@@ -25,6 +25,7 @@ namespace Thelia\Core\Template\Element;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; 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(); abstract public function defineArgs();

View File

@@ -29,12 +29,13 @@ namespace Thelia\Core\Template\Loop\Argument;
*/ */
class Argument class Argument
{ {
protected $name; public $name;
protected $type; public $type;
protected $mandatory; public $default;
protected $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->name = $name;
$this->type = $type; $this->type = $type;

View File

@@ -65,7 +65,7 @@ class ArgumentCollection implements \Iterator
* (PHP 5 &gt;= 5.0.0)<br/> * (PHP 5 &gt;= 5.0.0)<br/>
* Return the current element * Return the current element
* @link http://php.net/manual/en/iterator.current.php * @link http://php.net/manual/en/iterator.current.php
* @return \Thelia\Core\Template\Element\LoopResultRow * @return Argument
*/ */
public function current() public function current()
{ {
@@ -80,7 +80,7 @@ class ArgumentCollection implements \Iterator
*/ */
public function next() public function next()
{ {
++$this->arguments; $this->position++;
} }
/** /**
@@ -91,7 +91,7 @@ class ArgumentCollection implements \Iterator
*/ */
public function key() public function key()
{ {
return $this->arguments; return $this->position;
} }
/** /**

View File

@@ -105,7 +105,6 @@ class Category extends BaseLoop {
new TypeCollection( new TypeCollection(
new Type\AnyType() new Type\AnyType()
), ),
false,
0 0
), ),
new Argument( new Argument(
@@ -113,7 +112,6 @@ class Category extends BaseLoop {
new TypeCollection( new TypeCollection(
new Type\AnyType() new Type\AnyType()
), ),
false,
1 1
), ),
new Argument( new Argument(
@@ -133,7 +131,6 @@ class Category extends BaseLoop {
new TypeCollection( new TypeCollection(
new Type\AnyType() new Type\AnyType()
), ),
false,
0 0
), ),
new Argument( new Argument(
@@ -147,7 +144,6 @@ class Category extends BaseLoop {
new TypeCollection( new TypeCollection(
new Type\AnyType() new Type\AnyType()
), ),
false,
10 10
), ),
new Argument( new Argument(
@@ -155,7 +151,6 @@ class Category extends BaseLoop {
new TypeCollection( new TypeCollection(
new Type\AnyType() new Type\AnyType()
), ),
false,
0 0
) )
); );

View File

@@ -23,6 +23,7 @@
namespace Thelia\Core\Template\Smarty\Plugins; namespace Thelia\Core\Template\Smarty\Plugins;
use Thelia\Core\Template\Element\BaseLoop;
use Thelia\Core\Template\Smarty\SmartyPluginInterface; use Thelia\Core\Template\Smarty\SmartyPluginInterface;
use Thelia\Core\Template\Smarty\SmartyPluginDescriptor; use Thelia\Core\Template\Smarty\SmartyPluginDescriptor;
@@ -202,7 +203,7 @@ class TheliaLoop implements SmartyPluginInterface {
* @param unknown $smartyParam * @param unknown $smartyParam
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
protected function getLoopArgument($loop, $smartyParam) protected function getLoopArgument(BaseLoop $loop, $smartyParam)
{ {
$defaultItemsParams = array('required' => true); $defaultItemsParams = array('required' => true);
@@ -212,35 +213,37 @@ class TheliaLoop implements SmartyPluginInterface {
$faultActor = array(); $faultActor = array();
$faultDetails = array(); $faultDetails = array();
foreach($loop->defineArgs() as $name => $param){ $argumentsCollection = $loop->defineArgs();
if(is_integer($name)){ $argumentsCollection->rewind();
$name = $param;
$param = $defaultItemsParams;
}
if(is_string($param) && array_key_exists($param, $shortcutItemParams)){ while ($argumentsCollection->valid()) {
$param = $shortcutItemParams[$param];
}
if(!is_array($param)){ $argument = $argumentsCollection->current();
$param = array('default' => $param); $argumentsCollection->next();
}
$value = isset($smartyParam[$name]) ? $smartyParam[$name] : null; $value = isset($smartyParam[$argument->name]) ? $smartyParam[$argument->name] : null;
if($value == null){ /* check if mandatory */
if(isset($param['default'])){ if($value === null && $argument->mandatory) {
$value = $param['default']; $faultActor[] = $argument->name;
} $faultDetails[] = sprintf('"%s" parameter is missing', $argument->name);
else if($param['required'] === true){ continue;
$faultActor[] = $name; }
$faultDetails[] = sprintf('"%s" parameter is missing', $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)){ if(!empty($faultActor)){