Change API for serializer relationships

This commit is contained in:
Toby Zerner 2015-05-05 14:26:53 +09:30
parent 3726c97d5c
commit 24481f3f77
2 changed files with 27 additions and 17 deletions

View File

@ -1,14 +0,0 @@
<?php namespace Flarum\Api\Events;
class SerializeRelationship
{
public $serializer;
public $name;
public function __construct($serializer, $name)
{
$this->serializer = $serializer;
$this->name = $name;
}
}

View File

@ -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);
}
}