Allow use of ArrayAccess objects in context.
This commit is contained in:
committed by
Justin Hileman
parent
0e1d5a678f
commit
0c85d19509
@@ -133,17 +133,22 @@ class Mustache_Context
|
||||
private function findVariableInStack($id, array $stack)
|
||||
{
|
||||
for ($i = count($stack) - 1; $i >= 0; $i--) {
|
||||
if (is_object($stack[$i]) && !$stack[$i] instanceof Closure) {
|
||||
if (is_object($stack[$i])) {
|
||||
if( $stack[$i] instanceof ArrayAccess) {
|
||||
if (isset($stack[$i][$id])) {
|
||||
return $stack[$i][$id];
|
||||
}
|
||||
} elseif (!($stack[$i] instanceof Closure)) {
|
||||
if (method_exists($stack[$i], $id)) {
|
||||
return $stack[$i]->$id();
|
||||
} elseif (isset($stack[$i]->$id)) {
|
||||
return $stack[$i]->$id;
|
||||
}
|
||||
}
|
||||
} elseif (is_array($stack[$i]) && array_key_exists($id, $stack[$i])) {
|
||||
return $stack[$i][$id];
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,6 +71,8 @@ class Mustache_Test_ContextTest extends PHPUnit_Framework_TestCase
|
||||
|
||||
$string = 'some arbitrary string';
|
||||
|
||||
$access = new Mustache_Test_TestArrayAccess($arr);
|
||||
|
||||
$context->push($dummy);
|
||||
$this->assertEquals('dummy', $context->find('name'));
|
||||
|
||||
@@ -95,6 +97,11 @@ class Mustache_Test_ContextTest extends PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('see', $context->findDot('a.b.c'));
|
||||
$this->assertEquals('<foo>', $context->find('foo'));
|
||||
$this->assertEquals('<bar>', $context->findDot('bar'));
|
||||
|
||||
$context = new Mustache_Context($arr);
|
||||
$this->assertEquals('bee', $context->find('b'));
|
||||
$this->assertEquals('see', $context->findDot('a.b.c'));
|
||||
$this->assertEquals(null, $context->findDot('a.b.c.d'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,3 +124,28 @@ class Mustache_Test_TestDummy
|
||||
return '<bar>';
|
||||
}
|
||||
}
|
||||
|
||||
class Mustache_Test_TestArrayAccess implements arrayaccess {
|
||||
private $container = array();
|
||||
public function __construct($array) {
|
||||
foreach($array as $key => $value) {
|
||||
$this->container[$key] = $value;
|
||||
}
|
||||
}
|
||||
public function offsetSet($offset, $value) {
|
||||
if (is_null($offset)) {
|
||||
$this->container[] = $value;
|
||||
} else {
|
||||
$this->container[$offset] = $value;
|
||||
}
|
||||
}
|
||||
public function offsetExists($offset) {
|
||||
return isset($this->container[$offset]);
|
||||
}
|
||||
public function offsetUnset($offset) {
|
||||
unset($this->container[$offset]);
|
||||
}
|
||||
public function offsetGet($offset) {
|
||||
return isset($this->container[$offset]) ? $this->container[$offset] : null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user