summaryrefslogtreecommitdiff
path: root/lib/smarty3/sysplugins/smarty_internal_runtime_foreach.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/smarty3/sysplugins/smarty_internal_runtime_foreach.php')
-rw-r--r--lib/smarty3/sysplugins/smarty_internal_runtime_foreach.php119
1 files changed, 72 insertions, 47 deletions
diff --git a/lib/smarty3/sysplugins/smarty_internal_runtime_foreach.php b/lib/smarty3/sysplugins/smarty_internal_runtime_foreach.php
index bdefe69..badead1 100644
--- a/lib/smarty3/sysplugins/smarty_internal_runtime_foreach.php
+++ b/lib/smarty3/sysplugins/smarty_internal_runtime_foreach.php
@@ -6,11 +6,9 @@
* @package Smarty
* @subpackage PluginsInternal
* @author Uwe Tews
- *
*/
class Smarty_Internal_Runtime_Foreach
{
-
/**
* Stack of saved variables
*
@@ -34,39 +32,60 @@ class Smarty_Internal_Runtime_Foreach
*
* @return mixed $from
*/
- public function init(Smarty_Internal_Template $tpl, $from, $item, $needTotal = false, $key = null, $name = null,
- $properties = array())
- {
+ public function init(
+ Smarty_Internal_Template $tpl,
+ $from,
+ $item,
+ $needTotal = false,
+ $key = null,
+ $name = null,
+ $properties = array()
+ ) {
+ $needTotal = $needTotal || isset($properties[ 'total' ]);
$saveVars = array();
- if (!is_array($from) && !is_object($from)) {
- settype($from, 'array');
+ $total = null;
+ if (!is_array($from)) {
+ if (is_object($from)) {
+ if ($needTotal) {
+ $total = $this->count($from);
+ }
+ } else {
+ settype($from, 'array');
+ }
+ }
+ if (!isset($total)) {
+ $total = empty($from) ? 0 : ($needTotal ? count($from) : 1);
}
- $total = ($needTotal || isset($properties[ 'total' ])) ? $this->count($from) : 1;
if (isset($tpl->tpl_vars[ $item ])) {
- $saveVars[ $item ] = $tpl->tpl_vars[ $item ];
+ $saveVars[ 'item' ] = array(
+ $item,
+ $tpl->tpl_vars[ $item ]
+ );
}
$tpl->tpl_vars[ $item ] = new Smarty_Variable(null, $tpl->isRenderingCache);
- if (empty($from)) {
+ if ($total === 0) {
$from = null;
- $total = 0;
- if ($needTotal) {
- $tpl->tpl_vars[ $item ]->total = 0;
- }
} else {
if ($key) {
if (isset($tpl->tpl_vars[ $key ])) {
- $saveVars[ $key ] = $tpl->tpl_vars[ $key ];
+ $saveVars[ 'key' ] = array(
+ $key,
+ $tpl->tpl_vars[ $key ]
+ );
}
$tpl->tpl_vars[ $key ] = new Smarty_Variable(null, $tpl->isRenderingCache);
}
- if ($needTotal) {
- $tpl->tpl_vars[ $item ]->total = $total;
- }
+ }
+ if ($needTotal) {
+ $tpl->tpl_vars[ $item ]->total = $total;
}
if ($name) {
$namedVar = "__smarty_foreach_{$name}";
if (isset($tpl->tpl_vars[ $namedVar ])) {
- $saveVars[ $namedVar ] = $tpl->tpl_vars[ $namedVar ];
+ $saveVars[ 'named' ] = array(
+ $namedVar,
+ $tpl->tpl_vars[ $namedVar ]
+ );
}
$namedProp = array();
if (isset($properties[ 'total' ])) {
@@ -76,7 +95,7 @@ class Smarty_Internal_Runtime_Foreach
$namedProp[ 'iteration' ] = 0;
}
if (isset($properties[ 'index' ])) {
- $namedProp[ 'index' ] = - 1;
+ $namedProp[ 'index' ] = -1;
}
if (isset($properties[ 'show' ])) {
$namedProp[ 'show' ] = ($total > 0);
@@ -88,50 +107,56 @@ class Smarty_Internal_Runtime_Foreach
}
/**
- * Restore saved variables
- *
- * @param \Smarty_Internal_Template $tpl
- */
- public function restore(Smarty_Internal_Template $tpl)
- {
- foreach (array_pop($this->stack) as $k => $v) {
- $tpl->tpl_vars[ $k ] = $v;
- }
- }
-
- /*
- *
* [util function] counts an array, arrayAccess/traversable or PDOStatement object
*
- * @param mixed $value
+ * @param mixed $value
*
* @return int the count for arrays and objects that implement countable, 1 for other objects that don't, and 0
* for empty elements
*/
public function count($value)
{
- if (is_array($value) === true || $value instanceof Countable) {
- return count($value);
- } elseif ($value instanceof IteratorAggregate) {
+ if ($value instanceof IteratorAggregate) {
// Note: getIterator() returns a Traversable, not an Iterator
// thus rewind() and valid() methods may not be present
return iterator_count($value->getIterator());
} elseif ($value instanceof Iterator) {
- if ($value instanceof Generator) {
- return 1;
- }
- return iterator_count($value);
+ return $value instanceof Generator ? 1 : iterator_count($value);
+ } elseif ($value instanceof Countable) {
+ return count($value);
} elseif ($value instanceof PDOStatement) {
return $value->rowCount();
} elseif ($value instanceof Traversable) {
return iterator_count($value);
- } elseif ($value instanceof ArrayAccess) {
- if ($value->offsetExists(0)) {
- return 1;
+ }
+ return count((array)$value);
+ }
+
+ /**
+ * Restore saved variables
+ *
+ * will be called by {break n} or {continue n} for the required number of levels
+ *
+ * @param \Smarty_Internal_Template $tpl
+ * @param int $levels number of levels
+ */
+ public function restore(Smarty_Internal_Template $tpl, $levels = 1)
+ {
+ while ($levels) {
+ $saveVars = array_pop($this->stack);
+ if (!empty($saveVars)) {
+ if (isset($saveVars[ 'item' ])) {
+ $item = &$saveVars[ 'item' ];
+ $tpl->tpl_vars[ $item[ 0 ] ]->value = $item[ 1 ]->value;
+ }
+ if (isset($saveVars[ 'key' ])) {
+ $tpl->tpl_vars[ $saveVars[ 'key' ][ 0 ] ] = $saveVars[ 'key' ][ 1 ];
+ }
+ if (isset($saveVars[ 'named' ])) {
+ $tpl->tpl_vars[ $saveVars[ 'named' ][ 0 ] ] = $saveVars[ 'named' ][ 1 ];
+ }
}
- } elseif (is_object($value)) {
- return count($value);
+ $levels--;
}
- return 0;
}
}