Permalink
Browse files

Refactor how arrays are proxied. Implement an arrayiterator for array…

…s and send through the standard object proxy so iteration works as expected, as well as array access.
  • Loading branch information...
1 parent 9789a03 commit ea6a8e55ef8ba401a76b6af7fb15b22e47bade24 @saltybeagle saltybeagle committed Mar 14, 2011
@@ -234,7 +234,7 @@ function addGlobal($name, $value)
$value = $this->escape($value);
break;
case 'array':
- $value = new ObjectProxy\ArrayAccess(new \ArrayIterator($value), $this);
+ $value = new ObjectProxy\ArrayIterator($value, $this);
break;
}
}
@@ -714,6 +714,15 @@ public function renderAssocArray(array $array, $selected = false, Closure $templ
return $ret;
}
+ protected function renderArrayAccess(\ArrayAccess $array, $template = null)
+ {
+ $ret = '';
+ foreach ($array as $key => $element) {
+ $ret .= $this->render($element, $template);
+ }
+ return $ret;
+ }
+
/**
* Render an if else conditional template output.
*
@@ -744,9 +753,16 @@ public function renderElse($condition, $render, $else, $rendertemplate = null, $
*/
protected function renderObject($object, $template = null)
{
- if ($this->__config['escape']
- && !$object instanceof ObjectProxy) {
- $object = ObjectProxy::factory($object, $this);
+ if ($this->__config['escape']) {
+
+ if (!$object instanceof ObjectProxy) {
+ $object = ObjectProxy::factory($object, $this);
+ }
+
+ if ($object instanceof ObjectProxy\ArrayIterator) {
+ return $this->renderArrayAccess($object);
+ }
+
}
return $this->fetch($object, $template);
}
@@ -85,7 +85,7 @@ protected function filterVar($var)
case 'double':
return $this->savant->escape($var);
case 'array':
- return new ObjectProxy\ArrayAccess(new \ArrayIterator($var), $this->savant);
+ return new ObjectProxy\ArrayIterator($var, $this->savant);
}
return $var;
}
@@ -176,8 +176,8 @@ public static function factory($object, $savant)
if ($object instanceof \Traversable) {
return new ObjectProxy\Traversable($object, $savant);
}
- if ($object instanceof \ArrayAccess) {
- return new ObjectProxy\ArrayAccess($object, $savant);
+ if ($object instanceof \ArrayIterator) {
+ return new ObjectProxy\ArrayIterator($object, $savant);
}
return new self($object, $savant);
}
@@ -1,25 +0,0 @@
-<?php
-namespace PEAR2\Templates\Savant\ObjectProxy;
-use PEAR2\Templates\Savant\ObjectProxy;
-class ArrayAccess extends ObjectProxy implements \ArrayAccess
-{
- function offsetExists($offset)
- {
- return $this->object->offsetExists($offset);
- }
-
- function offsetGet($offset)
- {
- return $this->filterVar($this->object->offsetGet($offset));
- }
-
- function offsetSet($offset, $value)
- {
- $this->object->offsetSet($offset, $value);
- }
-
- function offsetUnset($offset)
- {
- $this->object->offsetUnset($offset);
- }
-}
@@ -0,0 +1,67 @@
+<?php
+namespace PEAR2\Templates\Savant\ObjectProxy;
+use PEAR2\Templates\Savant\ObjectProxy;
+class ArrayIterator extends ObjectProxy implements \Iterator, \ArrayAccess, \SeekableIterator, \Countable
+{
+
+ /**
+ * Construct a new object proxy
+ *
+ * @param array $array The array
+ * @param Main $savant The savant templating system
+ */
+ function __construct($array, $savant)
+ {
+ parent::__construct(new \ArrayIterator($array), $savant);
+ }
+
+ function current()
+ {
+ return $this->object->current();
+ }
+
+ function next()
+ {
+ return $this->object->next();
+ }
+
+ function key()
+ {
+ return $this->object->key();
+ }
+
+ function valid()
+ {
+ return $this->object->valid();
+ }
+
+ function rewind()
+ {
+ return $this->object->rewind();
+ }
+
+ function seek($offset)
+ {
+ return $this->object->seek($offset);
+ }
+
+ function offsetExists($offset)
+ {
+ return $this->object->offsetExists($offset);
+ }
+
+ function offsetGet($offset)
+ {
+ return $this->filterVar($this->object->offsetGet($offset));
+ }
+
+ function offsetSet($offset, $value)
+ {
+ $this->object->offsetSet($offset, $value);
+ }
+
+ function offsetUnset($offset)
+ {
+ $this->object->offsetUnset($offset);
+ }
+}

0 comments on commit ea6a8e5

Please sign in to comment.