Skip to content
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.

Add resource polymorphism #143

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
39 changes: 39 additions & 0 deletions src/AbstractSerializerRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of JSON-API.
*
* (c) Toby Zerner <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tobscure\JsonApi;

use RuntimeException;

abstract class AbstractSerializerRegistry implements SerializerRegistryInterface
{
/**
* @var array
*/
protected $serializers = [];

/**
* Instantiate serializer from the serializable object.
*
* @param object $serializable
* @return \Tobscure\JsonApi\SerializerInterface
*/
public function getFromSerializable($serializable)
{
$class = get_class($serializable);

if (! isset($this->serializers[$class])) {
throw new RuntimeException("Serializer with name `{$class}` is not exists");
Copy link
Author

Choose a reason for hiding this comment

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

I think we need to throw custom exception here.

}

return new $this->serializers[$class]();
}
}
125 changes: 125 additions & 0 deletions src/PolymorphicCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

/*
* This file is part of JSON-API.
*
* (c) Toby Zerner <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tobscure\JsonApi;

class PolymorphicCollection implements ElementInterface
{
/**
* @var array
*/
protected $resources = [];

/**
* Create a new collection instance.
*
* @param mixed $data
* @param \Tobscure\JsonApi\SerializerRegistryInterface $serializers
*/
public function __construct($data, SerializerRegistryInterface $serializers)
{
$this->resources = $this->buildResources($data, $serializers);
}

/**
* Convert an array of raw data to Resource objects.
*
* @param mixed $data
* @param \Tobscure\JsonApi\SerializerRegistryInterface $serializers
* @return \Tobscure\JsonApi\Resource[]
*/
protected function buildResources($data, SerializerRegistryInterface $serializers)
{
$resources = [];

foreach ($data as $resource) {
if (! ($resource instanceof Resource)) {
$resource = new Resource($resource, $serializers->getFromSerializable($resource));
}

$resources[] = $resource;
}

return $resources;
}

/**
* {@inheritdoc}
*/
public function getResources()
{
return $this->resources;
}

/**
* Set the resources array.
*
* @param array $resources
*
* @return void
*/
public function setResources($resources)
{
$this->resources = $resources;
}

/**
* Request a relationship to be included for all resources.
*
* @param string|array $relationships
*
* @return $this
*/
public function with($relationships)
{
foreach ($this->resources as $resource) {
$resource->with($relationships);
}

return $this;
}

/**
* Request a restricted set of fields.
*
* @param array|null $fields
*
* @return $this
*/
public function fields($fields)
{
foreach ($this->resources as $resource) {
$resource->fields($fields);
}

return $this;
}

/**
* {@inheritdoc}
*/
public function toArray()
{
return array_map(function (Resource $resource) {
return $resource->toArray();
}, $this->resources);
}

/**
* {@inheritdoc}
*/
public function toIdentifier()
{
return array_map(function (Resource $resource) {
return $resource->toIdentifier();
}, $this->resources);
}
}
26 changes: 26 additions & 0 deletions src/PolymorphicResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of JSON-API.
*
* (c) Toby Zerner <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tobscure\JsonApi;

use Tobscure\JsonApi\Resource as JsonApiResource;

class PolymorphicResource extends JsonApiResource
{
/**
* @param mixed $data
* @param \Tobscure\JsonApi\SerializerRegistryInterface $serializers
*/
public function __construct($data, SerializerRegistryInterface $serializers)
{
parent::__construct($data, $serializers->getFromSerializable($data));
}
}
23 changes: 23 additions & 0 deletions src/SerializerRegistryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of JSON-API.
*
* (c) Toby Zerner <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tobscure\JsonApi;

