Don't error if trying to serialise a non-existent relationship

This commit is contained in:
Toby Zerner 2015-07-04 18:39:43 +09:30
parent 86811c6508
commit 81170980e0

View File

@ -52,6 +52,18 @@ abstract class Serializer extends SerializerAbstract
*/
abstract protected function getDefaultAttributes($model);
/**
* {@inheritdoc}
*/
protected function getRelationshipFromMethod($name)
{
if (isset(static::$relationMethods[$name])) {
return call_user_func(static::$relationMethods[$name], $this);
}
return parent::getRelationshipFromMethod($name);
}
/**
* Get a closure that returns a Collection/Resource representing a relation.
*
@ -154,25 +166,4 @@ abstract class Serializer extends SerializerAbstract
{
static::$relationMethods[get_called_class()][$name] = $callback;
}
/**
* Check for and execute custom relationships.
*
* @param string $method
* @param array $parameters
* @return mixed
* @throws BadMethodCallException
*/
public function __call($method, $parameters)
{
if (isset(static::$relationMethods[$method])) {
array_unshift($parameters, $this);
return call_user_func_array(static::$relationMethods[$method], $parameters);
}
$className = get_class($this);
throw new BadMethodCallException("Call to undefined method {$className}::{$method}()");
}
}