mirror of
https://github.com/flarum/framework.git
synced 2025-02-22 05:38:56 +08:00
Change API for serializer relationships
This commit is contained in:
parent
ab6675d19b
commit
a3f44dea62
@ -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;
|
protected $actor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The custom relationships on this serializer.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected static $relationships = [];
|
||||||
|
|
||||||
public function __construct(Actor $actor, $include = null, $link = null)
|
public function __construct(Actor $actor, $include = null, $link = null)
|
||||||
{
|
{
|
||||||
parent::__construct($include, $link);
|
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 string $name
|
||||||
* @param array $arguments
|
* @param array $arguments
|
||||||
* @return void
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function __call($name, $arguments)
|
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…
x
Reference in New Issue
Block a user