Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add an ObjectProxy object which implements array access

  • Loading branch information...
commit e23420dac1efea683b3ddcb424ee564350c87c4e 1 parent 6694da3
Brett Bieber saltybeagle authored
6 src/Templates/Savant/Main.php
View
@@ -665,11 +665,7 @@ protected function renderObject($object, $template = null)
{
if ($this->__config['escape']
&& !$object instanceof ObjectProxy) {
- if ($object instanceof \Traversable) {
- $object = new ObjectProxy\Traversable($object, $this);
- } else {
- $object = new ObjectProxy($object, $this);
- }
+ $object = ObjectProxy::factory($object, $this);
}
return $this->fetch($object, $template);
}
37 src/Templates/Savant/ObjectProxy.php
View
@@ -65,13 +65,21 @@ function __construct($object, $savant)
*/
function __get($var)
{
- $var = $this->object->$var;
+ return $this->filterVar($this->object->$var);
+ }
+
+ /**
+ * Returns a variable, after it has been filtered.
+ *
+ * @param mixed $var
+ *
+ * @return string|ObjectProxy
+ */
+ protected function filterVar($var)
+ {
switch(gettype($var)) {
case 'object':
- if ($var instanceof \Traversable) {
- return new ObjectProxy\Traversable($var, $this->savant);
- }
- return new self($var, $this->savant);
+ return self::factory($var, $this->savant);
case 'string':
case 'int':
case 'bool':
@@ -118,4 +126,23 @@ function __getClass()
{
return get_class($this->object);
}
+
+ /**
+ * Constructs an ObjectProxy for the given object.
+ *
+ * @param mixed $object The object to proxy
+ * @param Main $savant The main savant instance
+ *
+ * @return ObjectProxy
+ */
+ 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);
+ }
+ return new self($object, $savant);
+ }
}
25 src/Templates/Savant/ObjectProxy/ArrayAccess.php
View
@@ -0,0 +1,25 @@
+<?php
+namespace pear2\Templates\Savant\ObjectProxy;
+use pear2\Templates\Savant\ObjectProxy;
+class ArrayAccess extends ObjectProxy implements \ArrayAccess
+{
+ function offsetExists($offset)
+ {
+ return $this->object->offsetExists();
+ }
+
+ 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.