From 4f1e2bfd025f22b0114fead03fb5e29010f90ff5 Mon Sep 17 00:00:00 2001 From: Frederic Oudry Date: Fri, 19 Jan 2018 14:58:37 +0100 Subject: [PATCH] feat: Yin 3.0 integration --- .travis.yml | 6 +- composer.json | 21 +++--- src/Request/Request.php | 107 ++++++++++++++++-------------- src/Resources/config/services.xml | 4 +- src/Resources/doc/index.rst | 15 ++++- 5 files changed, 88 insertions(+), 65 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6485be0..c6233b8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,8 @@ language: php php: - - '5.5' - - '5.6' + - '7.1' + - '7.2' install: composer install -script: vendor/bin/phpspec run -n -f dot \ No newline at end of file +script: composer test diff --git a/composer.json b/composer.json index 8261a52..4e8ef92 100644 --- a/composer.json +++ b/composer.json @@ -11,22 +11,25 @@ } ], "require": { - "php": ">=5.5", - "symfony/psr-http-message-bridge": "^0.2.0", + "php": "^7.1.0", + "symfony/psr-http-message-bridge": "^1.0.2", "zendframework/zend-diactoros": "^1.3", - "woohoolabs/yin": "^0.11.0" + "woohoolabs/yin": "^3.0" }, "require-dev": { - "fabpot/php-cs-fixer": "^1.11", - "phpspec/phpspec": "^2.5", - "sensio/framework-extra-bundle": "^3.0" - }, - "suggest": { - "sensio/framework-extra-bundle": "Allows you to use ParamConverter and access to JsonApi instance from your controllers" + "friendsofphp/php-cs-fixer": "^2.0", + "phpspec/phpspec": "^4.3", + "sensio/framework-extra-bundle": "^5.1" }, "autoload": { "psr-4": { "QP\\WoohoolabsYinBundle\\": "src/" } + }, + "scripts": { + "phpspec": "vendor/bin/phpspec run -n -f dot", + "test": [ + "@phpspec" + ] } } diff --git a/src/Request/Request.php b/src/Request/Request.php index 18fde6a..2cdf146 100644 --- a/src/Request/Request.php +++ b/src/Request/Request.php @@ -17,6 +17,10 @@ use WoohooLabs\Yin\JsonApi\Request\Pagination\PagePagination; use WoohooLabs\Yin\JsonApi\Schema\ResourceIdentifier; use WoohooLabs\Yin\JsonApi\Request\RequestInterface; +use WoohooLabs\Yin\JsonApi\Request\Pagination\FixedPageBasedPagination; +use WoohooLabs\Yin\JsonApi\Request\Pagination\PageBasedPagination; +use WoohooLabs\Yin\JsonApi\Request\Pagination\OffsetBasedPagination; +use WoohooLabs\Yin\JsonApi\Request\Pagination\CursorBasedPagination; class Request implements RequestInterface { @@ -56,7 +60,9 @@ class Request implements RequestInterface protected $filtering; /** - * @param \Psr\Http\Message\ServerRequestInterface $request + * Request constructor. + * @param ServerRequestInterface $request + * @param ExceptionFactoryInterface $exceptionFactory */ public function __construct(ServerRequestInterface $request, ExceptionFactoryInterface $exceptionFactory) { @@ -67,7 +73,7 @@ public function __construct(ServerRequestInterface $request, ExceptionFactoryInt /** * @throws \WoohooLabs\Yin\JsonApi\Exception\MediaTypeUnsupported */ - public function validateContentTypeHeader() + public function validateContentTypeHeader(): void { if ($this->isValidMediaTypeHeader("Content-Type") === false) { throw new MediaTypeUnsupported($this->getHeaderLine("Content-Type")); @@ -77,7 +83,7 @@ public function validateContentTypeHeader() /** * @throws \WoohooLabs\Yin\JsonApi\Exception\MediaTypeUnacceptable */ - public function validateAcceptHeader() + public function validateAcceptHeader(): void { if ($this->isValidMediaTypeHeader("Accept") === false) { throw new MediaTypeUnacceptable($this->getHeaderLine("Accept")); @@ -87,7 +93,7 @@ public function validateAcceptHeader() /** * @throws \WoohooLabs\Yin\JsonApi\Exception\QueryParamUnrecognized */ - public function validateQueryParams() + public function validateQueryParams(): void { foreach ($this->getQueryParams() as $queryParamName => $queryParamValue) { if (preg_match("/^([a-z]+)$/", $queryParamName) && @@ -104,13 +110,14 @@ public function validateQueryParams() * @param string $headerName * @return bool */ - protected function isValidMediaTypeHeader($headerName) + protected function isValidMediaTypeHeader(string $headerName): bool { $header = $this->getHeaderLine($headerName); return (strpos($header, "application/vnd.api+json") === false || $header === "application/vnd.api+json"); } - protected function setIncludedFields() + + protected function setIncludedFields(): void { $this->includedFields = []; $fields = $this->getQueryParam("fields", []); @@ -129,7 +136,7 @@ protected function setIncludedFields() * @param string $resourceType * @return array */ - public function getIncludedFields($resourceType) + public function getIncludedFields(string $resourceType): array { if ($this->includedFields === null) { $this->setIncludedFields(); @@ -143,7 +150,7 @@ public function getIncludedFields($resourceType) * @param string $field * @return bool */ - public function isIncludedField($resourceType, $field) + public function isIncludedField(string $resourceType, string $field): bool { if ($this->includedFields === null) { $this->setIncludedFields(); @@ -160,7 +167,7 @@ public function isIncludedField($resourceType, $field) return isset($this->includedFields[$resourceType][$field]); } - protected function setIncludedRelationships() + protected function setIncludedRelationships(): void { $this->includedRelationships = []; @@ -193,7 +200,7 @@ protected function setIncludedRelationships() /** * @return bool */ - public function hasIncludedRelationships() + public function hasIncludedRelationships(): bool { if ($this->includedRelationships === null) { $this->setIncludedRelationships(); @@ -206,7 +213,7 @@ public function hasIncludedRelationships() * @param string $baseRelationshipPath * @return array */ - public function getIncludedRelationships($baseRelationshipPath) + public function getIncludedRelationships(string $baseRelationshipPath): array { if ($this->includedRelationships === null) { $this->setIncludedRelationships(); @@ -225,7 +232,11 @@ public function getIncludedRelationships($baseRelationshipPath) * @param array $defaultRelationships * @return bool */ - public function isIncludedRelationship($baseRelationshipPath, $relationshipName, array $defaultRelationships) + public function isIncludedRelationship( + string $baseRelationshipPath, + string $relationshipName, + array $defaultRelationships + ): bool { if ($this->includedRelationships === null) { $this->setIncludedRelationships(); @@ -242,7 +253,7 @@ public function isIncludedRelationship($baseRelationshipPath, $relationshipName, return isset($this->includedRelationships[$baseRelationshipPath][$relationshipName]); } - protected function setSorting() + protected function setSorting(): void { $sortingQueryParam = $this->getQueryParam("sort", ""); if ($sortingQueryParam === "") { @@ -257,7 +268,7 @@ protected function setSorting() /** * @return array */ - public function getSorting() + public function getSorting(): array { if ($this->sorting === null) { $this->setSorting(); @@ -266,7 +277,7 @@ public function getSorting() return $this->sorting; } - protected function setPagination() + protected function setPagination(): void { $pagination = $this->getQueryParam("page", null); $this->pagination = is_array($pagination) ? $pagination : []; @@ -275,7 +286,7 @@ protected function setPagination() /** * @return array */ - public function getPagination() + public function getPagination(): array { if ($this->pagination === null) { $this->setPagination(); @@ -285,30 +296,33 @@ public function getPagination() } /** - * @param mixed $defaultPage - * @return \WoohooLabs\Yin\JsonApi\Request\Pagination\FixedPagePagination + * @param int|null $defaultPage + * @return FixedPageBasedPagination */ - public function getFixedPageBasedPagination($defaultPage = null) + public function getFixedPageBasedPagination(?int $defaultPage = null): FixedPageBasedPagination { return FixedPagePagination::fromPaginationQueryParams($this->getPagination(), $defaultPage); } /** - * @param mixed $defaultPage - * @param mixed $defaultSize - * @return \WoohooLabs\Yin\JsonApi\Request\Pagination\PagePagination + * @param int|null $defaultPage + * @param int|null $defaultSize + * @return PageBasedPagination */ - public function getPageBasedPagination($defaultPage = null, $defaultSize = null) + public function getPageBasedPagination(?int $defaultPage = null, ?int $defaultSize = null): PageBasedPagination { return PagePagination::fromPaginationQueryParams($this->getPagination(), $defaultPage, $defaultSize); } /** - * @param mixed $defaultOffset - * @param mixed $defaultLimit - * @return \WoohooLabs\Yin\JsonApi\Request\Pagination\OffsetPagination + * @param int|null $defaultOffset + * @param int|null $defaultLimit + * @return OffsetBasedPagination */ - public function getOffsetBasedPagination($defaultOffset = null, $defaultLimit = null) + public function getOffsetBasedPagination( + ?int $defaultOffset = null, + ?int $defaultLimit = null + ): OffsetBasedPagination { return OffsetPagination::fromPaginationQueryParams($this->getPagination(), $defaultOffset, $defaultLimit); } @@ -317,21 +331,21 @@ public function getOffsetBasedPagination($defaultOffset = null, $defaultLimit = * @param mixed $defaultCursor * @return \WoohooLabs\Yin\JsonApi\Request\Pagination\CursorPagination */ - public function getCursorBasedPagination($defaultCursor = null) + public function getCursorBasedPagination($defaultCursor = null): CursorBasedPagination { return CursorPagination::fromPaginationQueryParams($this->getPagination(), $defaultCursor); } - protected function setFiltering() + protected function setFiltering(): void { $filtering = $this->getQueryParam("filter", []); $this->filtering = is_array($filtering) ? $filtering : []; } /** - * @return array + * @inheritdoc */ - public function getFiltering() + public function getFiltering(): array { if ($this->filtering === null) { $this->setFiltering(); @@ -341,11 +355,9 @@ public function getFiltering() } /** - * @param string $param - * @param mixed $default - * @return mixed + * @inheritdoc */ - public function getFilteringParam($param, $default = null) + public function getFilteringParam(string $param, $default = null) { $filtering = $this->getFiltering(); @@ -357,7 +369,7 @@ public function getFilteringParam($param, $default = null) * @param mixed $default * @return array|string|mixed */ - public function getQueryParam($name, $default = null) + public function getQueryParam(string $name, $default = null) { $queryParams = $this->serverRequest->getQueryParams(); @@ -369,9 +381,9 @@ public function getQueryParam($name, $default = null) * * @param string $name * @param mixed $value - * @return $this + * @return $this|Request */ - public function withQueryParam($name, $value) + public function withQueryParam(string $name, $value) { $self = clone $this; $queryParams = $this->serverRequest->getQueryParams(); @@ -381,7 +393,7 @@ public function withQueryParam($name, $value) return $self; } - protected function initializeParsedQueryParams() + protected function initializeParsedQueryParams(): void { $this->includedFields = null; $this->includedRelationships = null; @@ -391,8 +403,7 @@ protected function initializeParsedQueryParams() } /** - * @param mixed $default - * @return array|mixed + * @inheritdoc */ public function getResource($default = null) { @@ -401,8 +412,7 @@ public function getResource($default = null) } /** - * @param mixed $default - * @return string|mixed + * @inheritdoc */ public function getResourceType($default = null) { @@ -412,8 +422,7 @@ public function getResourceType($default = null) } /** - * @param mixed $default - * @return string|mixed + * @inheritdoc */ public function getResourceId($default = null) { @@ -425,7 +434,7 @@ public function getResourceId($default = null) /** * @return array */ - public function getResourceAttributes() + public function getResourceAttributes(): array { $data = $this->getResource(); @@ -437,7 +446,7 @@ public function getResourceAttributes() * @param mixed $default * @return mixed */ - public function getResourceAttribute($attribute, $default = null) + public function getResourceAttribute(string $attribute, $default = null) { $attributes = $this->getResourceAttributes(); @@ -448,7 +457,7 @@ public function getResourceAttribute($attribute, $default = null) * @param string $relationship * @return \WoohooLabs\Yin\JsonApi\Hydrator\Relationship\ToOneRelationship|null */ - public function getToOneRelationship($relationship) + public function getToOneRelationship(string $relationship): ?ToOneRelationship { $data = $this->getResource(); @@ -472,7 +481,7 @@ public function getToOneRelationship($relationship) * @param string $relationship * @return \WoohooLabs\Yin\JsonApi\Hydrator\Relationship\ToManyRelationship|null */ - public function getToManyRelationship($relationship) + public function getToManyRelationship(string $relationship): ?ToManyRelationship { $data = $this->getResource(); diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index be8ed96..6bda512 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -8,7 +8,7 @@ Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory WoohooLabs\Yin\JsonApi\JsonApi QP\WoohoolabsYinBundle\Factory\JsonApiFactory - WoohooLabs\Yin\JsonApi\Exception\ExceptionFactory + WoohooLabs\Yin\JsonApi\Exception\DefaultExceptionFactory @@ -22,4 +22,4 @@ service('request_stack').getCurrentRequest() - \ No newline at end of file + diff --git a/src/Resources/doc/index.rst b/src/Resources/doc/index.rst index 826964b..53bebde 100644 --- a/src/Resources/doc/index.rst +++ b/src/Resources/doc/index.rst @@ -6,6 +6,8 @@ Implements `woohoolabs/yin`_ framework into Symfony. Installation ------------ +Note: `4.x` is for Yin `3.x` and `3.x` ks for Yin `0.11` + .. code-block:: bash $ composer require qpautrat/woohoolabs-yin-bundle @@ -35,7 +37,7 @@ To do that you have to define which service to use in your global configuration .. code-block:: yaml qp_woohoolabs_yin: - exception_factory: yolo_service + exception_factory: my_exception_factory_service Usage @@ -76,6 +78,15 @@ If you installed `sensio/framework-extra-bundle`_ you can use ``ParamConverter`` } } +You can also use symfony service binding instead of adding `sensio/framework-extra-bundle`_ dependency: + +.. code-block:: yaml + services: + _defaults: + ... + bind: + $jsonApi: '@qp_woohoolabs_yin.json_api' + .. _`woohoolabs/yin`: https://github.com/woohoolabs/yin .. _`sensio/framework-extra-bundle`: https://github.com/sensiolabs/SensioFrameworkExtraBundle -.. _`ExceptionFactory`: https://github.com/woohoolabs/yin#exceptions \ No newline at end of file +.. _`ExceptionFactory`: https://github.com/woohoolabs/yin#exceptions