Skip to content

Commit

Permalink
Added the magic getter function and its test
Browse files Browse the repository at this point in the history
  • Loading branch information
jelmersnoeck committed Jan 6, 2012
1 parent c7773df commit 8d16d2d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Renderer.php
Expand Up @@ -94,6 +94,13 @@ public function getVar(array $context, array $elements)
else $variable = $variable->$method();
}

// magic getter
elseif(method_exists($variable, '__get') && is_callable(array($variable, '__get')))
{
if($element !== null) $variable = $variable->$element;
else $variable = '';
}

// everythine failed
else $variable = null;
}
Expand Down
21 changes: 21 additions & 0 deletions Tests/RendererTest.php
Expand Up @@ -106,5 +106,26 @@ public function testGetVar()
$this->environment->isAutoEscape(),
$this->renderer->getVar($this->context, array('template', 'environment', 'isAutoEscape'))
);

// magic getter
$this->context = array('dummyObject' => new dummyObject());
$this->assertEquals(
'magicGetter',
$this->renderer->getVar($this->context, array('dummyObject', 'magicGetter'))
);
}
}

/**
* This is a dummy class so we can test the magic getter
*
* @author Jelmer Snoeck <jelmer.snoeck@siphoc.com>
*/
class dummyObject
{
// the magic getter to test
public function __get($value)
{
return $value;
}
}

0 comments on commit 8d16d2d

Please sign in to comment.