interface SerializerRegistryInterface
{
/**
* Instantiate serializer from the serializable object.
*
* @param object $serializable
* @return \Tobscure\JsonApi\SerializerInterface
*/
public function getFromSerializable($serializable);
}
65 changes: 65 additions & 0 deletions tests/PolymorphicCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/*
* This file is part of JSON-API.
*
* (c) Toby Zerner <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tobscure\Tests\JsonApi\Element;

use Tobscure\JsonApi\PolymorphicCollection;
use Tobscure\JsonApi\PolymorphicResource;
use Tobscure\Tests\JsonApi\AbstractTestCase;
use Tobscure\Tests\JsonApi\Stubs\Serializables\Bike;
use Tobscure\Tests\JsonApi\Stubs\Serializables\Car;
use Tobscure\Tests\JsonApi\Stubs\Serializables\Garage;
use Tobscure\Tests\JsonApi\Stubs\Serializers\VehicleSerializerRegistry;

class PolymorphicCollectionTest extends AbstractTestCase
{
public function testToArrayReturnsArrayOfResources()
{
$car = new Car(1);
$bike = new Bike(2);
$garage = new Garage(4, [$car, $bike]);
$serializerRegistry = new VehicleSerializerRegistry();

$collection = new PolymorphicCollection($garage->vehicles, $serializerRegistry);

$resource1 = new PolymorphicResource($car, $serializerRegistry);
$resource2 = new PolymorphicResource($bike, $serializerRegistry);
$this->assertSame([$resource1->toArray(), $resource2->toArray()], $collection->toArray());
}

public function testToArrayReturnsArrayOfPolymorphicResources()
{
$car = new Car(1);
$bike = new Bike(2);
$garage = new Garage(4, [$car, $bike]);
$serializerRegistry = new VehicleSerializerRegistry();

$collection = new PolymorphicCollection($garage->vehicles, $serializerRegistry);

$resources = $collection->getResources();
$this->assertSame('cars', $resources[0]->getType());
$this->assertSame('bikes', $resources[1]->getType());
}

public function testToIdentifierReturnsArrayOfResourceIdentifiers()
{
$car = new Car(1);
$bike = new Bike(2);
$garage = new Garage(4, [$car, $bike]);
$serializerRegistry = new VehicleSerializerRegistry();

$collection = new PolymorphicCollection($garage->vehicles, $serializerRegistry);

$resource1 = new PolymorphicResource($car, $serializerRegistry);
$resource2 = new PolymorphicResource($bike, $serializerRegistry);
$this->assertSame([$resource1->toIdentifier(), $resource2->toIdentifier()], $collection->toIdentifier());
}
}
34 changes: 34 additions & 0 deletions tests/PolymorphicResourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/*
* This file is part of JSON-API.
*
* (c) Toby Zerner <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tobscure\Tests\JsonApi\Element;

use Tobscure\JsonApi\PolymorphicResource;
use Tobscure\Tests\JsonApi\AbstractTestCase;
use Tobscure\Tests\JsonApi\Stubs\Serializables\Bike;
use Tobscure\Tests\JsonApi\Stubs\Serializables\Car;
use Tobscure\Tests\JsonApi\Stubs\Serializers\VehicleSerializerRegistry;

class PolymorphicResourceTest extends AbstractTestCase
{
public function testPolymorphicResourceType()
{
$car = new Car(123);
$bike = new Bike(234);
$serializerRegistry = new VehicleSerializerRegistry();

$resource1 = new PolymorphicResource($car, $serializerRegistry);
$resource2 = new PolymorphicResource($bike, $serializerRegistry);

$this->assertSame('cars', $resource1->getType());
$this->assertSame('bikes', $resource2->getType());
}
}
24 changes: 24 additions & 0 deletions tests/Stubs/Serializables/Bike.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of JSON-API.
*
* (c) Toby Zerner <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tobscure\Tests\JsonApi\Stubs\Serializables;

class Bike
{
public $id;

public $name = 'Bike';

public function __construct($id)
{
$this->id = $id;
}
}
24 changes: 24 additions & 0 deletions tests/Stubs/Serializables/Car.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of JSON-API.
*
* (c) Toby Zerner <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tobscure\Tests\JsonApi\Stubs\Serializables;

class Car
{
public $id;

public $name = 'Car';

public function __construct($id)
{
$this->id = $id;
}
}
25 changes: 25 additions & 0 deletions tests/Stubs/Serializables/Garage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of JSON-API.
*
* (c) Toby Zerner <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Tobscure\Tests\JsonApi\Stubs\Serializables;

class Garage
{
public $id;

public $vehicles = [];

public function __construct($id, array $vehicles = [])
{
$this->id = $id;
$this->vehicles = $vehicles;
}
}
Loading