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\EventDispatcher\EventDispatcherInterface;
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
/**
*
@@ -96,7 +97,7 @@ abstract class BaseLoop
* )
* );
*
* @return array
* @return ArgumentCollection
*/
abstract public function defineArgs();

View File

@@ -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;

View File

@@ -65,7 +65,7 @@ class ArgumentCollection implements \Iterator
* (PHP 5 &gt;= 5.0.0)<br/>
* 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;
}
/**

View File

@@ -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
)
);

View File

@@ -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)){