From a3f44dea62d8ec06e555da060c9ec00be55253ed Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Tue, 5 May 2015 14:26:53 +0930 Subject: [PATCH] Change API for serializer relationships --- .../src/Api/Events/SerializeRelationship.php | 14 --------- .../src/Api/Serializers/BaseSerializer.php | 30 +++++++++++++++++-- 2 files changed, 27 insertions(+), 17 deletions(-) delete mode 100644 framework/core/src/Api/Events/SerializeRelationship.php diff --git a/framework/core/src/Api/Events/SerializeRelationship.php b/framework/core/src/Api/Events/SerializeRelationship.php deleted file mode 100644 index cfc07e259..000000000 --- a/framework/core/src/Api/Events/SerializeRelationship.php +++ /dev/null @@ -1,14 +0,0 @@ -serializer = $serializer; - $this->name = $name; - } -} diff --git a/framework/core/src/Api/Serializers/BaseSerializer.php b/framework/core/src/Api/Serializers/BaseSerializer.php index 14eab87ac..a4c330260 100644 --- a/framework/core/src/Api/Serializers/BaseSerializer.php +++ b/framework/core/src/Api/Serializers/BaseSerializer.php @@ -13,6 +13,13 @@ abstract class BaseSerializer extends SerializerAbstract { protected $actor; + /** + * The custom relationships on this serializer. + * + * @var array + */ + protected static $relationships = []; + public function __construct(Actor $actor, $include = null, $link = null) { parent::__construct($include, $link); @@ -77,14 +84,31 @@ abstract class BaseSerializer extends SerializerAbstract } /** - * Fire an event to allow for custom links and includes. + * Add a custom relationship to the serializer. + * + * @param string $name The name of the relationship. + * @param Closure $callback The callback to execute. + * @return void + */ + public static function addRelationship($name, $callback) + { + static::$relationships[$name] = $callback; + } + + /** + * Check for and execute custom relationships. * * @param string $name * @param array $arguments - * @return void + * @return mixed */ public function __call($name, $arguments) { - return event(new SerializeRelationship($this, $name), null, true); + if (isset(static::$relationships[$name])) { + array_unshift($arguments, $this); + return call_user_func_array(static::$relationships[$name], $arguments); + } + + return parent::__call($name, $arguments); } }