Skip to content

Commit 243d465

Browse files
committed
Merge pull request #4 from sirn-se/harry-potter
Implementing classes as magic methods
2 parents bd74c09 + fb17f67 commit 243d465

File tree

6 files changed

+47
-4
lines changed

6 files changed

+47
-4
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use Textalk\WebshopClient\Connection;
2121
use Textalk\WebshopClient\ApiClass;
2222

2323
$connection = Connection::getInstance('default', array('webshop' => 22222));
24-
$assortment = $connection->getApiClass('Assortment');
24+
$assortment = $connection->Assortment;
2525

2626
var_dump($assortment->getArticlegroupUids());
2727

@@ -44,7 +44,7 @@ use Textalk\WebshopClient\Connection;
4444
use Textalk\WebshopClient\ApiInstance;
4545

4646
$connection = Connection::getInstance('default', array('webshop' => 22222));
47-
$articlegroup = $connection->getApiInstance('Articlegroup', 1347891);
47+
$articlegroup = $connection->Articlegroup(1347891);
4848

4949
var_dump($articlegroup->get('name'));
5050

examples/get_articlegroupdata.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
use Textalk\WebshopClient\Instance;
77

88
$connection = Connection::getInstance('default', array('webshop' => 22222));
9-
$articlegroup = new ApiInstance('Articlegroup', 1347891, $connection);
9+
$articlegroup = $connection->Articlegroup(1347891);
1010

1111
var_dump($articlegroup->get('name'));

examples/get_articlegroups.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
use Textalk\WebshopClient\ApiClass;
77

88
$connection = Connection::getInstance('default', array('webshop' => 22222));
9-
$assortment = new ApiClass('Assortment', $connection);
9+
$assortment = $connection->Assortment;
1010

1111
var_dump($assortment->getArticlegroupUids());

lib/Connection.php

+26
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,32 @@ public function getApiInstance($class, $uid) {
105105
return new ApiInstance($class, $uid, $this);
106106
}
107107

108+
//
109+
// Magic methods
110+
//
111+
112+
/**
113+
* Get an ApiClass for this connection.
114+
*
115+
* @param $class string Class name
116+
* @return Textalk\WebshopClient\ApiClass
117+
*/
118+
public function __get($class) {
119+
return new ApiClass(ucfirst($class), $this);
120+
}
121+
122+
/**
123+
* Get an ApiInstance for this connection.
124+
*
125+
* @param $class string Class name
126+
* @param $args array The API instance UID as only item
127+
* @return Textalk\WebshopClient\ApiInstance
128+
*/
129+
public function __call($class, array $args) {
130+
$uid = array_shift($args); // $uid becomes null if array is empty
131+
return new ApiInstance(ucfirst($class), $uid, $this);
132+
}
133+
108134
//
109135
// Protected
110136
//

tests/ApiClassTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,13 @@ public function testConnectionGetMethod() {
3535
// No classname validation is done before first call.
3636
$this->assertInstanceOf('Textalk\WebshopClient\ApiClass', $class);
3737
}
38+
39+
public function testMagicMethod() {
40+
$connection = new Connection();
41+
$class = $connection->Foo;
42+
43+
// No classname validation is done before first call.
44+
$this->assertInstanceOf('Textalk\WebshopClient\ApiClass', $class);
45+
}
46+
3847
}

tests/ApiInstanceTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,12 @@ public function testConnectionGetMethod() {
2828
$this->assertInstanceOf('Textalk\WebshopClient\ApiInstance', $class);
2929
}
3030

31+
public function testMagicMethod() {
32+
$connection = new Connection();
33+
$class = $connection->Foo('bar');
34+
35+
// No classname validation is done before first call.
36+
$this->assertInstanceOf('Textalk\WebshopClient\ApiInstance', $class);
37+
}
38+
3139
}

0 commit comments

Comments
 (0)