Skip to content

Commit

Permalink
Delegates the magic call strategy on model instances to `data\model\M…
Browse files Browse the repository at this point in the history
…odel`.
  • Loading branch information
jails committed Dec 15, 2012
1 parent 62b8b7a commit e73ffe6
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
18 changes: 5 additions & 13 deletions data/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,25 +182,17 @@ public function __isset($name) {
* $record->validates();
* }}}
*
* @see lithium\data\Model::instanceMethods
* @param string $method
* @param array $params
* @param string $method Method name caught by `__call()`.
* @param array $params Arguments given to the above `$method` call.
* @return mixed
*/
public function __call($method, $params) {
if ($model = $this->_model) {
$methods = $model::instanceMethods();
array_unshift($params, $this);

if (method_exists($model, $method)) {
$class = $model::invokeMethod('_object');
return call_user_func_array(array(&$class, $method), $params);
}
if (isset($methods[$method]) && is_callable($methods[$method])) {
return call_user_func_array($methods[$method], $params);
}
$class = $model::invokeMethod('_object');
return call_user_func_array(array(&$class, $method), $params);
}
$message = "No model bound or unhandled method call `{$method}`.";
$message = "No model bound to call `{$method}`.";
throw new BadMethodCallException($message);
}

Expand Down
18 changes: 18 additions & 0 deletions data/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,24 @@ public static function __callStatic($method, $params) {
return $self::find($type, compact('conditions') + $params);
}

/**
* Magic method that allows calling `Model::_instanceMethods`'s closure like normal methods
* on the model instance.
*
* @see lithium\data\Model::instanceMethods
* @param string $method Method name caught by `__call()`.
* @param array $params Arguments given to the above `$method` call.
* @return mixed
*/
public function __call($method, $params) {
$methods = static::instanceMethods();
if (isset($methods[$method]) && is_callable($methods[$method])) {
return call_user_func_array($methods[$method], $params);
}
$message = "Unhandled method call `{$method}`.";
throw new BadMethodCallException($message);
}

/**
* The `find` method allows you to retrieve data from the connected data source.
*
Expand Down
9 changes: 8 additions & 1 deletion tests/cases/data/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,14 @@ public function testMethodDispatch() {
}));
$this->assertEqual('testInstanceMethod', $entity->testInstanceMethod($entity));

$this->expectException("/^No model bound or unhandled method call `foo`.$/");
$this->expectException("/^Unhandled method call `foo`.$/");
$entity->foo();
}

public function testMethodDispatchWithNoModel() {
$data = array('foo' => true);
$entity = new Entity(compact('data'));
$this->expectException("/^No model bound to call `foo`.$/");
$entity->foo();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/cases/data/entity/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ public function testArrayValueSet() {
public function testInvalidCall() {
$doc = new Document();

$this->expectException("No model bound or unhandled method call `medicin`.");
$this->expectException("No model bound to call `medicin`.");
$result = $doc->medicin();
$this->assertNull($result);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/data/entity/RecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function testMethodDispatch() {
$this->assertEqual('create', $result['query']->type());
$this->assertEqual(array('title' => 'foo'), $result['query']->data());

$this->expectException("No model bound or unhandled method call `invalid`.");
$this->expectException("Unhandled method call `invalid`.");
$this->assertNull($this->record->invalid());
}
}
Expand Down

0 comments on commit e73ffe6

Please sign in to comment.