Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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...
commit 7197c4d78a62e9cbb8444ec41b86885102ae8cbb 1 parent 8cc8ef0
@saltybeagle authored
View
23 src/Savvy.php
@@ -289,7 +289,7 @@ function addGlobal($name, $value)
$value = $this->escape($value);
break;
case 'array':
- $value = new Savvy_ObjectProxy_ArrayAccess(new ArrayIterator($value), $this);
+ $value = new Savvy_ObjectProxy_ArrayIterator($value, $this);
break;
}
}
@@ -769,6 +769,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.
*
@@ -799,9 +808,15 @@ public function renderElse($condition, $render, $else, $rendertemplate = null, $
*/
protected function renderObject($object, $template = null)
{
- if ($this->__config['escape']
- && !$object instanceof Savvy_ObjectProxy) {
- $object = Savvy_ObjectProxy::factory($object, $this);
+ if ($this->__config['escape']) {
+
+ if (!$object instanceof Savvy_ObjectProxy) {
+ $object = Savvy_ObjectProxy::factory($object, $this);
+ }
+
+ if ($object instanceof Savvy_ObjectProxy_ArrayIterator) {
+ return $this->renderArrayAccess($object);
+ }
}
return $this->fetch($object, $template);
}
View
9 src/Savvy/ObjectProxy.php
@@ -84,8 +84,7 @@ protected function filterVar($var)
case 'double':
return $this->savvy->escape($var);
case 'array':
- return new Savvy_ObjectProxy_ArrayAccess(
- new ArrayIterator($var), $this->savvy);
+ return new Savvy_ObjectProxy_ArrayIterator($var, $this->savvy);
}
return $var;
}
@@ -176,8 +175,8 @@ public static function factory($object, $savvy)
if ($object instanceof Traversable) {
return new Savvy_ObjectProxy_Traversable($object, $savvy);
}
- if ($object instanceof ArrayAccess) {
- return new Savvy_ObjectProxy_ArrayAccess($object, $savvy);
+ if ($object instanceof ArrayIterator) {
+ return new Savvy_ObjectProxy_ArrayIterator($object, $savvy);
}
return new self($object, $savvy);
}
@@ -200,4 +199,4 @@ function count()
{
return count($this->object);
}
-}
+}
View
24 src/Savvy/ObjectProxy/ArrayAccess.php
@@ -1,24 +0,0 @@
-<?php
-
-class Savvy_ObjectProxy_ArrayAccess extends Savvy_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);
- }
-}
View
65 src/Savvy/ObjectProxy/ArrayIterator.php
@@ -0,0 +1,65 @@
+<?php
+class Savvy_ObjectProxy_ArrayIterator extends Savvy_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, $savvy)
+ {
+ parent::__construct(new ArrayIterator($array), $savvy);
+ }
+
+ 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);
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.