Rename ApiSerializer's mutate to attributes (#2578)

This commit is contained in:
Sami Mazouz 2021-02-05 19:21:36 +01:00 committed by GitHub
parent 8366ec720e
commit 0595aba76a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 25 deletions

View File

@ -43,7 +43,7 @@ abstract class AbstractSerializer extends BaseAbstractSerializer
/** /**
* @var callable[] * @var callable[]
*/ */
protected static $mutators = []; protected static $attributeMutators = [];
/** /**
* @var array * @var array
@ -87,8 +87,8 @@ abstract class AbstractSerializer extends BaseAbstractSerializer
$attributes = $this->getDefaultAttributes($model); $attributes = $this->getDefaultAttributes($model);
foreach (array_reverse(array_merge([static::class], class_parents($this))) as $class) { foreach (array_reverse(array_merge([static::class], class_parents($this))) as $class) {
if (isset(static::$mutators[$class])) { if (isset(static::$attributeMutators[$class])) {
foreach (static::$mutators[$class] as $callback) { foreach (static::$attributeMutators[$class] as $callback) {
$attributes = array_merge( $attributes = array_merge(
$attributes, $attributes,
$callback($this, $model, $attributes) $callback($this, $model, $attributes)
@ -281,15 +281,15 @@ abstract class AbstractSerializer extends BaseAbstractSerializer
/** /**
* @param string $serializerClass * @param string $serializerClass
* @param callable $mutator * @param callable $callback
*/ */
public static function addMutator(string $serializerClass, callable $mutator) public static function addAttributeMutator(string $serializerClass, callable $callback)
{ {
if (! isset(static::$mutators[$serializerClass])) { if (! isset(static::$attributeMutators[$serializerClass])) {
static::$mutators[$serializerClass] = []; static::$attributeMutators[$serializerClass] = [];
} }
static::$mutators[$serializerClass][] = $mutator; static::$attributeMutators[$serializerClass][] = $callback;
} }
/** /**

View File

@ -17,8 +17,8 @@ use Illuminate\Contracts\Container\Container;
class ApiSerializer implements ExtenderInterface class ApiSerializer implements ExtenderInterface
{ {
private $serializerClass; private $serializerClass;
private $attribute = [];
private $attributes = []; private $attributes = [];
private $mutators = [];
private $relationships = []; private $relationships = [];
/** /**
@ -46,7 +46,7 @@ class ApiSerializer implements ExtenderInterface
*/ */
public function attribute(string $name, $callback) public function attribute(string $name, $callback)
{ {
$this->attributes[$name] = $callback; $this->attribute[$name] = $callback;
return $this; return $this;
} }
@ -67,13 +67,35 @@ class ApiSerializer implements ExtenderInterface
* *
* @return self * @return self
*/ */
public function mutate($callback) public function attributes($callback)
{ {
$this->mutators[] = $callback; $this->attributes[] = $callback;
return $this; return $this;
} }
/**
* Add to or modify the attributes array of this serializer.
*
* @param callable|string $callback
*
* The callback can be a closure or an invokable class, and should accept:
* - $serializer: An instance of this serializer.
* - $model: An instance of the model being serialized.
* - $attributes: An array of existing attributes.
*
* The callable should return:
* - An array of additional attributes to merge with the existing array.
* Or a modified $attributes array.
*
* @deprecated in beta 16, removed in beta 17
* @return self
*/
public function mutate($callback)
{
return $this->attributes($callback);
}
/** /**
* Establish a simple hasOne relationship from this serializer to another serializer. * Establish a simple hasOne relationship from this serializer to another serializer.
* This represents a one-to-one relationship. * This represents a one-to-one relationship.
@ -133,9 +155,9 @@ class ApiSerializer implements ExtenderInterface
public function extend(Container $container, Extension $extension = null) public function extend(Container $container, Extension $extension = null)
{ {
if (! empty($this->attributes)) { if (! empty($this->attribute)) {
$this->mutators[] = function ($serializer, $model, $attributes) use ($container) { $this->attributes[] = function ($serializer, $model, $attributes) use ($container) {
foreach ($this->attributes as $attributeName => $callback) { foreach ($this->attribute as $attributeName => $callback) {
$callback = ContainerUtil::wrapCallback($callback, $container); $callback = ContainerUtil::wrapCallback($callback, $container);
$attributes[$attributeName] = $callback($serializer, $model, $attributes); $attributes[$attributeName] = $callback($serializer, $model, $attributes);
@ -145,10 +167,10 @@ class ApiSerializer implements ExtenderInterface
}; };
} }
foreach ($this->mutators as $mutator) { foreach ($this->attributes as $callback) {
$mutator = ContainerUtil::wrapCallback($mutator, $container); $callback = ContainerUtil::wrapCallback($callback, $container);
AbstractSerializer::addMutator($this->serializerClass, $mutator); AbstractSerializer::addAttributeMutator($this->serializerClass, $callback);
} }
foreach ($this->relationships as $serializerClass => $relationships) { foreach ($this->relationships as $serializerClass => $relationships) {

View File

@ -39,7 +39,7 @@ class Settings implements ExtenderInterface
public function extend(Container $container, Extension $extension = null) public function extend(Container $container, Extension $extension = null)
{ {
if (! empty($this->settings)) { if (! empty($this->settings)) {
AbstractSerializer::addMutator( AbstractSerializer::addAttributeMutator(
ForumSerializer::class, ForumSerializer::class,
function () use ($container) { function () use ($container) {
$settings = $container->make(SettingsRepositoryInterface::class); $settings = $container->make(SettingsRepositoryInterface::class);

View File

@ -74,7 +74,7 @@ class ApiSerializerTest extends TestCase
{ {
$this->extend( $this->extend(
(new Extend\ApiSerializer(ForumSerializer::class)) (new Extend\ApiSerializer(ForumSerializer::class))
->mutate(function () { ->attributes(function () {
return [ return [
'customAttribute' => true 'customAttribute' => true
]; ];
@ -101,7 +101,7 @@ class ApiSerializerTest extends TestCase
{ {
$this->extend( $this->extend(
(new Extend\ApiSerializer(ForumSerializer::class)) (new Extend\ApiSerializer(ForumSerializer::class))
->mutate(CustomAttributesInvokableClass::class) ->attributes(CustomAttributesInvokableClass::class)
); );
$this->app(); $this->app();
@ -124,7 +124,7 @@ class ApiSerializerTest extends TestCase
{ {
$this->extend( $this->extend(
(new Extend\ApiSerializer(BasicUserSerializer::class)) (new Extend\ApiSerializer(BasicUserSerializer::class))
->mutate(function () { ->attributes(function () {
return [ return [
'customAttribute' => true 'customAttribute' => true
]; ];
@ -151,13 +151,13 @@ class ApiSerializerTest extends TestCase
{ {
$this->extend( $this->extend(
(new Extend\ApiSerializer(BasicUserSerializer::class)) (new Extend\ApiSerializer(BasicUserSerializer::class))
->mutate(function () { ->attributes(function () {
return [ return [
'customAttribute' => 'initialValue' 'customAttribute' => 'initialValue'
]; ];
}), }),
(new Extend\ApiSerializer(UserSerializer::class)) (new Extend\ApiSerializer(UserSerializer::class))
->mutate(function () { ->attributes(function () {
return [ return [
'customAttribute' => 'newValue' 'customAttribute' => 'newValue'
]; ];
@ -294,7 +294,7 @@ class ApiSerializerTest extends TestCase
(new Extend\ApiSerializer(BasicUserSerializer::class)) (new Extend\ApiSerializer(BasicUserSerializer::class))
->attribute('someCustomAttribute', function () { ->attribute('someCustomAttribute', function () {
return 'newValue'; return 'newValue';
})->mutate(function () { })->attributes(function () {
return [ return [
'someCustomAttribute' => 'initialValue', 'someCustomAttribute' => 'initialValue',
'someOtherCustomAttribute' => 'initialValue', 'someOtherCustomAttribute' => 'initialValue',