Skip to content

Commit b87359c

Browse files
committed
Merge branch 'make-isset-check-methods' of github.com:BenConstable/presenter into develop closes #25
2 parents 796451c + 7acd20a commit b87359c

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/Robbo/Presenter/Presenter.php

+30-2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ public function offsetExists($offset)
7777
// We only check isset on the array, if it is an object we return true as the object could be overloaded
7878
if (is_array($this->object))
7979
{
80+
if ($method = $this->getPresentMethodForVariable($offset))
81+
{
82+
$result = $this->$method();
83+
return isset($result);
84+
}
85+
8086
return isset($this->object[$offset]);
8187
}
8288

@@ -137,8 +143,7 @@ public function offsetUnset($offset)
137143
*/
138144
public function __get($var)
139145
{
140-
$method = 'present'.str_replace(' ', '', ucwords(str_replace(array('-', '_'), ' ', $var)));
141-
if (method_exists($this, $method))
146+
if ($method = $this->getPresentMethodForVariable($var))
142147
{
143148
return $this->$method();
144149
}
@@ -173,6 +178,12 @@ public function __call($method, $arguments)
173178
*/
174179
public function __isset($name)
175180
{
181+
if ($method = $this->getPresentMethodForVariable($name))
182+
{
183+
$result = $this->$method();
184+
return isset($result);
185+
}
186+
176187
if (is_array($this->object))
177188
{
178189
return isset($this->object[$name]);
@@ -197,4 +208,21 @@ public function __unset($name)
197208
unset($this->object->$name);
198209
}
199210

211+
/**
212+
* Fetch the 'present' method name for the given variable.
213+
*
214+
* @param string $var Variable name
215+
* @return string|null Present method name, or null if method does not exist
216+
*/
217+
private function getPresentMethodForVariable($var)
218+
{
219+
$method = 'present'.str_replace(' ', '', ucwords(str_replace(array('-', '_'), ' ', $var)));
220+
221+
if (method_exists($this, $method)) {
222+
return $method;
223+
} else {
224+
return null;
225+
}
226+
}
227+
200228
}

tests/PresenterTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,21 @@ public function testArrayIsset()
7171

7272
$this->assertTrue(isset($presenter['testVar']));
7373
$this->assertFalse(isset($presenter['unsetVar']));
74+
$this->assertTrue(isset($presenter['awesome']));
7475

7576
$presenter = new PresenterStub(new InjectStub);
7677
$this->assertTrue(isset($presenter['unsetVar']));
7778
}
7879

80+
public function testObjectIsset()
81+
{
82+
$presenter = new PresenterStub(new InjectStub);
83+
84+
$this->assertTrue(isset($presenter->testVar));
85+
$this->assertTrue(isset($presenter->awesome));
86+
$this->assertFalse(isset($presenter->unsetVar));
87+
}
88+
7989
public function testArraySet()
8090
{
8191
$presenter = new PresenterStub(array('testVar' => 'testvar'));

0 commit comments

Comments
 (0)