From 81170980e0236a247e1fd46186c6a0808ad5e5d7 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sat, 4 Jul 2015 18:39:43 +0930 Subject: [PATCH] Don't error if trying to serialise a non-existent relationship --- src/Api/Serializers/Serializer.php | 33 +++++++++++------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/src/Api/Serializers/Serializer.php b/src/Api/Serializers/Serializer.php index d25097e2e..75818e120 100644 --- a/src/Api/Serializers/Serializer.php +++ b/src/Api/Serializers/Serializer.php @@ -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}()"); - } }