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

Refactor both DocumentBuilders to one DocumentFactory #52

Merged
merged 3 commits into from
Jul 5, 2019
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
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Added

* Added facades for `CollectionDocumentBuilder`, `ItemDocumentBuilder`, `ItemHydrator` and `TypeMapper`.
* Added `DocumentFactory`.
* Added facades for `DocumentFactory`, `ItemHydrator` and `TypeMapper`.

### Changed

* The `TypeMapper` now checks if the class exists in the setter instead of the getter.
* The `ItemHydrator` now also hydrates the id if provided.
* Added `hasType`, `hasAttributes`, `hasRelationships` and `getRelations` to `ItemInterface`.
* Removed `canBeIncluded` and `getIncluded` from `ItemInterface` as the `DocumentFactory` is now responsible for gathering the included items.

### Removed

* Removed `CollectionDocumentBuilder` and `ItemDocumentBuilder` in favor of `DocumentFactory`.

## [0.18.0] - 2019-07-01

Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@
"Swis\\JsonApi\\Client\\Providers\\ServiceProvider"
],
"aliases":{
"CollectionDocumentBuilder": "Swis\\JsonApi\\Client\\Facades\\CollectionDocumentBuilderFacade",
"ItemDocumentBuilder": "Swis\\JsonApi\\Client\\Facades\\ItemDocumentBuilderFacade",
"DocumentFactory": "Swis\\JsonApi\\Client\\Facades\\DocumentFactoryFacade",
"ItemHydrator": "Swis\\JsonApi\\Client\\Facades\\ItemHydratorFacade",
"TypeMapper": "Swis\\JsonApi\\Client\\Facades\\TypeMapperFacade"
}
Expand Down
16 changes: 0 additions & 16 deletions src/CollectionDocumentBuilder.php

This file was deleted.

93 changes: 93 additions & 0 deletions src/DocumentFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Swis\JsonApi\Client;

use Swis\JsonApi\Client\Interfaces\DataInterface;
use Swis\JsonApi\Client\Interfaces\DocumentInterface;
use Swis\JsonApi\Client\Interfaces\ItemInterface;

class DocumentFactory
{
/**
* @param \Swis\JsonApi\Client\Interfaces\DataInterface $data
*
* @return \Swis\JsonApi\Client\Interfaces\DocumentInterface
*/
public function make(DataInterface $data): DocumentInterface
{
if ($data instanceof ItemInterface) {
$document = new ItemDocument();
} elseif ($data instanceof Collection) {
$document = new CollectionDocument();
} else {
throw new \InvalidArgumentException(sprintf('%s is not supported as input', get_class($data)));
}
JaZo marked this conversation as resolved.
Show resolved Hide resolved

return $document->setData($data)->setIncluded($this->getIncluded($data));
}

/**
* @param \Swis\JsonApi\Client\Interfaces\DataInterface $data
*
* @return \Swis\JsonApi\Client\Collection
*/
private function getIncluded(DataInterface $data): Collection
{
return Collection::wrap($data)
->flatMap(
function (ItemInterface $item) {
return $this->getIncludedFromItem($item);
}
)
->unique(
static function (ItemInterface $item) {
return sprintf('%s:%s', $item->getType(), $item->getId());
}
)
->values();
}

/**
* @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
*
* @return \Swis\JsonApi\Client\Collection
*/
private function getIncludedFromItem(ItemInterface $item): Collection
{
return Collection::make($item->getRelations())
->reject(
static function ($relationship) {
/* @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface $relationship */
return $relationship->shouldOmitIncluded() || !$relationship->hasIncluded();
}
)
->flatMap(
static function ($relationship) {
/* @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface $relationship */
return Collection::wrap($relationship->getIncluded());
}
)
->flatMap(
function (ItemInterface $item) {
return Collection::wrap($item)->merge($this->getIncludedFromItem($item));
}
)
->filter(
function (ItemInterface $item) {
return $this->itemCanBeIncluded($item);
}
);
}

/**
* @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item
*
* @return bool
*/
private function itemCanBeIncluded(ItemInterface $item): bool
{
return $item->hasType()
&& $item->hasId()
&& ($item->hasAttributes() || $item->hasRelationships());
}
}
22 changes: 0 additions & 22 deletions src/Facades/CollectionDocumentBuilderFacade.php

