loop argument management
type management
This commit is contained in:
44
core/lib/Thelia/Core/Template/Loop/Argument/Argument.php
Normal file
44
core/lib/Thelia/Core/Template/Loop/Argument/Argument.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
namespace Thelia\Core\Template\Loop\Argument;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Etienne Roudeix <eroudeix@openstudio.fr>
|
||||
*
|
||||
*/
|
||||
class Argument
|
||||
{
|
||||
protected $name;
|
||||
protected $type;
|
||||
protected $mandatory;
|
||||
protected $default;
|
||||
|
||||
public function __construct($name, \Thelia\Type\TypeCollection $type, $mandatory = false, $default = null)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->type = $type;
|
||||
$this->mandatory = $mandatory ? true : false;
|
||||
$this->default = $default;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
namespace Thelia\Core\Template\Loop\Argument;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Etienne Roudeix <eroudeix@openstudio.fr>
|
||||
*
|
||||
*/
|
||||
|
||||
class ArgumentCollection implements \Iterator
|
||||
{
|
||||
private $position;
|
||||
protected $arguments = array();
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
foreach(func_get_args() as $argument) {
|
||||
$this->addArgument($argument);
|
||||
}
|
||||
}
|
||||
|
||||
public function isEmpty()
|
||||
{
|
||||
return count($this->arguments) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Argument $argument
|
||||
*
|
||||
* @return ArgumentCollection
|
||||
*/
|
||||
public function addArgument(Argument $argument)
|
||||
{
|
||||
$this->arguments[] = $argument;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCount()
|
||||
{
|
||||
return count($this->arguments);
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)<br/>
|
||||
* Return the current element
|
||||
* @link http://php.net/manual/en/iterator.current.php
|
||||
* @return \Thelia\Core\Template\Element\LoopResultRow
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
return $this->arguments[$this->position];
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)<br/>
|
||||
* Move forward to next element
|
||||
* @link http://php.net/manual/en/iterator.next.php
|
||||
* @return void Any returned value is ignored.
|
||||
*/
|
||||
public function next()
|
||||
{
|
||||
++$this->arguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)<br/>
|
||||
* Return the key of the current element
|
||||
* @link http://php.net/manual/en/iterator.key.php
|
||||
* @return mixed scalar on success, or null on failure.
|
||||
*/
|
||||
public function key()
|
||||
{
|
||||
return $this->arguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)<br/>
|
||||
* Checks if current position is valid
|
||||
* @link http://php.net/manual/en/iterator.valid.php
|
||||
* @return boolean The return value will be casted to boolean and then evaluated.
|
||||
* Returns true on success or false on failure.
|
||||
*/
|
||||
public function valid()
|
||||
{
|
||||
return isset($this->arguments[$this->position]);
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)<br/>
|
||||
* Rewind the Iterator to the first element
|
||||
* @link http://php.net/manual/en/iterator.rewind.php
|
||||
* @return void Any returned value is ignored.
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
$this->position = 0;
|
||||
}
|
||||
}
|
||||
@@ -28,8 +28,12 @@ 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\Log\Tlog;
|
||||
use Thelia\Model\CategoryQuery;
|
||||
use Thelia\Type\TypeCollection;
|
||||
use Thelia\Type;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -77,18 +81,83 @@ class Category extends BaseLoop {
|
||||
|
||||
public function defineArgs()
|
||||
{
|
||||
return array(
|
||||
"id" => "optional",
|
||||
"parent" => "optional",
|
||||
"current" => "optional",
|
||||
"not_empty" => 0,
|
||||
"visible" => 1,
|
||||
"link" => "optional",
|
||||
"order" => "optional",
|
||||
"random" => 0,
|
||||
"exclude" => "optional",
|
||||
"limit" => 10,
|
||||
"offset" => 0,
|
||||
return new ArgumentCollection(
|
||||
new Argument(
|
||||
'id',
|
||||
new TypeCollection(
|
||||
new Type\AnyType()
|
||||
)
|
||||
),
|
||||
new Argument(
|
||||
'parent',
|
||||
new TypeCollection(
|
||||
new Type\AnyType()
|
||||
)
|
||||
),
|
||||
new Argument(
|
||||
'current',
|
||||
new TypeCollection(
|
||||
new Type\AnyType()
|
||||
)
|
||||
),
|
||||
new Argument(
|
||||
'not_empty',
|
||||
new TypeCollection(
|
||||
new Type\AnyType()
|
||||
),
|
||||
false,
|
||||
0
|
||||
),
|
||||
new Argument(
|
||||
'visible',
|
||||
new TypeCollection(
|
||||
new Type\AnyType()
|
||||
),
|
||||
false,
|
||||
1
|
||||
),
|
||||
new Argument(
|
||||
'link',
|
||||
new TypeCollection(
|
||||
new Type\AnyType()
|
||||
)
|
||||
),
|
||||
new Argument(
|
||||
'order',
|
||||
new TypeCollection(
|
||||
new Type\AnyType()
|
||||
)
|
||||
),
|
||||
new Argument(
|
||||
'random',
|
||||
new TypeCollection(
|
||||
new Type\AnyType()
|
||||
),
|
||||
false,
|
||||
0
|
||||
),
|
||||
new Argument(
|
||||
'exclude',
|
||||
new TypeCollection(
|
||||
new Type\AnyType()
|
||||
)
|
||||
),
|
||||
new Argument(
|
||||
'limit',
|
||||
new TypeCollection(
|
||||
new Type\AnyType()
|
||||
),
|
||||
false,
|
||||
10
|
||||
),
|
||||
new Argument(
|
||||
'offset',
|
||||
new TypeCollection(
|
||||
new Type\AnyType()
|
||||
),
|
||||
false,
|
||||
0
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
42
core/lib/Thelia/Type/AnyType.php
Normal file
42
core/lib/Thelia/Type/AnyType.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
namespace Thelia\Type;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Etienne Roudeix <eroudeix@openstudio.fr>
|
||||
*
|
||||
*/
|
||||
|
||||
class AnyType implements TypeInterface
|
||||
{
|
||||
public function getType()
|
||||
{
|
||||
return 'Any type';
|
||||
}
|
||||
|
||||
public function isValid($value)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
119
core/lib/Thelia/Type/TypeCollection.php
Normal file
119
core/lib/Thelia/Type/TypeCollection.php
Normal file
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
namespace Thelia\Type;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Etienne Roudeix <eroudeix@openstudio.fr>
|
||||
*
|
||||
*/
|
||||
|
||||
class TypeCollection implements \Iterator
|
||||
{
|
||||
private $position;
|
||||
protected $types = array();
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
foreach(func_get_args() as $type) {
|
||||
$this->addType($type);
|
||||
}
|
||||
}
|
||||
|
||||
public function isEmpty()
|
||||
{
|
||||
return count($this->types) == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TypeInterface $type
|
||||
*
|
||||
* @return TypeCollection
|
||||
*/
|
||||
public function addType(TypeInterface $type)
|
||||
{
|
||||
$this->types[] = $type;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCount()
|
||||
{
|
||||
return count($this->types);
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)<br/>
|
||||
* Return the current element
|
||||
* @link http://php.net/manual/en/iterator.current.php
|
||||
* @return \Thelia\Core\Template\Element\LoopResultRow
|
||||
*/
|
||||
public function current()
|
||||
{
|
||||
return $this->types[$this->position];
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)<br/>
|
||||
* Move forward to next element
|
||||
* @link http://php.net/manual/en/iterator.next.php
|
||||
* @return void Any returned value is ignored.
|
||||
*/
|
||||
public function next()
|
||||
{
|
||||
++$this->types;
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)<br/>
|
||||
* Return the key of the current element
|
||||
* @link http://php.net/manual/en/iterator.key.php
|
||||
* @return mixed scalar on success, or null on failure.
|
||||
*/
|
||||
public function key()
|
||||
{
|
||||
return $this->types;
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)<br/>
|
||||
* Checks if current position is valid
|
||||
* @link http://php.net/manual/en/iterator.valid.php
|
||||
* @return boolean The return value will be casted to boolean and then evaluated.
|
||||
* Returns true on success or false on failure.
|
||||
*/
|
||||
public function valid()
|
||||
{
|
||||
return isset($this->types[$this->position]);
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)<br/>
|
||||
* Rewind the Iterator to the first element
|
||||
* @link http://php.net/manual/en/iterator.rewind.php
|
||||
* @return void Any returned value is ignored.
|
||||
*/
|
||||
public function rewind()
|
||||
{
|
||||
$this->position = 0;
|
||||
}
|
||||
}
|
||||
36
core/lib/Thelia/Type/TypeInterface.php
Normal file
36
core/lib/Thelia/Type/TypeInterface.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
namespace Thelia\Type;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Etienne Roudeix <eroudeix@openstudio.fr>
|
||||
*
|
||||
*/
|
||||
|
||||
interface TypeInterface
|
||||
{
|
||||
public function getType();
|
||||
|
||||
public function isValid($value);
|
||||
}
|
||||
Reference in New Issue
Block a user