This repository has been archived by the owner on Jan 2, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 80
Add resource polymorphism #143
Open
antonkomarev
wants to merge
4
commits into
tobyzerner:master
Choose a base branch
from
cybercog:feature/add-resource-polymorphism
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
|
||
return new $this->serializers[$class](); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.