This file was deleted.

22 changes: 22 additions & 0 deletions src/Facades/DocumentFactoryFacade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Swis\JsonApi\Client\Facades;

use Illuminate\Support\Facades\Facade;
use Swis\JsonApi\Client\DocumentFactory;

/**
* @method static \Swis\JsonApi\Client\Interfaces\DocumentInterface make(\Swis\JsonApi\Client\Interfaces\DataInterface $data)
*
* @see \Swis\JsonApi\Client\DocumentFactory
*/
class DocumentFactoryFacade extends Facade
{
/**
* {@inheritdoc}
*/
protected static function getFacadeAccessor()
{
return DocumentFactory::class;
}
}
22 changes: 0 additions & 22 deletions src/Facades/ItemDocumentBuilderFacade.php

This file was deleted.

26 changes: 17 additions & 9 deletions src/Interfaces/ItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public function setId(? string $id);
*/
public function getType(): string;

/**
* @return bool
*/
public function hasType(): bool;

/**
* @param string $type
*
Expand Down Expand Up @@ -97,6 +102,16 @@ public function getAttribute($key);
*/
public function setAttribute($key, $value);

/**
* @return bool
*/
public function hasAttributes(): bool;

/**
* @return bool
*/
public function hasRelationships(): bool;

/**
* @return array
*/
Expand All @@ -115,14 +130,7 @@ public function getAvailableRelations(): array;
public function setRelation(string $relation, DataInterface $value, Links $links = null, Meta $meta = null);

/**
* @TODO: MEGA TODO. Set up a serializer for the Item so that we can remove this, getRelationships etc
*
* @return \Swis\JsonApi\Client\Collection
*/
public function getIncluded();

/**
* @return bool
* @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface[]
*/
public function canBeIncluded(): bool;
public function getRelations(): array;
}
74 changes: 18 additions & 56 deletions src/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,66 +161,12 @@ public function getRelationships(): array
return $relationships;
}

/**
* @TODO: MEGA TODO. Set up a serializer for the Item so that we can remove this, getRelationships etc
*
* @return \Swis\JsonApi\Client\Collection
*/
public function getIncluded(): Collection
{
$included = new Collection();

foreach ($this->relationships as $name => $relationship) {
if ($relationship->shouldOmitIncluded() || !$relationship->hasIncluded()) {
continue;
}

if ($relationship instanceof OneRelationInterface) {
/** @var \Swis\JsonApi\Client\Interfaces\ItemInterface $item */
$item = $relationship->getIncluded();
if ($item->canBeIncluded()) {
$included->push($item->toJsonApiArray());
}
$included = $included->merge($item->getIncluded());
} elseif ($relationship instanceof ManyRelationInterface) {
$relationship->getIncluded()->each(
function (ItemInterface $item) use (&$included) {
if ($item->canBeIncluded()) {
$included->push($item->toJsonApiArray());
}
$included = $included->merge($item->getIncluded());
}
);
}
}

return $included
->unique(
function (array $item) {
return $item['type'].':'.$item['id'];
}
)
->values();
}

/**
* @return bool
*/
public function canBeIncluded(): bool
public function hasRelationships(): bool
{
if (empty($this->getType())) {
return false;
}

if (null === $this->getId()) {
return false;
}

if (empty($this->relationships) && empty($this->toArray())) {
return false;
}

return true;
return !empty($this->getRelationships());
JaZo marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -261,6 +207,14 @@ public function hasAttribute($key): bool
return array_key_exists($key, $this->attributes);
}

/**
* @return bool
*/
public function hasAttributes(): bool
{
return !empty($this->toArray());
JaZo marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @param string $key
* @param mixed $value
Expand Down Expand Up @@ -510,4 +464,12 @@ public function setRelation(string $relation, DataInterface $value, Links $links

return $this;
}

/**
* @return array
*/
public function getRelations(): array
{
return $this->relationships;
}
}
35 changes: 0 additions & 35 deletions src/ItemDocumentBuilder.php

This file was deleted.

Loading