Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename ApiSerializer's mutate to attributes #2578

Merged
merged 2 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions src/Extend/ApiSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
class ApiSerializer implements ExtenderInterface
{
private $serializerClass;
private $attribute = [];
private $attributes = [];
private $mutators = [];
private $relationships = [];

/**
Expand Down Expand Up @@ -46,7 +46,7 @@ public function __construct(string $serializerClass)
*/
public function attribute(string $name, $callback)
{
$this->attributes[$name] = $callback;
$this->attribute[$name] = $callback;

return $this;
}
Expand All @@ -67,13 +67,35 @@ public function attribute(string $name, $callback)
*
* @return self
*/
public function mutate($callback)
public function attributes($callback)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized, we need to deprecate the mutate method, since that was released in the previous version

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is deprecated, look below this code

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I.... don't know how I missed that... What.... 🙈

{
$this->mutators[] = $callback;
$this->attributes[] = $callback;

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.
* This represents a one-to-one relationship.
Expand Down Expand Up @@ -133,9 +155,9 @@ public function relationship(string $name, $callback)

public function extend(Container $container, Extension $extension = null)
{
if (! empty($this->attributes)) {
$this->mutators[] = function ($serializer, $model, $attributes) use ($container) {
foreach ($this->attributes as $attributeName => $callback) {
if (! empty($this->attribute)) {
$this->attributes[] = function ($serializer, $model, $attributes) use ($container) {
foreach ($this->attribute as $attributeName => $callback) {
$callback = ContainerUtil::wrapCallback($callback, $container);

$attributes[$attributeName] = $callback($serializer, $model, $attributes);
Expand All @@ -145,10 +167,10 @@ public function extend(Container $container, Extension $extension = null)
};
}

foreach ($this->mutators as $mutator) {
$mutator = ContainerUtil::wrapCallback($mutator, $container);
foreach ($this->attributes as $callback) {
$callback = ContainerUtil::wrapCallback($callback, $container);

AbstractSerializer::addMutator($this->serializerClass, $mutator);
AbstractSerializer::addMutator($this->serializerClass, $callback);
SychO9 marked this conversation as resolved.
Show resolved Hide resolved
}

foreach ($this->relationships as $serializerClass => $relationships) {
Expand Down
12 changes: 6 additions & 6 deletions tests/integration/extenders/ApiSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function custom_attributes_exist_if_added()
{
$this->extend(
(new Extend\ApiSerializer(ForumSerializer::class))
->mutate(function () {
->attributes(function () {
return [
'customAttribute' => true
];
Expand All @@ -101,7 +101,7 @@ public function custom_attributes_with_invokable_exist_if_added()
{
$this->extend(
(new Extend\ApiSerializer(ForumSerializer::class))
->mutate(CustomAttributesInvokableClass::class)
->attributes(CustomAttributesInvokableClass::class)
);

$this->app();
Expand All @@ -124,7 +124,7 @@ public function custom_attributes_exist_if_added_to_parent_class()
{
$this->extend(
(new Extend\ApiSerializer(BasicUserSerializer::class))
->mutate(function () {
->attributes(function () {
return [
'customAttribute' => true
];
Expand All @@ -151,13 +151,13 @@ public function custom_attributes_prioritize_child_classes()
{
$this->extend(
(new Extend\ApiSerializer(BasicUserSerializer::class))
->mutate(function () {
->attributes(function () {
return [
'customAttribute' => 'initialValue'
];
}),
(new Extend\ApiSerializer(UserSerializer::class))
->mutate(function () {
->attributes(function () {
return [
'customAttribute' => 'newValue'
];
Expand Down Expand Up @@ -294,7 +294,7 @@ public function custom_attributes_can_be_overriden()
(new Extend\ApiSerializer(BasicUserSerializer::class))
->attribute('someCustomAttribute', function () {
return 'newValue';
})->mutate(function () {
})->attributes(function () {
return [
'someCustomAttribute' => 'initialValue',
'someOtherCustomAttribute' => 'initialValue',
Expand Down