From 6c94024b8fd60d0ecb4fe9fa109d1bc0ae25ddd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kocsis=20M=C3=A1t=C3=A9?= Date: Fri, 11 Sep 2015 22:57:33 +0200 Subject: [PATCH] Reverting collection filtering --- .../User/JsonApi/Document/UsersDocument.php | 9 - src/JsonApi/Request/Request.php | 20 +- .../AbstractCollectionDocument.php | 29 -- .../Transformer/CollectionFilterTrait.php | 249 ------------------ 4 files changed, 13 insertions(+), 294 deletions(-) delete mode 100644 src/JsonApi/Transformer/CollectionFilterTrait.php diff --git a/examples/User/JsonApi/Document/UsersDocument.php b/examples/User/JsonApi/Document/UsersDocument.php index 3538ad5d..06eb4cd9 100644 --- a/examples/User/JsonApi/Document/UsersDocument.php +++ b/examples/User/JsonApi/Document/UsersDocument.php @@ -67,13 +67,4 @@ public function getLinks() ->setPagination("http://example.com/api/users", $this->domainObject) ; } - - /** - * @param \WoohooLabs\Yin\JsonApi\Request\RequestInterface $request - */ - protected function filterContent(RequestInterface $request) - { - $this->filterCollection($request); - $this->sortCollection($request, "id"); - } } diff --git a/src/JsonApi/Request/Request.php b/src/JsonApi/Request/Request.php index e4717c59..742a26ec 100644 --- a/src/JsonApi/Request/Request.php +++ b/src/JsonApi/Request/Request.php @@ -16,39 +16,39 @@ class Request implements RequestInterface /** * @var \Psr\Http\Message\ServerRequestInterface */ - private $serverRequest; + protected $serverRequest; /** * @var array|null */ - private $includedFields; + protected $includedFields; /** * @var array|null */ - private $includedRelationships; + protected $includedRelationships; /** * @var array|null */ - private $sorting; + protected $sorting; /** * @var array|null */ - private $pagination; + protected $pagination; /** * @var array|null */ - private $filtering; + protected $filtering; /** * @param \Psr\Http\Message\ServerRequestInterface $request */ public function __construct(ServerRequestInterface $request) { - $this->serverRequest = $request->withParsedBody(json_decode($request->getBody(), true)); + $this->serverRequest = $request; } /** @@ -754,6 +754,12 @@ public function withUploadedFiles(array $uploadedFiles) */ public function getParsedBody() { + if ($this->serverRequest->getBody()->getSize() && $this->serverRequest->getParsedBody() === null) { + $this->serverRequest = $this->serverRequest->withParsedBody( + json_decode($this->serverRequest->getBody(), true) + ); + } + return $this->serverRequest->getParsedBody(); } diff --git a/src/JsonApi/Transformer/AbstractCollectionDocument.php b/src/JsonApi/Transformer/AbstractCollectionDocument.php index 493ab803..4db1877a 100644 --- a/src/JsonApi/Transformer/AbstractCollectionDocument.php +++ b/src/JsonApi/Transformer/AbstractCollectionDocument.php @@ -5,8 +5,6 @@ abstract class AbstractCollectionDocument extends AbstractCompoundDocument { - use CollectionFilterTrait; - /** * @var \Traversable|array */ @@ -25,31 +23,6 @@ public function __construct(AbstractResourceTransformer $transformer) $this->transformer = $transformer; } - /** - * @param \WoohooLabs\Yin\JsonApi\Request\RequestInterface $request - */ - abstract protected function filterContent(RequestInterface $request); - - /** - * @param \WoohooLabs\Yin\JsonApi\Request\RequestInterface $request - * @param string $defaultSorting - */ - protected function sortCollection(RequestInterface $request, $defaultSorting = "") - { - if (empty($request->getQueryParam("sort"))) { - $request = $request->withQueryParam("sort", $defaultSorting); - } - $this->sortByFields($this->data, $this->included, $request->getSortingByFields()); - } - - /** - * @param \WoohooLabs\Yin\JsonApi\Request\RequestInterface $request - */ - protected function filterCollection(RequestInterface $request) - { - $this->filterByFields($this->data, $this->included, $request->getFilteringByFields()); - } - /** * Sets the value of the "data" and "included" properties based on the "resource" property. * @@ -62,8 +35,6 @@ protected function setContent(RequestInterface $request) foreach ($this->domainObject as $item) { $this->data[] = $this->transformer->transformToResource($item, $request, $this->included); } - - $this->filterContent($request); } /** diff --git a/src/JsonApi/Transformer/CollectionFilterTrait.php b/src/JsonApi/Transformer/CollectionFilterTrait.php deleted file mode 100644 index 4cef4d32..00000000 --- a/src/JsonApi/Transformer/CollectionFilterTrait.php +++ /dev/null @@ -1,249 +0,0 @@ -getResourceField($a, $field, $included); - $b = $this->getResourceField($b, $field, $included); - } - - $result = $this->performSortingComparison($sorting["field"], $a, $b); - if ($result !== 0) { - return $result * $sorting["direction"]; - } - } - - return 0; - }; - - usort($data, $comparator); - } - - /** - * @param array $data - * @param \WoohooLabs\Yin\JsonApi\Schema\Included $included - * @param array $filteringFields - * @return array - */ - protected function filterByFields(&$data, Included $included, array $filteringFields) - { - $filter = function ($item) use ($filteringFields, $included) { - foreach ($filteringFields as $filtering) { - foreach ($filtering["field"] as $field) { - $item = $this->getResourceField($item, $field, $included); - } - - $result = $this->performFilteringComparison( - $filtering["originalField"], - $item, - $filtering["operator"], - $filtering["value"] - ); - if ($result === false) { - return false; - } - } - - return true; - }; - - // Filtering primary data and collecting relationship identifiers which can be included - $remainingIncludableRelationships = []; - foreach ($data as $key => $resource) { - if ($filter($resource) === false) { - unset($data[$key]); - } else { - $this->addRelationshipIdentifiers($remainingIncludableRelationships, $resource); - } - } - - // Filtering included data - $included->filterResources(function ($type, $id) use ($remainingIncludableRelationships) { - return isset($remainingIncludableRelationships[$type][$id]) ? true : false; - }); - } - - /** - * @param array $relationships - * @param array $resource - */ - protected function addRelationshipIdentifiers(&$relationships, $resource) - { - if (isset($resource["relationships"]) === false) { - return; - } - - foreach ($resource["relationships"] as $relationship) { - if (isset($relationship["data"]) && $this->isAssociativeArray($relationship["data"]) === false) { - foreach ($relationship["data"] as $item) { - if (isset($item["type"]) && isset($item["id"])) { - $relationships[$item["type"]][$item["id"]] = true; - } - } - } - } - } - - /** - * @param mixed $resource - * @param string $field - * @param \WoohooLabs\Yin\JsonApi\Schema\Included $included - * @return string|null - */ - protected function getResourceField($resource, $field, Included $included) - { - if ($resource === null) { - return null; - } - - if ($field === "type" || $field === "id") { - return isset($resource[$field]) ? $resource[$field] : null; - } - - if (isset($resource["attributes"][$field])) { - return $resource["attributes"][$field]; - } - - if (isset($resource["relationships"][$field]["data"]["type"]) && - isset($resource["relationships"][$field]["data"]["id"]) - ) { - $type = $resource["relationships"][$field]["data"]["type"]; - $id = $resource["relationships"][$field]["data"]["id"]; - return $included->getResource($type, $id); - } - - return null; - } - - /** - * @param string $fieldName - * @param mixed $x - * @param string $operator - * @param mixed $y - * @return bool - * @throws \WoohooLabs\Yin\JsonApi\Exception\FilteringCriteriaUnsupported - */ - protected function performFilteringComparison($fieldName, $x, $operator, $y) - { - if (($x !== null && is_scalar($x) === false) || ($y !== null && is_scalar($y) === false)) { - throw new FilteringCriteriaUnsupported($fieldName); - } - - switch ($operator) { - case "=": - return $this->equals($x, $y); - case "<": - return $x < $y; - case "<=": - return $x <= $y; - case ">": - return $x > $y; - case ">=": - return $x >= $y; - } - - return false; - } - - /** - * @param string $fieldName - * @param mixed $x - * @param mixed $y - * @return int - * @throws \WoohooLabs\Yin\JsonApi\Exception\SortingCriteriaUnsupported - */ - protected function performSortingComparison($fieldName, $x, $y) - { - if (($x !== null && is_scalar($x) === false) || ($y !== null && is_scalar($y) === false)) { - throw new SortingCriteriaUnsupported($fieldName); - } - - if ($x === null || $y === null) { - return $this->compareNull($x, $y); - } - - if (is_bool($x) && is_bool($y)) { - return $this->compareNumeric($x, $y); - } - - if (is_numeric($x) && is_numeric($y)) { - return $this->compareNumeric($x, $y); - } - - return $this->compareString($x, $y); - } - - /** - * @param mixed $x - * @param mixed $y - * @return bool - */ - protected function equals($x, $y) - { - if (is_bool($x)) { - $y = boolval($y); - } elseif (is_long($x)) { - $y = intval($y); - } elseif (is_double($x)) { - $y = doubleval($y); - } elseif (is_string($x)) { - $y = print_r($y, true); - } - - return $x === $y; - } - - /** - * @param mixed $x - * @param mixed $y - * @return int - */ - private function compareNull($x, $y) - { - return $x !== null && $y === null ? 1 : ($y === null && $y === null ? 0 : -1); - } - - /** - * @param int|float|double|bool $x - * @param int|float|double|bool $y - * @return int - */ - private function compareNumeric($x, $y) - { - return $x > $y ? 1 : ($x === $y ? 0 : -1); - } - - /** - * @param bool $x - * @param bool $y - * @return int - */ - private function compareString($x, $y) - { - return strnatcasecmp($x, $y); - } - - /** - * @param array $array - * @return bool - */ - private function isAssociativeArray(array $array) - { - return (bool)count(array_filter(array_keys($array), 'is_string')); - } -}