mirror of
https://github.com/flarum/framework.git
synced 2024-11-24 00:57:55 +08:00
Change API for serializer relationships
This commit is contained in:
parent
3726c97d5c
commit
24481f3f77
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user