Skip to content

Commit 8d16d2d

Browse files
committed
Added the magic getter function and its test
1 parent c7773df commit 8d16d2d

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

Renderer.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ public function getVar(array $context, array $elements)
9494
else $variable = $variable->$method();
9595
}
9696

97+
// magic getter
98+
elseif(method_exists($variable, '__get') && is_callable(array($variable, '__get')))
99+
{
100+
if($element !== null) $variable = $variable->$element;
101+
else $variable = '';
102+
}
103+
97104
// everythine failed
98105
else $variable = null;
99106
}

Tests/RendererTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,26 @@ public function testGetVar()
106106
$this->environment->isAutoEscape(),
107107
$this->renderer->getVar($this->context, array('template', 'environment', 'isAutoEscape'))
108108
);
109+
110+
// magic getter
111+
$this->context = array('dummyObject' => new dummyObject());
112+
$this->assertEquals(
113+
'magicGetter',
114+
$this->renderer->getVar($this->context, array('dummyObject', 'magicGetter'))
115+
);
116+
}
117+
}
118+
119+
/**
120+
* This is a dummy class so we can test the magic getter
121+
*
122+
* @author Jelmer Snoeck <[email protected]>
123+
*/
124+
class dummyObject
125+
{
126+
// the magic getter to test
127+
public function __get($value)
128+
{
129+
return $value;
109130
}
110131
}

0 commit comments

Comments
 (0)