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

allow for null values to be passed to Scope #553

Merged
merged 4 commits into from
Apr 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 11 additions & 2 deletions src/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,20 @@ protected function serializeResource(Serializer $serializer, $data): ?array
$resourceKey = $this->resource->getResourceKey();

if ($this->resource instanceof Collection) {
return $serializer->collection($resourceKey, $data);
if (!empty($resourceKey)) {
return $serializer->collection($resourceKey, $data);
}

return $serializer->collection(null, $data);
}

if ($this->resource instanceof Item) {
return $serializer->item($resourceKey, $data);
// this is where it breaks now.
if (!empty($resourceKey)) {
return $serializer->item($resourceKey, $data);
}

return $serializer->item(null, $data);
}

return $serializer->null();
Expand Down
4 changes: 2 additions & 2 deletions src/Serializer/ArraySerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ class ArraySerializer extends SerializerAbstract
/**
* {@inheritDoc}
*/
public function collection(string $resourceKey, array $data): array
public function collection(?string $resourceKey, array $data): array
{
return [$resourceKey ?: 'data' => $data];
}

/**
* {@inheritDoc}
*/
public function item(string $resourceKey, array $data): array
public function item(?string $resourceKey, array $data): array
{
return $data;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Serializer/DataArraySerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ class DataArraySerializer extends ArraySerializer
/**
* {@inheritDoc}
*/
public function collection(string $resourceKey, array $data): array
public function collection(?string $resourceKey, array $data): array
{
return ['data' => $data];
}

/**
* {@inheritDoc}
*/
public function item(string $resourceKey, array $data): array
public function item(?string $resourceKey, array $data): array
{
return ['data' => $data];
}
Expand Down
4 changes: 2 additions & 2 deletions src/Serializer/JsonApiSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(?string $baseUrl = null)
/**
* {@inheritDoc}
*/
public function collection(string $resourceKey, array $data): array
public function collection(?string $resourceKey, array $data): array
{
$resources = [];

Expand All @@ -42,7 +42,7 @@ public function collection(string $resourceKey, array $data): array
/**
* {@inheritDoc}
*/
public function item(string $resourceKey, array $data): array
public function item(?string $resourceKey, array $data): array
{
$id = $this->getIdFromData($data);

Expand Down
4 changes: 2 additions & 2 deletions src/Serializer/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ interface Serializer
/**
* Serialize a collection.
*/
public function collection(string $resourceKey, array $data): array;
public function collection(?string $resourceKey, array $data): array;

/**
* Serialize an item.
*/
public function item(string $resourceKey, array $data): array;
public function item(?string $resourceKey, array $data): array;

/**
* Serialize null resource.
Expand Down
76 changes: 76 additions & 0 deletions test/Serializer/CustomArraySerializerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace League\Fractal\Test\Serializer;

use League\Fractal\Manager;
use League\Fractal\Resource\Item;
use League\Fractal\Scope;
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Test\Stub\Serializer\RootSerializer;
use League\Fractal\Test\Stub\Transformer\GenericBookNoResourceKeyTransformer;
use PHPUnit\Framework\TestCase;

class CustomArraySerializerTest extends TestCase
{
public function testAllowNullResourceKey()
{
$manager = new Manager();
$manager->parseIncludes('author');
$manager->setSerializer(new RootSerializer());

$bookData = [
'title' => 'Foo',
'year' => '1991',
'_author' => [
'name' => 'Dave',
],
];

$resource = new Item($bookData, new GenericBookNoResourceKeyTransformer(), 'data');
$scope = new Scope($manager, $resource);

$expected = [
'data' => [
'title' => 'Foo',
'year' => 1991,
'author' => [
'name' => 'Dave',
],
],
];

$this->assertSame($expected, $scope->toArray());
}

public function testMismatchedResourceKey()
{
$manager = new Manager();
$manager->parseIncludes('author');
$manager->setSerializer(new RootSerializer());

$bookData = [
'title' => 'Foo',
'year' => '1991',
'_author' => [
'name' => 'Dave',
],
];

$resource = new Item($bookData, new GenericBookNoResourceKeyTransformer(), 'data');
$scope = new Scope($manager, $resource);

$expected = [
'data' => [
'title' => 'Foo',
'year' => 1991,
'author' => [
'data' => [
'name' => 'Dave',
]
],
],
];

$this->assertNotSame($expected, $scope->toArray());
}
}
11 changes: 11 additions & 0 deletions test/Serializer/DataArraySerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use League\Fractal\Resource\NullResource;
use League\Fractal\Scope;
use League\Fractal\Serializer\DataArraySerializer;
use League\Fractal\Test\Stub\Serializer\RootSerializer;
use League\Fractal\Test\Stub\Transformer\GenericBookNoResourceKeyTransformer;
use League\Fractal\Test\Stub\Transformer\GenericBookTransformer;
use Mockery;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -355,6 +357,15 @@ public function testSerializingNullResource()
$this->assertSame($expected, $scope->toArray());
}

public function testCanPassNullValueToSerializer()
{
$testClass = new \stdClass();
$testClass->name = 'test';
$testClass->email = '[email protected]';


}

public function tearDown(): void
{
Mockery::close();
Expand Down
32 changes: 32 additions & 0 deletions test/Stub/Serializer/RootSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace League\Fractal\Test\Stub\Serializer;

use League\Fractal\Serializer\DataArraySerializer;

class RootSerializer extends DataArraySerializer
{
/**
* Serialize a collection.
*
* @param string $resourceKey
* @param array $data
* @return array
*/
public function collection($resourceKey, array $data): array
{
return is_null($resourceKey) ? $data : [$resourceKey => $data];
}

/**
* Serialize an item.
*
* @param string $resourceKey
* @param array $data
* @return array
*/
public function item($resourceKey, array $data): array
{
return is_null($resourceKey) ? $data : [$resourceKey => $data];
}
}
29 changes: 29 additions & 0 deletions test/Stub/Transformer/GenericBookNoResourceKeyTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace League\Fractal\Test\Stub\Transformer;

use League\Fractal\TransformerAbstract;

class GenericBookNoResourceKeyTransformer extends TransformerAbstract
{
protected array $availableIncludes = [
'author',
];

public function transform(array $book): array
{
$book['year'] = (int) $book['year'];
unset($book['_author']);

return $book;
}

public function includeAuthor(array $book)
{
if (! isset($book['_author'])) {
return;
}

return $this->item($book['_author'], new GenericAuthorTransformer());
}
}