+ * public function getRoles()
+ * {
+ * return array('ROLE_USER');
+ * }
+ *
+ *
+ * @return Role[] The user roles
+ */
+ public function getRoles();
+
/**
* Removes sensitive data from the user.
*
diff --git a/core/lib/Thelia/Core/Template/Element/BaseLoop.php b/core/lib/Thelia/Core/Template/Element/BaseLoop.php
index 8345ae06a..f7a958cad 100755
--- a/core/lib/Thelia/Core/Template/Element/BaseLoop.php
+++ b/core/lib/Thelia/Core/Template/Element/BaseLoop.php
@@ -38,16 +38,14 @@ abstract class BaseLoop
/**
* @var \Symfony\Component\HttpFoundation\Request
*/
- public $request;
+ protected $request;
/**
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
- public $dispatcher;
+ protected $dispatcher;
- public $limit;
- public $page;
- public $offset;
+ private $args;
protected function getDefaultArgs()
{
@@ -59,6 +57,8 @@ abstract class BaseLoop
}
/**
+ * Create a new Loop
+ *
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
*/
@@ -66,14 +66,94 @@ abstract class BaseLoop
{
$this->request = $request;
$this->dispatcher = $dispatcher;
+
+ $this->args = $this->getArgDefinitions()->addArguments($this->getDefaultArgs());
}
/**
- * @return \Thelia\Core\Template\Loop\Argument\ArgumentCollection
+ * Initialize the loop arguments.
+ *
+ * @param array $nameValuePairs a array of name => value pairs. The name is the name of the argument.
+ *
+ * @throws \InvalidArgumentException if somùe argument values are missing, or invalid
*/
- public function getArgs()
- {
- return $this->defineArgs()->addArguments($this->getDefaultArgs());
+ public function initializeArgs(array $nameValuePairs) {
+
+ $faultActor = array();
+ $faultDetails = array();
+
+ while (($argument = $this->args->current()) !== false) {
+
+ $value = isset($nameValuePairs[$argument->name]) ? $nameValuePairs[$argument->name] : null;
+
+ /* check if mandatory */
+ if($value === null && $argument->mandatory) {
+ $faultActor[] = $argument->name;
+ $faultDetails[] = sprintf('"%s" parameter is missing', $argument->name);
+ continue;
+ }
+
+ /* check if empty */
+ if($value === '' && !$argument->empty) {
+ $faultActor[] = $argument->name;
+ $faultDetails[] = sprintf('"%s" parameter cannot be empty', $argument->name);
+ continue;
+ }
+
+ /* 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;
+ }
+
+ $argument->setValue($value);
+
+ $this->args->next();
+ }
+
+ if (!empty($faultActor)) {
+
+ $complement = sprintf('[%s]', implode(', ', $faultDetails));
+ throw new \InvalidArgumentException($complement);
+ }
+ }
+
+ /**
+ * Return a loop argument
+ *
+ * @param string $argumentName the argument name
+ *
+ * @throws \InvalidArgumentException if argument is not found in loop argument list
+ * @return Argument the loop argument.
+ */
+ public function getArg($argumentName) {
+
+ $arg = $this->args->get($argumentName);
+
+ if ($arg === null)
+ throw new \InvalidArgumentException("Undefined loop argument '$argumentName'");
+
+ return $arg;
+ }
+
+ /**
+ * Return a loop argument value
+ *
+ * @param string $argumentName the argument name
+ *
+ * @throws \InvalidArgumentException if argument is not found in loop argument list
+ * @return Argument the loop argument.
+ */
+ public function getArgValue($argumentName) {
+
+ return $this->getArg($argumentName)->getValue();
}
/**
@@ -84,7 +164,7 @@ abstract class BaseLoop
*/
public function search(ModelCriteria $search, &$pagination = null)
{
- if($this->page !== null) {
+ if($this->getArgValue('page') !== null) {
return $this->searchWithPagination($search, $pagination);
} else {
return $this->searchWithOffset($search);
@@ -98,10 +178,10 @@ abstract class BaseLoop
*/
public function searchWithOffset(ModelCriteria $search)
{
- if($this->limit >= 0) {
- $search->limit($this->limit);
+ if($this->getArgValue('limit') >= 0) {
+ $search->limit($this->getArgValue('limit'));
}
- $search->offset($this->offset);
+ $search->offset($this->getArgValue('offset'));
return $search->find();
}
@@ -114,9 +194,9 @@ abstract class BaseLoop
*/
public function searchWithPagination(ModelCriteria $search, &$pagination)
{
- $pagination = $search->paginate($this->page, $this->limit);
+ $pagination = $search->paginate($this->getArgValue('page'), $this->getArgValue('limit'));
- if($this->page > $pagination->getLastPage()) {
+ if($this->getArgValue('page') > $pagination->getLastPage()) {
return array();
} else {
return $pagination;
@@ -148,7 +228,8 @@ abstract class BaseLoop
* @param $pagination
*
* @return mixed
- */abstract public function exec(&$pagination);
+ */
+ abstract public function exec(&$pagination);
/**
*
@@ -169,6 +250,6 @@ abstract class BaseLoop
*
* @return \Thelia\Core\Template\Loop\Argument\ArgumentCollection
*/
- abstract protected function defineArgs();
+ abstract protected function getArgDefinitions();
}
diff --git a/core/lib/Thelia/Core/Template/Loop/Argument/Argument.php b/core/lib/Thelia/Core/Template/Loop/Argument/Argument.php
index 86c11edfb..721dde312 100755
--- a/core/lib/Thelia/Core/Template/Loop/Argument/Argument.php
+++ b/core/lib/Thelia/Core/Template/Loop/Argument/Argument.php
@@ -38,12 +38,25 @@ class Argument
public $mandatory;
public $empty;
- public function __construct($name, \Thelia\Type\TypeCollection $type, $default = null, $mandatory = false, $empty = true)
+ private $value;
+
+ public function __construct($name, \Thelia\Type\TypeCollection $type, $default = null, $mandatory = false, $empty = true, $value = null)
{
$this->name = $name;
$this->type = $type;
$this->mandatory = $mandatory ? true : false;
$this->default = $default;
+ $this->empty = $empty;
+
+ $this->setValue($value);
+ }
+
+ public function getValue() {
+ return $this->value;
+ }
+
+ public function setValue($value) {
+ $this->value = $value;
}
public static function createAnyTypeArgument($name, $default=null, $mandatory=false, $empty=true)
diff --git a/core/lib/Thelia/Core/Template/Loop/Argument/ArgumentCollection.php b/core/lib/Thelia/Core/Template/Loop/Argument/ArgumentCollection.php
index 6aec1fd9b..b4b680e08 100755
--- a/core/lib/Thelia/Core/Template/Loop/Argument/ArgumentCollection.php
+++ b/core/lib/Thelia/Core/Template/Loop/Argument/ArgumentCollection.php
@@ -30,14 +30,21 @@ namespace Thelia\Core\Template\Loop\Argument;
class ArgumentCollection implements \Iterator
{
- private $position;
- protected $arguments = array();
+ private $arguments = array();
public function __construct()
{
$this->addArguments(func_get_args());
}
+ public function hasKey($key) {
+ return isset($this->arguments[$key]);
+ }
+
+ public function get($key) {
+ return $this->hasKey($key) ? $this->arguments[$key] : null;
+ }
+
public function isEmpty()
{
return count($this->arguments) == 0;
@@ -64,7 +71,8 @@ class ArgumentCollection implements \Iterator
*/
public function addArgument(Argument $argument)
{
- $this->arguments[] = $argument;
+ $this->arguments[$argument->name] = $argument;
+
return $this;
}
@@ -81,7 +89,7 @@ class ArgumentCollection implements \Iterator
*/
public function current()
{
- return $this->arguments[$this->position];
+ return current($this->arguments);
}
/**
@@ -92,7 +100,7 @@ class ArgumentCollection implements \Iterator
*/
public function next()
{
- $this->position++;
+ next($this->arguments);
}
/**
@@ -103,7 +111,7 @@ class ArgumentCollection implements \Iterator
*/
public function key()
{
- return $this->position;
+ return key($this->arguments);
}
/**
@@ -115,7 +123,7 @@ class ArgumentCollection implements \Iterator
*/
public function valid()
{
- return isset($this->arguments[$this->position]);
+ return $this->key() !== null;
}
/**
@@ -126,6 +134,6 @@ class ArgumentCollection implements \Iterator
*/
public function rewind()
{
- $this->position = 0;
+ reset($this->arguments);
}
}
diff --git a/core/lib/Thelia/Core/Template/Loop/Category.php b/core/lib/Thelia/Core/Template/Loop/Category.php
index 44874ed59..a80f42549 100755
--- a/core/lib/Thelia/Core/Template/Loop/Category.php
+++ b/core/lib/Thelia/Core/Template/Loop/Category.php
@@ -68,20 +68,10 @@ use Thelia\Type;
*/
class Category extends BaseLoop
{
- public $id;
- public $parent;
- public $current;
- public $not_empty;
- public $visible;
- public $link;
- public $order;
- public $random;
- public $exclude;
-
/**
* @return ArgumentCollection
*/
- protected function defineArgs()
+ protected function getArgDefinitions()
{
return new ArgumentCollection(
Argument::createIntListTypeArgument('id'),
@@ -110,31 +100,45 @@ class Category extends BaseLoop
{
$search = CategoryQuery::create();
- if (!is_null($this->id)) {
- $search->filterById($this->id, Criteria::IN);
+ $id = $this->getArgValue('id');
+
+ if (!is_null($id)) {
+ $search->filterById($id, Criteria::IN);
}
- if (!is_null($this->parent)) {
- $search->filterByParent($this->parent);
+
+ $parent = $this->getArgValue('parent');
+
+ if (!is_null($parent)) {
+ $search->filterByParent($parent);
}
- if ($this->current === true) {
+
+ $current = $this->getArgValue('current');
+
+ if ($current === true) {
$search->filterById($this->request->get("category_id"));
- } elseif ($this->current === false) {
+ } elseif ($current === false) {
$search->filterById($this->request->get("category_id"), Criteria::NOT_IN);
}
- if (!is_null($this->exclude)) {
- $search->filterById($this->exclude, Criteria::NOT_IN);
+
+ $exclude = $this->getArgValue('exclude');
+
+ if (!is_null($exclude)) {
+ $search->filterById($exclude, Criteria::NOT_IN);
}
- if (!is_null($this->link)) {
- $search->filterByLink($this->link);
+
+ $link = $this->getArgValue('link');
+
+ if (!is_null($link)) {
+ $search->filterByLink($link);
}
- $search->filterByVisible($this->visible ? 1 : 0);
+ $search->filterByVisible($this->getArgValue('visible') ? 1 : 0);
- switch ($this->order) {
+ switch ($this->getArgValue('order')) {
case "alpha":
$search->addAscendingOrderByColumn(\Thelia\Model\CategoryI18nPeer::TITLE);
break;
@@ -149,7 +153,8 @@ class Category extends BaseLoop
break;
}
- if ($this->random === true) {
+
+ if ($this->getArgValue('random') === true) {
$search->clearOrderByColumns();
$search->addAscendingOrderByColumn('RAND()');
}
@@ -161,7 +166,7 @@ class Category extends BaseLoop
*/
$search->joinWithI18n(
- $this->request->getSession()->get('locale', 'en_US'),
+ $this->request->getSession()->getLocale(),
(ConfigQuery::read("default_lang_without_translation", 1)) ? Criteria::LEFT_JOIN : Criteria::INNER_JOIN
);
@@ -189,5 +194,4 @@ class Category extends BaseLoop
return $loopResult;
}
-
-}
+}
\ No newline at end of file
diff --git a/core/lib/Thelia/Core/Template/Loop/Feed.php b/core/lib/Thelia/Core/Template/Loop/Feed.php
new file mode 100644
index 000000000..8d6626013
--- /dev/null
+++ b/core/lib/Thelia/Core/Template/Loop/Feed.php
@@ -0,0 +1,115 @@
+. */
+/* */
+/*************************************************************************************/
+
+namespace Thelia\Core\Template\Loop;
+
+use Thelia\Core\Template\Element\BaseLoop;
+use Thelia\Core\Template\Element\LoopResult;
+use Thelia\Core\Template\Element\LoopResultRow;
+
+use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
+use Thelia\Core\Template\Loop\Argument\Argument;
+
+use Thelia\Type\TypeCollection;
+use Thelia\Type;
+
+/**
+ *
+ * @package Thelia\Core\Template\Loop
+ *
+ * @author Franck Allimant