From a4b24fcda57150d4e0bf6066acca4397c4ac622f Mon Sep 17 00:00:00 2001 From: Frederick Vanbrabant Date: Fri, 26 Feb 2016 14:11:27 +0100 Subject: [PATCH 1/5] Dived the methods into smaller chunks. --- src/Serializer/JsonApiSerializer.php | 218 +++++++++++++++++++-------- 1 file changed, 158 insertions(+), 60 deletions(-) diff --git a/src/Serializer/JsonApiSerializer.php b/src/Serializer/JsonApiSerializer.php index 43f76cb8..8dd638ad 100644 --- a/src/Serializer/JsonApiSerializer.php +++ b/src/Serializer/JsonApiSerializer.php @@ -164,12 +164,8 @@ public function includedData(ResourceInterface $resource, array $data) if ($this->isNull($includeObject) || $this->isEmpty($includeObject)) { continue; } - if ($this->isCollection($includeObject)) { - $includeObjects = $includeObject['data']; - } - else { - $includeObjects = [$includeObject['data']]; - } + + $includeObjects = $this->CreateIncludeObjects($includeObject); foreach ($includeObjects as $object) { $includeType = $object['type']; @@ -232,12 +228,8 @@ public function filterIncludes($includedData, $data) return $includedData; } - if ($this->isCollection($data)) { - $this->setRootObjects($data['data']); - } - else { - $this->setRootObjects([$data['data']]); - } + // Create the RootObjects + $this->CreateRootObjects($data); // Filter out the root objects $filteredIncludes = array_filter($includedData['included'], [$this, 'filterRootObject']); @@ -293,7 +285,7 @@ protected function isRootObject($object) protected function isCollection($data) { return array_key_exists('data', $data) && - array_key_exists(0, $data['data']); + array_key_exists(0, $data['data']); } /** @@ -319,29 +311,18 @@ protected function isEmpty($data) { * @param array $data * @param array $relationships * - * @return mixed + * @return array */ protected function fillRelationships($data, $relationships) { if ($this->isCollection($data)) { foreach ($relationships as $key => $relationship) { - foreach ($relationship as $index => $relationshipData) { - $data['data'][$index]['relationships'][$key] = $relationshipData; - } + $data = $this->FillRelationshipAsCollection($data, $relationship, $key); } } else { // Single resource foreach ($relationships as $key => $relationship) { - $data['data']['relationships'][$key] = $relationship[0]; - - if ($this->shouldIncludeLinks()) { - $data['data']['relationships'][$key] = array_merge([ - 'links' => [ - 'self' => "{$this->baseUrl}/{$data['data']['type']}/{$data['data']['id']}/relationships/$key", - 'related' => "{$this->baseUrl}/{$data['data']['type']}/{$data['data']['id']}/$key", - ], - ], $data['data']['relationships'][$key]); - } + $data = $this->FillRelationshipAsSingleResource($data, $relationship, $key); } } @@ -360,38 +341,7 @@ protected function parseRelationships($includedData) foreach ($includedData as $key => $inclusion) { foreach ($inclusion as $includeKey => $includeObject) { - if (!array_key_exists($includeKey, $relationships)) { - $relationships[$includeKey] = []; - } - - if ($this->isNull($includeObject)) { - $relationship = $this->null(); - } - elseif ($this->isEmpty($includeObject)) { - $relationship = [ - 'data' => [], - ]; - } - elseif ($this->isCollection($includeObject)) { - $relationship = ['data' => []]; - - foreach ($includeObject['data'] as $object) { - $relationship['data'][] = [ - 'type' => $object['type'], - 'id' => $object['id'], - ]; - } - } - else { - $relationship = [ - 'data' => [ - 'type' => $includeObject['data']['type'], - 'id' => $includeObject['data']['id'], - ], - ]; - } - - $relationships[$includeKey][$key] = $relationship; + $relationships = $this->buildRelationships($includeKey, $relationships, $includeObject, $key); } } @@ -401,7 +351,7 @@ protected function parseRelationships($includedData) /** * @param array $data * - * @return mixed + * @return integer */ protected function getIdFromData(array $data) { @@ -454,4 +404,152 @@ protected function shouldIncludeLinks() { return $this->baseUrl !== null; } + + /** + * Check if the objects are part of a collection or not + * + * @param $includeObject + * + * @return array + */ + private function CreateIncludeObjects($includeObject) + { + if ($this->isCollection($includeObject)) { + $includeObjects = $includeObject['data']; + + return $includeObjects; + } else { + $includeObjects = [$includeObject['data']]; + + return $includeObjects; + } + } + + /** + * Sets the RootObjects, either as collection or not. + * + * @param $data + */ + private function CreateRootObjects($data) + { + if ($this->isCollection($data)) { + $this->setRootObjects($data['data']); + } else { + $this->setRootObjects([$data['data']]); + } + } + + + /** + * Loops over the relationships of the provided data and formats it + * + * @param $data + * @param $relationship + * @param $nestedDepth + * + * @return array + */ + private function FillRelationshipAsCollection($data, $relationship, $nestedDepth) + { + foreach ($relationship as $index => $relationshipData) { + $data['data'][$index]['relationships'][$nestedDepth] = $relationshipData; + } + + return $data; + } + + + /** + * @param $data + * @param $relationship + * @param $key + * + * @return array + */ + protected function FillRelationshipAsSingleResource($data, $relationship, $key) + { + $data['data']['relationships'][$key] = $relationship[0]; + + if ($this->shouldIncludeLinks()) { + $data['data']['relationships'][$key] = array_merge([ + 'links' => [ + 'self' => "{$this->baseUrl}/{$data['data']['type']}/{$data['data']['id']}/relationships/$key", + 'related' => "{$this->baseUrl}/{$data['data']['type']}/{$data['data']['id']}/$key", + ], + ], $data['data']['relationships'][$key]); + + return $data; + } + return $data; + } + + /** + * @param $includeKey + * @param $relationships + * @param $includeObject + * @param $key + * + * @return array + */ + protected function buildRelationships($includeKey, $relationships, $includeObject, $key) + { + $relationships = $this->addIncludekeyToRelationsIfNotSet($includeKey, $relationships); + + if ($this->isNull($includeObject)) { + $relationship = $this->null(); + } elseif ($this->isEmpty($includeObject)) { + $relationship = [ + 'data' => [], + ]; + } elseif ($this->isCollection($includeObject)) { + $relationship = ['data' => []]; + + $relationship = $this->addIncludedDataToRelationship($includeObject, $relationship); + } else { + $relationship = [ + 'data' => [ + 'type' => $includeObject['data']['type'], + 'id' => $includeObject['data']['id'], + ], + ]; + } + + $relationships[$includeKey][$key] = $relationship; + + return $relationships; + } + + /** + * @param $includeKey + * @param $relationships + * + * @return array + */ + protected function addIncludekeyToRelationsIfNotSet($includeKey, $relationships) + { + if (!array_key_exists($includeKey, $relationships)) { + $relationships[$includeKey] = []; + return $relationships; + } + + return $relationships; + } + + /** + * @param $includeObject + * @param $relationship + * + * @return array + */ + protected function addIncludedDataToRelationship($includeObject, $relationship) + { + foreach ($includeObject['data'] as $object) { + $relationship['data'][] = [ + 'type' => $object['type'], + 'id' => $object['id'], + ]; + } + + return $relationship; + } } From b5e69d4d1159f0f494fd8a23e2261cff4050d1e1 Mon Sep 17 00:00:00 2001 From: Frederick Vanbrabant Date: Fri, 26 Feb 2016 14:14:15 +0100 Subject: [PATCH 2/5] Applied some code style changes. --- src/Serializer/JsonApiSerializer.php | 30 +++++++++++++--------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/Serializer/JsonApiSerializer.php b/src/Serializer/JsonApiSerializer.php index 8dd638ad..e968428f 100644 --- a/src/Serializer/JsonApiSerializer.php +++ b/src/Serializer/JsonApiSerializer.php @@ -30,7 +30,7 @@ public function __construct($baseUrl = null) * Serialize a collection. * * @param string $resourceKey - * @param array $data + * @param array $data * * @return array */ @@ -49,7 +49,7 @@ public function collection($resourceKey, array $data) * Serialize an item. * * @param string $resourceKey - * @param array $data + * @param array $data * * @return array */ @@ -85,13 +85,13 @@ public function item($resourceKey, array $data) */ public function paginator(PaginatorInterface $paginator) { - $currentPage = (int) $paginator->getCurrentPage(); - $lastPage = (int) $paginator->getLastPage(); + $currentPage = (int)$paginator->getCurrentPage(); + $lastPage = (int)$paginator->getLastPage(); $pagination = [ - 'total' => (int) $paginator->getTotal(), - 'count' => (int) $paginator->getCount(), - 'per_page' => (int) $paginator->getPerPage(), + 'total' => (int)$paginator->getTotal(), + 'count' => (int)$paginator->getCount(), + 'per_page' => (int)$paginator->getPerPage(), 'current_page' => $currentPage, 'total_pages' => $lastPage, ]; @@ -151,7 +151,7 @@ public function null() * Serialize the included data. * * @param ResourceInterface $resource - * @param array $data + * @param array $data * * @return array */ @@ -211,14 +211,13 @@ public function injectData($data, $includedData) /** * Hook to manipulate the final sideloaded includes. - * * The JSON API specification does not allow the root object to be included * into the sideloaded `included`-array. We have to make sure it is * filtered out, in case some object links to the root object in a * relationship. * - * @param array $includedData - * @param array $data + * @param array $includedData + * @param array $data * * @return array */ @@ -303,7 +302,8 @@ protected function isNull($data) * * @return bool */ - protected function isEmpty($data) { + protected function isEmpty($data) + { return array_key_exists('data', $data) && $data['data'] === []; } @@ -319,8 +319,7 @@ protected function fillRelationships($data, $relationships) foreach ($relationships as $key => $relationship) { $data = $this->FillRelationshipAsCollection($data, $relationship, $key); } - } - else { // Single resource + } else { // Single resource foreach ($relationships as $key => $relationship) { $data = $this->FillRelationshipAsSingleResource($data, $relationship, $key); } @@ -339,8 +338,7 @@ protected function parseRelationships($includedData) $relationships = []; foreach ($includedData as $key => $inclusion) { - foreach ($inclusion as $includeKey => $includeObject) - { + foreach ($inclusion as $includeKey => $includeObject) { $relationships = $this->buildRelationships($includeKey, $relationships, $includeObject, $key); } } From d6f7242104fd3df375ad9fe1ed93cb33fcfb2756 Mon Sep 17 00:00:00 2001 From: Frederick Vanbrabant Date: Fri, 4 Mar 2016 14:03:02 +0100 Subject: [PATCH 3/5] Made the method names PSR compliant --- src/Serializer/JsonApiSerializer.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Serializer/JsonApiSerializer.php b/src/Serializer/JsonApiSerializer.php index e968428f..373521f9 100644 --- a/src/Serializer/JsonApiSerializer.php +++ b/src/Serializer/JsonApiSerializer.php @@ -165,7 +165,7 @@ public function includedData(ResourceInterface $resource, array $data) continue; } - $includeObjects = $this->CreateIncludeObjects($includeObject); + $includeObjects = $this->createIncludeObjects($includeObject); foreach ($includeObjects as $object) { $includeType = $object['type']; @@ -228,7 +228,7 @@ public function filterIncludes($includedData, $data) } // Create the RootObjects - $this->CreateRootObjects($data); + $this->createRootObjects($data); // Filter out the root objects $filteredIncludes = array_filter($includedData['included'], [$this, 'filterRootObject']); @@ -317,7 +317,7 @@ protected function fillRelationships($data, $relationships) { if ($this->isCollection($data)) { foreach ($relationships as $key => $relationship) { - $data = $this->FillRelationshipAsCollection($data, $relationship, $key); + $data = $this->fillRelationshipAsCollection($data, $relationship, $key); } } else { // Single resource foreach ($relationships as $key => $relationship) { @@ -410,7 +410,7 @@ protected function shouldIncludeLinks() * * @return array */ - private function CreateIncludeObjects($includeObject) + private function createIncludeObjects($includeObject) { if ($this->isCollection($includeObject)) { $includeObjects = $includeObject['data']; @@ -428,7 +428,7 @@ private function CreateIncludeObjects($includeObject) * * @param $data */ - private function CreateRootObjects($data) + private function createRootObjects($data) { if ($this->isCollection($data)) { $this->setRootObjects($data['data']); @@ -447,7 +447,7 @@ private function CreateRootObjects($data) * * @return array */ - private function FillRelationshipAsCollection($data, $relationship, $nestedDepth) + private function fillRelationshipAsCollection($data, $relationship, $nestedDepth) { foreach ($relationship as $index => $relationshipData) { $data['data'][$index]['relationships'][$nestedDepth] = $relationshipData; From 537de407b6bfb1fec16cebf6ed838f026095e4bf Mon Sep 17 00:00:00 2001 From: Frederick Vanbrabant Date: Fri, 4 Mar 2016 14:03:30 +0100 Subject: [PATCH 4/5] Made the internal methods private --- src/Serializer/JsonApiSerializer.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Serializer/JsonApiSerializer.php b/src/Serializer/JsonApiSerializer.php index 373521f9..b9c61473 100644 --- a/src/Serializer/JsonApiSerializer.php +++ b/src/Serializer/JsonApiSerializer.php @@ -464,7 +464,7 @@ private function fillRelationshipAsCollection($data, $relationship, $nestedDepth * * @return array */ - protected function FillRelationshipAsSingleResource($data, $relationship, $key) + private function FillRelationshipAsSingleResource($data, $relationship, $key) { $data['data']['relationships'][$key] = $relationship[0]; @@ -489,7 +489,7 @@ protected function FillRelationshipAsSingleResource($data, $relationship, $key) * * @return array */ - protected function buildRelationships($includeKey, $relationships, $includeObject, $key) + private function buildRelationships($includeKey, $relationships, $includeObject, $key) { $relationships = $this->addIncludekeyToRelationsIfNotSet($includeKey, $relationships); @@ -523,7 +523,7 @@ protected function buildRelationships($includeKey, $relationships, $includeObjec * * @return array */ - protected function addIncludekeyToRelationsIfNotSet($includeKey, $relationships) + private function addIncludekeyToRelationsIfNotSet($includeKey, $relationships) { if (!array_key_exists($includeKey, $relationships)) { $relationships[$includeKey] = []; @@ -539,7 +539,7 @@ protected function addIncludekeyToRelationsIfNotSet($includeKey, $relationships) * * @return array */ - protected function addIncludedDataToRelationship($includeObject, $relationship) + private function addIncludedDataToRelationship($includeObject, $relationship) { foreach ($includeObject['data'] as $object) { $relationship['data'][] = [ From b2fca0ee95dfd87e77863c3e1dbb0e4567ef51e3 Mon Sep 17 00:00:00 2001 From: Frederick Vanbrabant Date: Fri, 4 Mar 2016 14:14:15 +0100 Subject: [PATCH 5/5] Added docblok and small code upgrade --- src/Serializer/JsonApiSerializer.php | 5 +++++ test/Serializer/JsonApiSerializerTest.php | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Serializer/JsonApiSerializer.php b/src/Serializer/JsonApiSerializer.php index b9c61473..fc841161 100644 --- a/src/Serializer/JsonApiSerializer.php +++ b/src/Serializer/JsonApiSerializer.php @@ -20,6 +20,11 @@ class JsonApiSerializer extends ArraySerializer protected $baseUrl; protected $rootObjects; + /** + * JsonApiSerializer constructor. + * + * @param string $baseUrl + */ public function __construct($baseUrl = null) { $this->baseUrl = $baseUrl; diff --git a/test/Serializer/JsonApiSerializerTest.php b/test/Serializer/JsonApiSerializerTest.php index 8050d60e..b759b957 100644 --- a/test/Serializer/JsonApiSerializerTest.php +++ b/test/Serializer/JsonApiSerializerTest.php @@ -5,8 +5,8 @@ use League\Fractal\Resource\Item; use League\Fractal\Scope; use League\Fractal\Serializer\JsonApiSerializer; -use League\Fractal\Test\Stub\Transformer\JsonApiBookTransformer; use League\Fractal\Test\Stub\Transformer\JsonApiAuthorTransformer; +use League\Fractal\Test\Stub\Transformer\JsonApiBookTransformer; class JsonApiSerializerTest extends PHPUnit_Framework_TestCase {