From 7cea85b43e8a4bc05e20f6215ba4d0f0e3b49072 Mon Sep 17 00:00:00 2001 From: franck Date: Mon, 8 Jul 2013 20:04:53 +0200 Subject: [PATCH] Added magic method __call to access loop arguments using getArgname() --- .../Thelia/Core/Template/Element/BaseLoop.php | 13 +++++++ .../Thelia/Core/Template/Loop/Category.php | 17 +++++----- core/lib/Thelia/Core/Template/Loop/Feed.php | 19 +++-------- .../lib/Thelia/Core/Template/Loop/Product.php | 34 +++++++++---------- 4 files changed, 43 insertions(+), 40 deletions(-) diff --git a/core/lib/Thelia/Core/Template/Element/BaseLoop.php b/core/lib/Thelia/Core/Template/Element/BaseLoop.php index f7a958cad..cb012d430 100755 --- a/core/lib/Thelia/Core/Template/Element/BaseLoop.php +++ b/core/lib/Thelia/Core/Template/Element/BaseLoop.php @@ -56,6 +56,19 @@ abstract class BaseLoop ); } + public function __call($name, $arguments) { + if (substr($name, 0, 3) == 'get') { + + $argName = strtolower(substr($name, 3)); + + return $this->getArg($argName)->getValue(); + } + + throw new \InvalidArgumentException(sprintf("Unsupported magic method %s. only getArgname() is supported.", $name)); + + } + + /** * Create a new Loop * diff --git a/core/lib/Thelia/Core/Template/Loop/Category.php b/core/lib/Thelia/Core/Template/Loop/Category.php index a80f42549..c12d723e4 100755 --- a/core/lib/Thelia/Core/Template/Loop/Category.php +++ b/core/lib/Thelia/Core/Template/Loop/Category.php @@ -100,21 +100,20 @@ class Category extends BaseLoop { $search = CategoryQuery::create(); - $id = $this->getArgValue('id'); + $id = $this->getId(); if (!is_null($id)) { $search->filterById($id, Criteria::IN); } - - $parent = $this->getArgValue('parent'); + $parent = $this->getParent(); if (!is_null($parent)) { $search->filterByParent($parent); } - $current = $this->getArgValue('current'); + $current = $this->getCurrent(); if ($current === true) { $search->filterById($this->request->get("category_id")); @@ -123,22 +122,22 @@ class Category extends BaseLoop } - $exclude = $this->getArgValue('exclude'); + $exclude = $this->getExclude(); if (!is_null($exclude)) { $search->filterById($exclude, Criteria::NOT_IN); } - $link = $this->getArgValue('link'); + $link = $this->getLink(); if (!is_null($link)) { $search->filterByLink($link); } - $search->filterByVisible($this->getArgValue('visible') ? 1 : 0); + $search->filterByVisible($this->getVisible() ? 1 : 0); - switch ($this->getArgValue('order')) { + switch ($this->getOrder()) { case "alpha": $search->addAscendingOrderByColumn(\Thelia\Model\CategoryI18nPeer::TITLE); break; @@ -154,7 +153,7 @@ class Category extends BaseLoop } - if ($this->getArgValue('random') === true) { + if ($this->getRandom() === true) { $search->clearOrderByColumns(); $search->addAscendingOrderByColumn('RAND()'); } diff --git a/core/lib/Thelia/Core/Template/Loop/Feed.php b/core/lib/Thelia/Core/Template/Loop/Feed.php index 8d6626013..f76c0c049 100644 --- a/core/lib/Thelia/Core/Template/Loop/Feed.php +++ b/core/lib/Thelia/Core/Template/Loop/Feed.php @@ -44,17 +44,8 @@ class Feed extends BaseLoop public function getArgDefinitions() { return new ArgumentCollection( - new Argument( - 'url', - new TypeCollection(new Type\AnyType()) - ), - new Argument( - 'timeout', - new TypeCollection( - new Type\IntType() - ), - 10 - ) + Argument::createAnyTypeArgument('url', null, true), + Argument::createIntTypeArgument('timeout', 10) ); } @@ -73,17 +64,17 @@ class Feed extends BaseLoop } } - $feed = new \SimplePie($this->getArgValue('url'), THELIA_ROOT . 'cache/feeds'); + $feed = new \SimplePie($this->getUrl(), THELIA_ROOT . 'cache/feeds'); $feed->init(); $feed->handle_content_type(); - $feed->set_timeout($this->getArgValue('timeout')); + $feed->set_timeout($this->getTimeout()); $items = $feed->get_items(); - $limit = min(count($items), $this->getArgValue('limit')); + $limit = min(count($items), $this->getLimit()); $loopResult = new LoopResult(); diff --git a/core/lib/Thelia/Core/Template/Loop/Product.php b/core/lib/Thelia/Core/Template/Loop/Product.php index acef5a27d..935879961 100755 --- a/core/lib/Thelia/Core/Template/Loop/Product.php +++ b/core/lib/Thelia/Core/Template/Loop/Product.php @@ -97,24 +97,24 @@ class Product extends BaseLoop { $search = ProductQuery::create(); - $id = $this->getArgValue('id'); + $id = $this->getId(); if (!is_null($id)) { $search->filterById($id, Criteria::IN); } - $ref = $this->getArgValue('ref'); + $ref = $this->getRef(); if (!is_null($ref)) { $search->filterByRef($ref, Criteria::IN); } - $category = $this->getArgValue('category'); + $category = $this->getCategory(); if (!is_null($category)) { $categories = CategoryQuery::create()->filterById($category, Criteria::IN)->find(); - $depth = $this->getArgValue('depth'); + $depth = $this->getDepth(); if(null !== $depth) { foreach(CategoryQuery::findAllChild($category, $depth) as $subCategory) { @@ -128,7 +128,7 @@ class Product extends BaseLoop ); } - $new = $this->getArgValue('new'); + $new = $this->getNew(); if ($new === true) { $search->filterByNewness(1, Criteria::EQUAL); @@ -136,7 +136,7 @@ class Product extends BaseLoop $search->filterByNewness(0, Criteria::EQUAL); } - $promo = $this->getArgValue('promo'); + $promo = $this->getPromo(); if ($promo === true) { $search->filterByPromo(1, Criteria::EQUAL); @@ -144,13 +144,13 @@ class Product extends BaseLoop $search->filterByNewness(0, Criteria::EQUAL); } - $min_stock = $this->getArgValue('min_stock'); + $min_stock = $this->getMin_stock(); if (null != $min_stock) { $search->filterByQuantity($min_stock, Criteria::GREATER_EQUAL); } - $min_price = $this->getArgValue('min_price'); + $min_price = $this->getMin_price(); if(null !== $min_price) { $search->condition('in_promo', ProductPeer::PROMO . Criteria::EQUAL . '1') @@ -162,7 +162,7 @@ class Product extends BaseLoop ->where(array('not_in_promo_min_price', 'in_promo_min_price'), Criteria::LOGICAL_OR); } - $max_price = $this->getArgValue('max_price'); + $max_price = $this->getMax_price(); if(null !== $max_price) { $search->condition('in_promo', ProductPeer::PROMO . Criteria::EQUAL . '1') @@ -174,19 +174,19 @@ class Product extends BaseLoop ->where(array('not_in_promo_max_price', 'in_promo_max_price'), Criteria::LOGICAL_OR); } - $min_weight = $this->getArgValue('min_weight'); + $min_weight = $this->getMin_weight(); if(null !== $min_weight) { $search->filterByWeight($min_weight, Criteria::GREATER_EQUAL); } - $max_weight = $this->getArgValue('max_weight'); + $max_weight = $this->getMax_weight(); if(null !== $max_weight) { $search->filterByWeight($max_weight, Criteria::LESS_EQUAL); } - $current = $this->getArgValue('current'); + $current = $this->getCurrent(); if ($current === true) { $search->filterById($this->request->get("product_id")); @@ -194,7 +194,7 @@ class Product extends BaseLoop $search->filterById($this->request->get("product_id"), Criteria::NOT_IN); } - $current_category = $this->getArgValue('current_category'); + $current_category = $this->getCurrent_category(); if ($current_category === true) { $search->filterByCategory( @@ -220,9 +220,9 @@ class Product extends BaseLoop ); } - $search->filterByVisible($this->getArgValue('visible')); + $search->filterByVisible($this->getVisible()); - switch ($this->getArgValue('order')) { + switch ($this->getOrder()) { case "alpha": $search->addAscendingOrderByColumn(\Thelia\Model\CategoryI18nPeer::TITLE); break; @@ -262,12 +262,12 @@ class Product extends BaseLoop break; } - if ($this->getArgValue('random') === true) { + if ($this->getRandom() === true) { $search->clearOrderByColumns(); $search->addAscendingOrderByColumn('RAND()'); } - $exclude = $this->getArgValue('exclude'); + $exclude = $this->getExclude(); if (!is_null($exclude)) { $search->filterById($exclude, Criteria::NOT_IN);