From 00a8ad51b37d3b21cc1e807b5e3c2ecb74b0662c Mon Sep 17 00:00:00 2001 From: Etienne Roudeix Date: Mon, 24 Jun 2013 12:31:47 +0200 Subject: [PATCH] check type in Smarty plugin --- .../Template/Smarty/Plugins/TheliaLoop.php | 10 ++- core/lib/Thelia/Type/TypeCollection.php | 23 ++++- templates/smarty-sample/index.html | 85 ++----------------- 3 files changed, 36 insertions(+), 82 deletions(-) diff --git a/core/lib/Thelia/Core/Template/Smarty/Plugins/TheliaLoop.php b/core/lib/Thelia/Core/Template/Smarty/Plugins/TheliaLoop.php index 1810ad117..6d455d49d 100755 --- a/core/lib/Thelia/Core/Template/Smarty/Plugins/TheliaLoop.php +++ b/core/lib/Thelia/Core/Template/Smarty/Plugins/TheliaLoop.php @@ -237,7 +237,15 @@ class TheliaLoop implements SmartyPluginInterface { continue; } - /* check default */ + /* check type */ + if($value !== null && !$argument->type->isValid($value)) { + $faultActor[] = $argument->name; + $faultDetails[] = sprintf('Invalid value for "%s" argument', $argument->name); + continue; + } + + /* set default */ + /* did it as last checking for we consider default value is acceptable no matter type or empty restriction */ if($value === null) { $value = $argument->default; } diff --git a/core/lib/Thelia/Type/TypeCollection.php b/core/lib/Thelia/Type/TypeCollection.php index 621197776..ebbd3274b 100755 --- a/core/lib/Thelia/Type/TypeCollection.php +++ b/core/lib/Thelia/Type/TypeCollection.php @@ -65,7 +65,7 @@ class TypeCollection 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 \Thelia\Type\TypeInterface */ public function current() { @@ -116,4 +116,25 @@ class TypeCollection implements \Iterator { $this->position = 0; } + + /** + * @param $value + * + * @return bool + */ + public function isValid($value) + { + $this->rewind(); + while ($this->valid()) { + $type = $this->current(); + + if($type->isValid($value)) { + return true; + } + + $this->next(); + } + + return false; + } } diff --git a/templates/smarty-sample/index.html b/templates/smarty-sample/index.html index f93dd3876..7bba1e5da 100755 --- a/templates/smarty-sample/index.html +++ b/templates/smarty-sample/index.html @@ -1,80 +1,5 @@ -{include file="includes/header.html"} -
-An image from asset directory : -{images file='assets/img/logo-thelia-34px.png'}{intl l='Thelia, solution e-commerce libre'}{/images} -
- -
-{intl l='An internationalized string'} -
- -
- jQuery data: -
- -
-

Category loop example

- -
- -
-

Conditional example #1

- - {ifloop rel="catloop2"} - Hey ! Loop catloop2 is not empty: - - {/ifloop} - - {elseloop rel="catloop2"} -

Loop catloop2 is empty

- {/elseloop} -
- -
-

Conditional example #2

- - {ifloop rel="catloop3"} - Loop catloop3 is not empty: - - {/ifloop} - - {elseloop rel="catloop3"} -

Loop catloop3 is empty

- {/elseloop} - - {elseloop rel="catloop2"} -

... but catloop2 is still empty :-)

- {/elseloop} - {ifloop rel="catloop1"} -

... and catloop1 is still NOT empty :-)

- {/ifloop} -
- -
-

Traditional for loop

- {for $index=5 to 12 step 1} - Compteur = {$index}
- {/for} -
- -{include file="includes/footer.html"} \ No newline at end of file + \ No newline at end of file