Skip to content

Commit 1da15ad

Browse files
committed
Make dot helper handle magic method #10
1 parent e9162ba commit 1da15ad

File tree

3 files changed

+131
-3
lines changed

3 files changed

+131
-3
lines changed

phpunit.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
<testsuite name="options">
2323
<file>tests/options.php</file>
2424
</testsuite>
25+
<testsuite name="helpers">
26+
<file>tests/dotHelper.php</file>
27+
</testsuite>
2528
<testsuite name="badSyntaxes">
2629
<file>tests/badSyntaxes.php</file>
2730
<file>tests/badSwitchSyntaxes.php</file>

src/JsPhpize/Compiler/Helpers/Dot.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
function ($base) {
2+
$getCallable = function ($base, $key) {
3+
if (is_callable(array($base, $key))) {
4+
return array($base, $key);
5+
}
6+
};
27
foreach (array_slice(func_get_args(), 1) as $key) {
38
$base = is_array($base)
4-
? (isset($base[$key]) ? $base[$key] : null)
9+
? (isset($base[$key])
10+
? $base[$key]
11+
: null
12+
)
513
: (is_object($base)
614
? (isset($base->$key)
715
? $base->$key
816
: (method_exists($base, $method = "get" . ucfirst($key))
917
? $base->$method()
1018
: (method_exists($base, $key)
1119
? array($base, $key)
12-
: null
20+
: $getCallable($base, $key)
1321
)
1422
)
1523
)
16-
: null
24+
: $getCallable($base, $key)
1725
);
1826
}
1927

tests/dotHelper.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
use JsPhpize\JsPhpize;
4+
5+
class MagicMethodObject
6+
{
7+
public function __get($name)
8+
{
9+
if ($name === 'foo') {
10+
return 'bar';
11+
}
12+
}
13+
14+
public function __call($name, array $args)
15+
{
16+
if ($name === 'bar') {
17+
return 'biz';
18+
}
19+
}
20+
21+
public function __isset($name)
22+
{
23+
return $name === 'foo';
24+
}
25+
}
26+
27+
class DotHelperTest extends \PHPUnit_Framework_TestCase
28+
{
29+
protected function getDotHelper()
30+
{
31+
return eval('return ' . file_get_contents(__DIR__ . '/../src/JsPhpize/Compiler/Helpers/Dot.h') . ';');
32+
}
33+
34+
public function testArrayValue()
35+
{
36+
$dotHelper = $this->getDotHelper();
37+
38+
$this->assertSame(42, $dotHelper(array(
39+
'foo' => 42,
40+
), 'foo'));
41+
$this->assertSame('biz', $dotHelper(array(
42+
'foo' => array(
43+
'bar' => 'biz',
44+
),
45+
), 'foo', 'bar'));
46+
$this->assertSame(null, $dotHelper(array(
47+
'foo' => array(
48+
),
49+
), 'foo', 'bar'));
50+
$this->assertSame(null, $dotHelper(array(
51+
'foo' => array(
52+
),
53+
), 'biz', 'bar'));
54+
}
55+
56+
public function testObjectMember()
57+
{
58+
$dotHelper = $this->getDotHelper();
59+
60+
$this->assertSame(42, $dotHelper((object) array(
61+
'foo' => 42,
62+
), 'foo'));
63+
$this->assertSame('biz', $dotHelper((object) array(
64+
'foo' => (object) array(
65+
'bar' => 'biz',
66+
),
67+
), 'foo', 'bar'));
68+
$this->assertSame(null, $dotHelper((object) array(
69+
'foo' => (object) array(
70+
),
71+
), 'foo', 'bar'));
72+
$this->assertSame(null, $dotHelper((object) array(
73+
'foo' => (object) array(
74+
),
75+
), 'biz', 'bar'));
76+
}
77+
78+
public function testMixedObjectMemberAndArayValue()
79+
{
80+
$dotHelper = $this->getDotHelper();
81+
82+
$this->assertSame('biz', $dotHelper(array(
83+
'foo' => (object) array(
84+
'bar' => 'biz',
85+
),
86+
), 'foo', 'bar'));
87+
$this->assertSame('biz', $dotHelper((object) array(
88+
'foo' => array(
89+
'bar' => 'biz',
90+
),
91+
), 'foo', 'bar'));
92+
$this->assertSame(42, $dotHelper((object) array(
93+
'foo' => array(
94+
'bar' => (object) array(
95+
'biz' => 42,
96+
),
97+
),
98+
), 'foo', 'bar', 'biz'));
99+
$this->assertSame(42, $dotHelper(array(
100+
'foo' => (object) array(
101+
'bar' => array(
102+
'biz' => 42,
103+
),
104+
),
105+
), 'foo', 'bar', 'biz'));
106+
}
107+
108+
public function testMagicMethod()
109+
{
110+
$dotHelper = $this->getDotHelper();
111+
$object = new MagicMethodObject();
112+
113+
$this->assertSame('bar', $dotHelper($object, 'foo'));
114+
$this->assertSame('biz', call_user_func($dotHelper($object, 'bar')));
115+
$this->assertSame(null, call_user_func($dotHelper($object, 'biz')));
116+
}
117+
}

0 commit comments

Comments
 (0)