From 04a6e0806d13010ba11766426cd9f9e65aa79426 Mon Sep 17 00:00:00 2001 From: Vincent Klaiber Date: Thu, 31 Mar 2016 11:28:31 +0200 Subject: [PATCH 01/23] Update php version constraint --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index fbeebf8..3061728 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ } ], "require": { - "php": ">=5.5.9" + "php": "^5.5.9 || ^7.0" }, "require-dev": { "phpunit/phpunit": "^4.8 || ^5.0" From cae4b4d9866fc94c51cadadc77885a2f172f8a14 Mon Sep 17 00:00:00 2001 From: Vincent Klaiber Date: Thu, 31 Mar 2016 11:29:51 +0200 Subject: [PATCH 02/23] Test agains 5.5.9 Since we target 5.5.9 we could/should also test against it. --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index bb6e5c8..37af9cc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ language: php php: + - 5.5.9 - 5.5 - 5.6 - 7.0 From 6fb8dec1f0defd05cd1da27b507b98fcd518b9bd Mon Sep 17 00:00:00 2001 From: Vincent Klaiber Date: Thu, 31 Mar 2016 12:54:48 +0200 Subject: [PATCH 03/23] Add gitattributes file --- .gitattributes | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..0bbb62a --- /dev/null +++ b/.gitattributes @@ -0,0 +1,11 @@ +* text=auto + +/tests export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/.php_cs export-ignore +/.travis.yml export-ignore +/phpunit.xml.dist export-ignore +/CHANGELOG.md export-ignore +/CONTRIBUTING.md export-ignore +/README.md export-ignore From 1f1313884a12c1c11ae245d3d0a123fa6104d7f5 Mon Sep 17 00:00:00 2001 From: Vincent Klaiber Date: Sun, 3 Apr 2016 13:08:56 +0200 Subject: [PATCH 04/23] Update phpunit config With can now make the tests fail on warnings and risky tests. --- phpunit.xml.dist | 2 ++ 1 file changed, 2 insertions(+) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 51bc73c..ae108af 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -8,6 +8,8 @@ convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" + failOnRisky="true" + failOnWarning="true" processIsolation="false" stopOnError="false" stopOnFailure="false" From ba343e57fe76c136dcd53547916f761e8cc1e850 Mon Sep 17 00:00:00 2001 From: Vincent Klaiber Date: Sun, 3 Apr 2016 13:12:17 +0200 Subject: [PATCH 05/23] Update contributing copy --- CONTRIBUTING.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 0ca2ed1..9a9f662 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -6,10 +6,9 @@ Contributions are welcome, and are accepted via pull requests. Please review the * Please follow the [PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md) and [PHP-FIG Naming Conventions](https://github.com/php-fig/fig-standards/blob/master/bylaws/002-psr-naming-conventions.md). * Ensure that the current tests pass, and if you've added something new, add the tests where relevant. -* Remember that we follow [SemVer](http://semver.org). If you are changing the behaviour, or the public api, you may need to update the docs. -* Send a coherent commit history, making sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash](http://git-scm.com/book/en/Git-Tools-Rewriting-History) them before submitting. -* You may also need to [rebase](http://git-scm.com/book/en/Git-Branching-Rebasing) to avoid merge conflicts. - +* Remember that we follow [SemVer](http://semver.org). If you are changing the behavior, or the public api, you may need to update the docs. +* Send a coherent commit history, making sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash](https://git-scm.com/book/en/Git-Tools-Rewriting-History) them before submitting. +* You may also need to [rebase](https://git-scm.com/book/en/Git-Branching-Rebasing) to avoid merge conflicts. ## Running Tests @@ -21,7 +20,7 @@ First, install the dependencies: $ composer install ``` -Then run phpunit: +Then run PHPUnit: ```bash $ vendor/bin/phpunit @@ -29,4 +28,4 @@ $ vendor/bin/phpunit If the test suite passes on your local machine you should be good to go. -When you make a pull request, the tests will automatically be run again by [Travis CI](https://travis-ci.org/) on multiple php versions and hhvm. +When you make a pull request, the tests will automatically be run again by [Travis CI](https://travis-ci.org/) on multiple PHP versions and HHVM. From 3035ee56b0538c914193ae37deeb9e47d8baef06 Mon Sep 17 00:00:00 2001 From: Vincent Klaiber Date: Sun, 3 Apr 2016 13:23:49 +0200 Subject: [PATCH 06/23] Update docblocs Added missing return statements and added fullpaths. --- src/AbstractSerializer.php | 3 +- src/Collection.php | 10 +++++-- src/Document.php | 17 +++++++---- src/ElementInterface.php | 2 ++ src/ErrorHandler.php | 10 +++++-- .../Handler/ExceptionHandlerInterface.php | 8 +++-- .../Handler/FallbackExceptionHandler.php | 3 +- src/Exception/InvalidParameterException.php | 1 + src/LinksTrait.php | 6 ++++ src/MetaTrait.php | 2 ++ src/Parameters.php | 19 +++++++++--- src/Relationship.php | 9 +++--- src/Resource.php | 30 ++++++++++++------- src/SerializerInterface.php | 6 ++++ src/Util.php | 1 + 15 files changed, 93 insertions(+), 34 deletions(-) diff --git a/src/AbstractSerializer.php b/src/AbstractSerializer.php index 49a25c6..2d1178f 100644 --- a/src/AbstractSerializer.php +++ b/src/AbstractSerializer.php @@ -65,7 +65,7 @@ public function getMeta($model) /** * {@inheritdoc} * - * @throws LogicException + * @throws \LogicException */ public function getRelationship($model, $name) { @@ -88,6 +88,7 @@ public function getRelationship($model, $name) * kebab-case is converted into camelCase. * * @param string $name + * * @return string */ private function getRelationshipMethodName($name) diff --git a/src/Collection.php b/src/Collection.php index c0ae153..33f20ba 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -22,7 +22,7 @@ class Collection implements ElementInterface * Create a new collection instance. * * @param mixed $data - * @param SerializerInterface $serializer + * @param \Tobscure\JsonApi\SerializerInterface $serializer */ public function __construct($data, SerializerInterface $serializer) { @@ -34,7 +34,8 @@ public function __construct($data, SerializerInterface $serializer) * * @param mixed $data * @param SerializerInterface $serializer - * @return Resource[] + * + * @return \Tobscure\JsonApi\Resource[] */ protected function buildResources($data, SerializerInterface $serializer) { @@ -63,6 +64,8 @@ public function getResources() * Set the resources array. * * @param array $resources + * + * @return void */ public function setResources($resources) { @@ -73,6 +76,7 @@ public function setResources($resources) * Request a relationship to be included for all resources. * * @param string|array $relationships + * * @return $this */ public function with($relationships) @@ -88,6 +92,7 @@ public function with($relationships) * Request a relationship to be identified for all resources. * * @param string|array $relationships + * * @return $this */ public function identify($relationships) @@ -103,6 +108,7 @@ public function identify($relationships) * Request a restricted set of fields. * * @param array|null $fields + * * @return $this */ public function fields($fields) diff --git a/src/Document.php b/src/Document.php index 9d1318e..cc524d3 100644 --- a/src/Document.php +++ b/src/Document.php @@ -57,9 +57,10 @@ public function __construct(ElementInterface $data = null) /** * Get included resources. * - * @param ElementInterface $element + * @param \Tobscure\JsonApi\ElementInterface $element * @param bool $includeParent - * @return Resource[] + * + * @return \Tobscure\JsonApi\Resource[] */ protected function getIncluded(ElementInterface $element, $includeParent = false) { @@ -107,9 +108,10 @@ protected function getIncluded(ElementInterface $element, $includeParent = false } /** - * @param Resource[] $resources - * @param Resource $newResource - * @return Resource[] + * @param \Tobscure\JsonApi\Resource[] $resources + * @param \Tobscure\JsonApi\Resource $newResource + * + * @return \Tobscure\JsonApi\Resource[] */ protected function mergeResource(array $resources, Resource $newResource) { @@ -128,7 +130,8 @@ protected function mergeResource(array $resources, Resource $newResource) /** * Set the data object. * - * @param ElementInterface $element + * @param \Tobscure\JsonApi\ElementInterface $element + * * @return $this */ public function setData(ElementInterface $element) @@ -142,6 +145,7 @@ public function setData(ElementInterface $element) * Set the errors array. * * @param array $errors + * * @return $this */ public function setErrors($errors) @@ -155,6 +159,7 @@ public function setErrors($errors) * Set the jsonapi array. * * @param array $jsonapi + * * @return $this */ public function setJsonapi($jsonapi) diff --git a/src/ElementInterface.php b/src/ElementInterface.php index 6b0ac00..81b74ba 100644 --- a/src/ElementInterface.php +++ b/src/ElementInterface.php @@ -38,6 +38,7 @@ public function toIdentifier(); * Request a relationship to be included. * * @param string|array $relationships + * * @return $this */ public function with($relationships); @@ -46,6 +47,7 @@ public function with($relationships); * Request a restricted set of fields. * * @param array|null $fields + * * @return $this */ public function fields($fields); diff --git a/src/ErrorHandler.php b/src/ErrorHandler.php index 33243f0..1e3492d 100644 --- a/src/ErrorHandler.php +++ b/src/ErrorHandler.php @@ -20,7 +20,7 @@ class ErrorHandler /** * Stores the valid handlers. * - * @var ExceptionHandlerInterface[] + * @var \Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface[] */ private $handlers = []; @@ -28,8 +28,10 @@ class ErrorHandler * Handle the exception provided. * * @param Exception $e - * @return \Tobscure\JsonApi\Exception\Handler\ResponseBag + * * @throws RuntimeException + * + * @return \Tobscure\JsonApi\Exception\Handler\ResponseBag */ public function handle(Exception $e) { @@ -45,7 +47,9 @@ public function handle(Exception $e) /** * Register a new exception handler. * - * @param ExceptionHandlerInterface $handler + * @param \Tobscure\JsonApi\Exception\Handler\ExceptionHandlerInterface $handler + * + * @return void */ public function registerHandler(ExceptionHandlerInterface $handler) { diff --git a/src/Exception/Handler/ExceptionHandlerInterface.php b/src/Exception/Handler/ExceptionHandlerInterface.php index 15ea0b4..4108fc0 100644 --- a/src/Exception/Handler/ExceptionHandlerInterface.php +++ b/src/Exception/Handler/ExceptionHandlerInterface.php @@ -19,7 +19,8 @@ interface ExceptionHandlerInterface * If the exception handler is able to format a response for the provided exception, * then the implementation should return true. * - * @param Exception $e + * @param \Exception $e + * * @return bool */ public function manages(Exception $e); @@ -27,8 +28,9 @@ public function manages(Exception $e); /** * Handle the provided exception. * - * @param Exception $e - * @return ResponseBag + * @param \Exception $e + * + * @return \Tobscure\JsonApi\Exception\Handler\ResponseBag */ public function handle(Exception $e); } diff --git a/src/Exception/Handler/FallbackExceptionHandler.php b/src/Exception/Handler/FallbackExceptionHandler.php index 4d1fa96..2799d55 100644 --- a/src/Exception/Handler/FallbackExceptionHandler.php +++ b/src/Exception/Handler/FallbackExceptionHandler.php @@ -48,8 +48,9 @@ public function handle(Exception $e) } /** - * @param Exception $e + * @param \Exception $e * @param $status + * * @return array */ private function constructError(Exception $e, $status) diff --git a/src/Exception/InvalidParameterException.php b/src/Exception/InvalidParameterException.php index b8fcf5e..02aad05 100644 --- a/src/Exception/InvalidParameterException.php +++ b/src/Exception/InvalidParameterException.php @@ -15,4 +15,5 @@ class InvalidParameterException extends Exception { + // } diff --git a/src/LinksTrait.php b/src/LinksTrait.php index 316790b..1f22171 100644 --- a/src/LinksTrait.php +++ b/src/LinksTrait.php @@ -34,6 +34,7 @@ public function getLinks() * Set the links. * * @param array $links + * * @return $this */ public function setLinks(array $links) @@ -48,6 +49,7 @@ public function setLinks(array $links) * * @param string $key * @param string $value + * * @return $this */ public function addLink($key, $value) @@ -65,6 +67,8 @@ public function addLink($key, $value) * @param int $offset The current offset. * @param int $limit The current limit. * @param int|null $total The total number of results, or null if unknown. + * + * @return void */ public function addPaginationLinks($url, array $queryParams, $offset, $limit, $total = null) { @@ -95,6 +99,8 @@ public function addPaginationLinks($url, array $queryParams, $offset, $limit, $t * @param array $queryParams The query params provided in the request. * @param int $offset The offset to link to. * @param int $limit The current limit. + * + * @return void */ protected function addPaginationLink($name, $url, array $queryParams, $offset, $limit) { diff --git a/src/MetaTrait.php b/src/MetaTrait.php index 5202033..5572446 100644 --- a/src/MetaTrait.php +++ b/src/MetaTrait.php @@ -34,6 +34,7 @@ public function getMeta() * Set the meta data array. * * @param array $meta + * * @return $this */ public function setMeta(array $meta) @@ -48,6 +49,7 @@ public function setMeta(array $meta) * * @param string $key * @param string $value + * * @return $this */ public function addMeta($key, $value) diff --git a/src/Parameters.php b/src/Parameters.php index cfd6db6..acf9f2b 100644 --- a/src/Parameters.php +++ b/src/Parameters.php @@ -32,8 +32,10 @@ public function __construct(array $input) * Get the includes. * * @param array $available + * + * @throws \Tobscure\JsonApi\Exception\InvalidParameterException + * * @return array - * @throws InvalidParameterException */ public function getInclude(array $available = []) { @@ -56,8 +58,10 @@ public function getInclude(array $available = []) * Get number of offset. * * @param int|null $perPage + * + * @throws \Tobscure\JsonApi\Exception\InvalidParameterException + * * @return int - * @throws InvalidParameterException */ public function getOffset($perPage = null) { @@ -78,8 +82,10 @@ public function getOffset($perPage = null) * Calculate the offset based on the page[number] parameter. * * @param int $perPage + * + * @throws \Tobscure\JsonApi\Exception\InvalidParameterException + * * @return int - * @throws InvalidParameterException */ protected function getOffsetFromNumber($perPage) { @@ -96,6 +102,7 @@ protected function getOffsetFromNumber($perPage) * Get the limit. * * @param int|null $max + * * @return int|null */ public function getLimit($max = null) @@ -113,8 +120,10 @@ public function getLimit($max = null) * Get the sort. * * @param array $available + * + * @throws \Tobscure\JsonApi\Exception\InvalidParameterException + * * @return array - * @throws InvalidParameterException */ public function getSort(array $available = []) { @@ -177,6 +186,7 @@ public function getFilter() * * @param string $key * @param null $default + * * @return mixed */ protected function getInput($key, $default = null) @@ -188,6 +198,7 @@ protected function getInput($key, $default = null) * Get the page. * * @param string $key + * * @return string */ protected function getPage($key) diff --git a/src/Relationship.php b/src/Relationship.php index bbad37d..29f7fc6 100644 --- a/src/Relationship.php +++ b/src/Relationship.php @@ -19,14 +19,14 @@ class Relationship /** * The data object. * - * @var ElementInterface|null + * @var \Tobscure\JsonApi\ElementInterface|null */ protected $data; /** * Create a new relationship. * - * @param ElementInterface|null $data + * @param \Tobscure\JsonApi\ElementInterface|null $data */ public function __construct(ElementInterface $data = null) { @@ -36,7 +36,7 @@ public function __construct(ElementInterface $data = null) /** * Get the data object. * - * @return ElementInterface|null + * @return \Tobscure\JsonApi\ElementInterface|null */ public function getData() { @@ -46,7 +46,8 @@ public function getData() /** * Set the data object. * - * @param ElementInterface|null $data + * @param \Tobscure\JsonApi\ElementInterface|null $data + * * @return $this */ public function setData($data) diff --git a/src/Resource.php b/src/Resource.php index b8cabd9..feb7296 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -22,7 +22,7 @@ class Resource implements ElementInterface protected $data; /** - * @var SerializerInterface + * @var \Tobscure\JsonApi\SerializerInterface */ protected $serializer; @@ -43,13 +43,13 @@ class Resource implements ElementInterface /** * An array of Resources that should be merged into this one. * - * @var Resource[] + * @var \Tobscure\JsonApi\Resource[] */ protected $merged = []; /** * @param mixed $data - * @param SerializerInterface $serializer + * @param \Tobscure\JsonApi\SerializerInterface $serializer */ public function __construct($data, SerializerInterface $serializer) { @@ -194,6 +194,7 @@ protected function getOwnFields() * to the requested fieldset. * * @param array $fields + * * @return array */ protected function filterFields(array $fields) @@ -209,6 +210,7 @@ protected function filterFields(array $fields) * Merge the attributes of merged resources into an array of attributes. * * @param array $attributes + * * @return array */ protected function mergeAttributes(array $attributes) @@ -223,7 +225,7 @@ protected function mergeAttributes(array $attributes) /** * Get the resource relationships. * - * @return Relationship[] + * @return \Tobscure\JsonApi\Relationship[] */ public function getRelationships() { @@ -235,7 +237,7 @@ public function getRelationships() /** * Get the resource relationships without considering requested ones. * - * @return Relationship[] + * @return \Tobscure\JsonApi\Relationship[] */ public function getUnfilteredRelationships() { @@ -259,7 +261,7 @@ public function getRelationshipsAsArray() /** * Get an array of built relationships. * - * @return Relationship[] + * @return \Tobscure\JsonApi\Relationship[] */ protected function buildRelationships() { @@ -288,6 +290,7 @@ protected function buildRelationships() * relationships. * * @param array $relationships + * * @return array */ protected function mergeRelationships(array $relationships) @@ -302,7 +305,8 @@ protected function mergeRelationships(array $relationships) /** * Convert the given array of Relationship objects into an array. * - * @param Relationship[] $relationships + * @param \Tobscure\JsonApi\Relationship[] $relationships + * * @return array */ protected function convertRelationshipsToArray(array $relationships) @@ -315,7 +319,9 @@ protected function convertRelationshipsToArray(array $relationships) /** * Merge a resource into this one. * - * @param Resource $resource + * @param \Tobscure\JsonApi\Resource $resource + * + * @return void */ public function merge(Resource $resource) { @@ -352,6 +358,8 @@ public function getData() /** * @param mixed $data + * + * @return void */ public function setData($data) { @@ -359,7 +367,7 @@ public function setData($data) } /** - * @return SerializerInterface + * @return \Tobscure\JsonApi\SerializerInterface */ public function getSerializer() { @@ -367,7 +375,9 @@ public function getSerializer() } /** - * @param SerializerInterface $serializer + * @param \Tobscure\JsonApi\SerializerInterface $serializer + * + * @return void */ public function setSerializer(SerializerInterface $serializer) { diff --git a/src/SerializerInterface.php b/src/SerializerInterface.php index ddd43cb..96730fa 100644 --- a/src/SerializerInterface.php +++ b/src/SerializerInterface.php @@ -17,6 +17,7 @@ interface SerializerInterface * Get the type. * * @param mixed $model + * * @return string */ public function getType($model); @@ -25,6 +26,7 @@ public function getType($model); * Get the id. * * @param mixed $model + * * @return string */ public function getId($model); @@ -34,6 +36,7 @@ public function getId($model); * * @param mixed $model * @param array|null $fields + * * @return array */ public function getAttributes($model, array $fields = null); @@ -42,6 +45,7 @@ public function getAttributes($model, array $fields = null); * Get the links array. * * @param mixed $model + * * @return array */ public function getLinks($model); @@ -50,6 +54,7 @@ public function getLinks($model); * Get the meta. * * @param mixed $model + * * @return array */ public function getMeta($model); @@ -59,6 +64,7 @@ public function getMeta($model); * * @param mixed $model * @param string $name + * * @return \Tobscure\JsonApi\Relationship|null */ public function getRelationship($model, $name); diff --git a/src/Util.php b/src/Util.php index b5d6a28..cac77a3 100644 --- a/src/Util.php +++ b/src/Util.php @@ -26,6 +26,7 @@ class Util * ['user' => ['employer', 'employer.country'], 'comments' => []] * * @param array $paths + * * @return array */ public static function parseRelationshipPaths(array $paths) From d9eccd996fdfc6a00175cfd1a227f80f517f4d0d Mon Sep 17 00:00:00 2001 From: Spencer Rinehart Date: Thu, 26 May 2016 20:54:36 +0930 Subject: [PATCH 07/23] Enhance invalid parameter exceptions. closes #96 --- .../InvalidParameterExceptionHandler.php | 10 +++++ src/Exception/InvalidParameterException.php | 25 ++++++++++- src/Parameters.php | 16 +++++-- .../InvalidParameterExceptionHandlerTest.php | 44 +++++++++++++++++++ tests/ParametersTest.php | 25 ++++++++++- 5 files changed, 115 insertions(+), 5 deletions(-) create mode 100644 tests/Exception/Handler/InvalidParameterExceptionHandlerTest.php diff --git a/src/Exception/Handler/InvalidParameterExceptionHandler.php b/src/Exception/Handler/InvalidParameterExceptionHandler.php index d4ff608..374020a 100644 --- a/src/Exception/Handler/InvalidParameterExceptionHandler.php +++ b/src/Exception/Handler/InvalidParameterExceptionHandler.php @@ -32,6 +32,16 @@ public function handle(Exception $e) $status = 400; $error = []; + $code = $e->getCode(); + if ($code) { + $error['code'] = $code; + } + + $invalidParameter = $e->getInvalidParameter(); + if ($invalidParameter) { + $error['source'] = ['parameter' => $invalidParameter]; + } + return new ResponseBag($status, [$error]); } } diff --git a/src/Exception/InvalidParameterException.php b/src/Exception/InvalidParameterException.php index 02aad05..72027c0 100644 --- a/src/Exception/InvalidParameterException.php +++ b/src/Exception/InvalidParameterException.php @@ -15,5 +15,28 @@ class InvalidParameterException extends Exception { - // + /** + * @var string The parameter that caused this exception. + */ + private $invalidParameter; + + /** + * {@inheritdoc} + * + * @param string $invalidParameter The parameter that caused this exception. + */ + public function __construct($message = '', $code = 0, $previous = null, $invalidParameter = '') + { + parent::__construct($message, $code, $previous); + + $this->invalidParameter = $invalidParameter; + } + + /** + * @return string The parameter that caused this exception. + */ + public function getInvalidParameter() + { + return $this->invalidParameter; + } } diff --git a/src/Parameters.php b/src/Parameters.php index acf9f2b..0280e47 100644 --- a/src/Parameters.php +++ b/src/Parameters.php @@ -45,7 +45,12 @@ public function getInclude(array $available = []) $invalid = array_diff($relationships, $available); if (count($invalid)) { - throw new InvalidParameterException('Invalid includes ['.implode(',', $invalid).']'); + throw new InvalidParameterException( + 'Invalid includes ['.implode(',', $invalid).']', + 1, + null, + 'include' + ); } return $relationships; @@ -72,7 +77,7 @@ public function getOffset($perPage = null) $offset = (int) $this->getPage('offset'); if ($offset < 0) { - throw new InvalidParameterException('page[offset] must be >=0'); + throw new InvalidParameterException('page[offset] must be >=0', 2, null, 'page[offset]'); } return $offset; @@ -146,7 +151,12 @@ public function getSort(array $available = []) $invalid = array_diff(array_keys($sort), $available); if (count($invalid)) { - throw new InvalidParameterException('Invalid sort fields ['.implode(',', $invalid).']'); + throw new InvalidParameterException( + 'Invalid sort fields ['.implode(',', $invalid).']', + 3, + null, + 'sort' + ); } } diff --git a/tests/Exception/Handler/InvalidParameterExceptionHandlerTest.php b/tests/Exception/Handler/InvalidParameterExceptionHandlerTest.php new file mode 100644 index 0000000..eb01ec2 --- /dev/null +++ b/tests/Exception/Handler/InvalidParameterExceptionHandlerTest.php @@ -0,0 +1,44 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Tobscure\Tests\Exception\Handler; + +use Exception; +use Tobscure\JsonApi\Exception\Handler\InvalidParameterExceptionHandler; +use Tobscure\JsonApi\Exception\Handler\ResponseBag; +use Tobscure\JsonApi\Exception\InvalidParameterException; + +class InvalidParameterExceptionHandlerTest extends \PHPUnit_Framework_TestCase +{ + public function testHandlerCanManageInvalidParameterExceptions() + { + $handler = new InvalidParameterExceptionHandler(); + + $this->assertTrue($handler->manages(new InvalidParameterException)); + } + + public function testHandlerCanNotManageOtherExceptions() + { + $handler = new InvalidParameterExceptionHandler(); + + $this->assertFalse($handler->manages(new Exception)); + } + + public function testErrorHandling() + { + $handler = new InvalidParameterExceptionHandler(); + $response = $handler->handle(new InvalidParameterException('error', 1, null, 'include')); + + $this->assertInstanceOf(ResponseBag::class, $response); + $this->assertEquals(400, $response->getStatus()); + $this->assertEquals([['code' => 1, 'source' => ['parameter' => 'include']]], $response->getErrors()); + } +} diff --git a/tests/ParametersTest.php b/tests/ParametersTest.php index a2f0bbe..e38362b 100644 --- a/tests/ParametersTest.php +++ b/tests/ParametersTest.php @@ -34,6 +34,17 @@ public function testGetIncludeReturnsEmptyArray() $this->assertEquals([], $parameters->getInclude(['posts', 'images'])); } + /** + * @expectedException \Tobscure\JsonApi\Exception\InvalidParameterException + * @expectedExceptionCode 1 + */ + public function testGetIncludeWithUnallowedField() + { + $parameters = new Parameters(['include' => 'posts,images']); + + $parameters->getInclude(['posts']); + } + public function testGetSortReturnsArrayOfFieldToSortDirection() { $parameters = new Parameters(['sort' => 'firstname']); @@ -55,6 +66,17 @@ public function testGetSortDefaultsToEmptyArray() $this->assertEmpty($parameters->getSort()); } + /** + * @expectedException \Tobscure\JsonApi\Exception\InvalidParameterException + * @expectedExceptionCode 3 + */ + public function testGetSortWithUnallowedField() + { + $parameters = new Parameters(['sort' => 'firstname,lastname']); + + $parameters->getSort(['firstname']); + } + public function testGetOffsetParsesThePageOffset() { $parameters = new Parameters(['page' => ['offset' => 10]]); @@ -64,12 +86,13 @@ public function testGetOffsetParsesThePageOffset() /** * @expectedException \Tobscure\JsonApi\Exception\InvalidParameterException + * @expectedExceptionCode 2 */ public function testGetOffsetIsAtLeastZero() { $parameters = new Parameters(['page' => ['offset' => -5]]); - $this->assertEquals(0, $parameters->getOffset()); + $parameters->getOffset(); } public function testGetOffsetParsesThePageNumber() From 24b0e852c5809fd078d7095c033b0884c6d0c330 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Thu, 26 May 2016 21:39:32 +0930 Subject: [PATCH 08/23] Better support for empty relationships. closes #94 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To return an empty to-one relationship, pass an EmptyResource instance as the Relationship data. To return an empty to-many relationship, pass an EmptyCollection instance as the Relationship data. (Passing a Collection with an empty array will work too.) Why necessitate an EmptyResource object rather than just passing null as the Relationship data? Because there are times where the user may not wish to include the `data` key in the relationship object at all, instead filling it with `links` or `meta` – null data value is reserved for this case. --- src/EmptyCollection.php | 55 +++++++++++++++++++++++++++++++++++++++++ src/EmptyResource.php | 55 +++++++++++++++++++++++++++++++++++++++++ tests/ResourceTest.php | 50 +++++++++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 src/EmptyCollection.php create mode 100644 src/EmptyResource.php diff --git a/src/EmptyCollection.php b/src/EmptyCollection.php new file mode 100644 index 0000000..f134ac9 --- /dev/null +++ b/src/EmptyCollection.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Tobscure\JsonApi; + +class EmptyCollection implements ElementInterface +{ + /** + * {@inheritdoc} + */ + public function getResources() + { + return []; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + return $this->toIdentifier(); + } + + /** + * {@inheritdoc} + */ + public function toIdentifier() + { + return []; + } + + /** + * {@inheritdoc} + */ + public function with($relationships) + { + return $this; + } + + /** + * {@inheritdoc} + */ + public function fields($fields) + { + return $this; + } +} diff --git a/src/EmptyResource.php b/src/EmptyResource.php new file mode 100644 index 0000000..d0a3eee --- /dev/null +++ b/src/EmptyResource.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Tobscure\JsonApi; + +class EmptyResource implements ElementInterface +{ + /** + * {@inheritdoc} + */ + public function getResources() + { + return [$this]; + } + + /** + * {@inheritdoc} + */ + public function toArray() + { + return $this->toIdentifier(); + } + + /** + * {@inheritdoc} + */ + public function toIdentifier() + { + return null; + } + + /** + * {@inheritdoc} + */ + public function with($relationships) + { + return $this; + } + + /** + * {@inheritdoc} + */ + public function fields($fields) + { + return $this; + } +} diff --git a/tests/ResourceTest.php b/tests/ResourceTest.php index b17ef78..4b30e6d 100644 --- a/tests/ResourceTest.php +++ b/tests/ResourceTest.php @@ -15,6 +15,8 @@ use Tobscure\JsonApi\Collection; use Tobscure\JsonApi\Relationship; use Tobscure\JsonApi\Resource; +use Tobscure\JsonApi\EmptyResource; +use Tobscure\JsonApi\EmptyCollection; use Tobscure\Tests\JsonApi\AbstractTestCase; class ResourceTest extends AbstractTestCase @@ -170,6 +172,44 @@ public function testMetaMergeWithSerializerLinks() ] ], $resource1->toArray()); } + + public function testEmptyToOneRelationships() + { + $post1 = (object) ['id' => '123', 'foo' => 'bar']; + + $resource1 = new Resource($post1, new PostSerializer4()); + $resource1->with('author'); + + $this->assertEquals([ + 'type' => 'posts', + 'id' => '123', + 'attributes' => [ + 'foo' => 'bar' + ], + 'relationships' => [ + 'author' => ['data' => null] + ] + ], $resource1->toArray()); + } + + public function testEmptyToManyRelationships() + { + $post1 = (object) ['id' => '123', 'foo' => 'bar']; + + $resource1 = new Resource($post1, new PostSerializer4()); + $resource1->with('likes'); + + $this->assertEquals([ + 'type' => 'posts', + 'id' => '123', + 'attributes' => [ + 'foo' => 'bar' + ], + 'relationships' => [ + 'likes' => ['data' => []] + ] + ], $resource1->toArray()); + } } class PostSerializer4 extends AbstractSerializer @@ -194,6 +234,16 @@ public function comments($post) { return new Relationship(new Collection($post->comments, new CommentSerializer)); } + + public function author($post) + { + return new Relationship(new EmptyResource); + } + + public function likes($post) + { + return new Relationship(new EmptyCollection); + } } class PostSerializer4WithLinksAndMeta extends PostSerializer4 { From de166728935546690ada9b837a5fb87cf7a2c13f Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Thu, 26 May 2016 21:49:19 +0930 Subject: [PATCH 09/23] Simplify empty relationship support. ref #94 Since the natural thing to do is: return new Relationship(new Resource($data, new Serializer)); where $data may or may not be null. Now if $data is null, the resource identifier will be returned as null too, indicating an empty relationship. --- src/EmptyCollection.php | 55 ----------------------------------------- src/EmptyResource.php | 55 ----------------------------------------- src/Resource.php | 4 +++ tests/ResourceTest.php | 4 +-- 4 files changed, 6 insertions(+), 112 deletions(-) delete mode 100644 src/EmptyCollection.php delete mode 100644 src/EmptyResource.php diff --git a/src/EmptyCollection.php b/src/EmptyCollection.php deleted file mode 100644 index f134ac9..0000000 --- a/src/EmptyCollection.php +++ /dev/null @@ -1,55 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Tobscure\JsonApi; - -class EmptyCollection implements ElementInterface -{ - /** - * {@inheritdoc} - */ - public function getResources() - { - return []; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - return $this->toIdentifier(); - } - - /** - * {@inheritdoc} - */ - public function toIdentifier() - { - return []; - } - - /** - * {@inheritdoc} - */ - public function with($relationships) - { - return $this; - } - - /** - * {@inheritdoc} - */ - public function fields($fields) - { - return $this; - } -} diff --git a/src/EmptyResource.php b/src/EmptyResource.php deleted file mode 100644 index d0a3eee..0000000 --- a/src/EmptyResource.php +++ /dev/null @@ -1,55 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Tobscure\JsonApi; - -class EmptyResource implements ElementInterface -{ - /** - * {@inheritdoc} - */ - public function getResources() - { - return [$this]; - } - - /** - * {@inheritdoc} - */ - public function toArray() - { - return $this->toIdentifier(); - } - - /** - * {@inheritdoc} - */ - public function toIdentifier() - { - return null; - } - - /** - * {@inheritdoc} - */ - public function with($relationships) - { - return $this; - } - - /** - * {@inheritdoc} - */ - public function fields($fields) - { - return $this; - } -} diff --git a/src/Resource.php b/src/Resource.php index feb7296..7705e26 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -123,6 +123,10 @@ public function isIdentifier() */ public function toIdentifier() { + if (! $this->data) { + return null; + } + $array = [ 'type' => $this->getType(), 'id' => $this->getId() diff --git a/tests/ResourceTest.php b/tests/ResourceTest.php index 4b30e6d..b35f36b 100644 --- a/tests/ResourceTest.php +++ b/tests/ResourceTest.php @@ -237,12 +237,12 @@ public function comments($post) public function author($post) { - return new Relationship(new EmptyResource); + return new Relationship(new Resource(null, new CommentSerializer)); } public function likes($post) { - return new Relationship(new EmptyCollection); + return new Relationship(new Collection([], new CommentSerializer)); } } class PostSerializer4WithLinksAndMeta extends PostSerializer4 From 5f6b532fb113a1794eeee8da78505e5d3eacd1b5 Mon Sep 17 00:00:00 2001 From: Anthony Hendrickson Date: Fri, 22 Jul 2016 16:35:40 -0400 Subject: [PATCH 10/23] Fixed issue where resource-identifier objects included empty 'attributes' field --- src/Resource.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Resource.php b/src/Resource.php index 7705e26..8268058 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -72,7 +72,9 @@ public function toArray() { $array = $this->toIdentifier(); - $array['attributes'] = $this->getAttributes(); + if (! $this->isIdentifier()) { + $array['attributes'] = $this->getAttributes(); + } $relationships = $this->getRelationshipsAsArray(); From a691e2bd3bff0708e1ed33705a879e47da262f72 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Thu, 10 Nov 2016 09:45:36 +1030 Subject: [PATCH 11/23] Cache resource relationships to prevent them being built twice. fixes #103 --- src/Resource.php | 13 ++++++++++++- tests/DocumentTest.php | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/Resource.php b/src/Resource.php index 8268058..1695f88 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -47,6 +47,11 @@ class Resource implements ElementInterface */ protected $merged = []; + /** + * @var \Tobscure\JsonApi\Relationship[] + */ + private $relationships; + /** * @param mixed $data * @param \Tobscure\JsonApi\SerializerInterface $serializer @@ -271,6 +276,10 @@ public function getRelationshipsAsArray() */ protected function buildRelationships() { + if (isset($this->relationships)) { + return $this->relationships; + } + $paths = Util::parseRelationshipPaths($this->includes); $relationships = []; @@ -288,7 +297,7 @@ protected function buildRelationships() } } - return $relationships; + return $this->relationships = $relationships; } /** @@ -341,6 +350,8 @@ public function with($relationships) { $this->includes = array_unique(array_merge($this->includes, (array) $relationships)); + $this->relationships = null; + return $this; } diff --git a/tests/DocumentTest.php b/tests/DocumentTest.php index 6007c58..bbd68e2 100644 --- a/tests/DocumentTest.php +++ b/tests/DocumentTest.php @@ -14,6 +14,8 @@ use Tobscure\JsonApi\AbstractSerializer; use Tobscure\JsonApi\Document; use Tobscure\JsonApi\Resource; +use Tobscure\JsonApi\Collection; +use Tobscure\JsonApi\Relationship; /** * This is the document test class. @@ -40,6 +42,24 @@ public function testItCanBeSerializedToJson() { $this->assertEquals('[]', (string) new Document()); } + + public function testToArrayIncludesIncludedResources() + { + $comment = (object) ['id' => 1, 'foo' => 'bar']; + $post = (object) ['id' => 1, 'foo' => 'bar', 'comments' => [$comment]]; + + $resource = new Resource($post, new PostSerializer2); + $includedResource = new Resource($comment, new CommentSerializer2); + + $document = new Document($resource->with('comments')); + + $this->assertEquals([ + 'data' => $resource->toArray(), + 'included' => [ + $includedResource->toArray() + ] + ], $document->toArray()); + } } class PostSerializer2 extends AbstractSerializer @@ -50,4 +70,19 @@ public function getAttributes($post, array $fields = null) { return ['foo' => $post->foo]; } + + public function comments($post) + { + return new Relationship(new Collection($post->comments, new CommentSerializer2)); + } +} + +class CommentSerializer2 extends AbstractSerializer +{ + protected $type = 'comments'; + + public function getAttributes($comment, array $fields = null) + { + return ['foo' => $comment->foo]; + } } From df7e1a56e3262eca2faaeb5b70141126d4258c94 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Wed, 9 Nov 2016 23:16:23 +0000 Subject: [PATCH 12/23] Applied fixes from StyleCI --- src/Resource.php | 4 ++-- tests/DocumentTest.php | 4 ++-- tests/ResourceTest.php | 2 -- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Resource.php b/src/Resource.php index 1695f88..f1cc9c5 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -131,9 +131,9 @@ public function isIdentifier() public function toIdentifier() { if (! $this->data) { - return null; + return; } - + $array = [ 'type' => $this->getType(), 'id' => $this->getId() diff --git a/tests/DocumentTest.php b/tests/DocumentTest.php index bbd68e2..d402b79 100644 --- a/tests/DocumentTest.php +++ b/tests/DocumentTest.php @@ -12,10 +12,10 @@ namespace Tobscure\Tests\JsonApi; use Tobscure\JsonApi\AbstractSerializer; -use Tobscure\JsonApi\Document; -use Tobscure\JsonApi\Resource; use Tobscure\JsonApi\Collection; +use Tobscure\JsonApi\Document; use Tobscure\JsonApi\Relationship; +use Tobscure\JsonApi\Resource; /** * This is the document test class. diff --git a/tests/ResourceTest.php b/tests/ResourceTest.php index b35f36b..63246ca 100644 --- a/tests/ResourceTest.php +++ b/tests/ResourceTest.php @@ -15,8 +15,6 @@ use Tobscure\JsonApi\Collection; use Tobscure\JsonApi\Relationship; use Tobscure\JsonApi\Resource; -use Tobscure\JsonApi\EmptyResource; -use Tobscure\JsonApi\EmptyCollection; use Tobscure\Tests\JsonApi\AbstractTestCase; class ResourceTest extends AbstractTestCase From 8d4116ec580febf36d7e6995829a6f5adf6f9c48 Mon Sep 17 00:00:00 2001 From: ustrugany Date: Wed, 18 Jan 2017 13:37:00 +0400 Subject: [PATCH 13/23] #105 handling underscored relationships names --- src/AbstractSerializer.php | 7 ++++++- tests/AbstractSerializerTest.php | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/AbstractSerializer.php b/src/AbstractSerializer.php index 2d1178f..c5055fe 100644 --- a/src/AbstractSerializer.php +++ b/src/AbstractSerializer.php @@ -85,7 +85,8 @@ public function getRelationship($model, $name) /** * Get the serializer method name for the given relationship. * - * kebab-case is converted into camelCase. + * + * underscore, kebab-case is converted into camelCase. * * @param string $name * @@ -97,6 +98,10 @@ private function getRelationshipMethodName($name) $name = lcfirst(implode('', array_map('ucfirst', explode('-', $name)))); } + if (stripos($name, '_')) { + $name = lcfirst(implode('', array_map('ucfirst', explode('_', $name)))); + } + return $name; } } diff --git a/tests/AbstractSerializerTest.php b/tests/AbstractSerializerTest.php index f0210ba..0ddc86e 100644 --- a/tests/AbstractSerializerTest.php +++ b/tests/AbstractSerializerTest.php @@ -14,6 +14,7 @@ use Tobscure\JsonApi\AbstractSerializer; use Tobscure\JsonApi\Collection; use Tobscure\JsonApi\Relationship; +use Tobscure\JsonApi\Resource; class AbstractSerializerTest extends AbstractTestCase { @@ -41,6 +42,24 @@ public function testGetRelationshipReturnsRelationshipFromMethod() $this->assertTrue($relationship instanceof Relationship); } + public function testGetRelationshipReturnsRelationshipFromMethodUnderscored() + { + $serializer = new PostSerializer1; + + $relationship = $serializer->getRelationship(null, 'parent_post'); + + $this->assertTrue($relationship instanceof Relationship); + } + + public function testGetRelationshipReturnsRelationshipFromMethodKebabCase() + { + $serializer = new PostSerializer1; + + $relationship = $serializer->getRelationship(null, 'parent-post'); + + $this->assertTrue($relationship instanceof Relationship); + } + /** * @expectedException \LogicException */ @@ -68,6 +87,13 @@ public function comments($post) return new Relationship($element); } + public function parentPost($post) + { + $element = new Resource([], new self); + + return new Relationship($element); + } + public function invalid($post) { return 'invalid'; From cc338b27714d893b1e1732ab827ed042ce0f0918 Mon Sep 17 00:00:00 2001 From: Alexey Karapetov Date: Fri, 3 Mar 2017 23:12:31 -0800 Subject: [PATCH 14/23] Omit empty attributes --- src/Resource.php | 5 ++++- tests/DocumentTest.php | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Resource.php b/src/Resource.php index f1cc9c5..407d358 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -78,7 +78,10 @@ public function toArray() $array = $this->toIdentifier(); if (! $this->isIdentifier()) { - $array['attributes'] = $this->getAttributes(); + $attributes = $this->getAttributes(); + if ($attributes) { + $array['attributes'] = $this->getAttributes(); + } } $relationships = $this->getRelationshipsAsArray(); diff --git a/tests/DocumentTest.php b/tests/DocumentTest.php index d402b79..e1c6f51 100644 --- a/tests/DocumentTest.php +++ b/tests/DocumentTest.php @@ -60,6 +60,19 @@ public function testToArrayIncludesIncludedResources() ] ], $document->toArray()); } + + public function testNoEmptyAttributes() + { + $post = (object) [ + 'id' => 1, + ]; + + $resource = new Resource($post, new PostSerializerEmptyAttributes2); + + $document = new Document($resource); + + $this->assertEquals('{"data":{"type":"posts","id":"1"}}', (string) $document, 'Attributes should be omitted'); + } } class PostSerializer2 extends AbstractSerializer @@ -77,6 +90,14 @@ public function comments($post) } } +class PostSerializerEmptyAttributes2 extends PostSerializer2 +{ + public function getAttributes($post, array $fields = null) + { + return []; + } +} + class CommentSerializer2 extends AbstractSerializer { protected $type = 'comments'; From 78e908f388dd1ef000797b66f578e19e45508de0 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sun, 5 Mar 2017 17:10:36 +1030 Subject: [PATCH 15/23] Fix grammar --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 200ee23..974d612 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ A JSON-API Document may contain one primary Element. The primary Element will be #### Sparse Fieldsets -You can specify which fields (attributes and relationships) to be included on an Element using the `fields` method. You must provide a multidimensional array organized by resource type: +You can specify which fields (attributes and relationships) are to be included on an Element using the `fields` method. You must provide a multidimensional array organized by resource type: ```php $collection->fields(['posts' => ['title', 'date']]); From 9eb6173af0d32305712cfeae12f9728e4782d7cb Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sun, 5 Mar 2017 17:11:16 +1030 Subject: [PATCH 16/23] Add documentation about AbstractSerializer's handling of relationship names. --- README.md | 9 +++++++++ src/AbstractSerializer.php | 3 +-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 974d612..311cdda 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,15 @@ public function comments($post) } ``` +By default, the `AbstractSerializer` will convert relationship names from `kebab-case` and `snake_case` into a `camelCase` method name and call that on the serializer. If you wish to customize this behaviour, you may override the `getRelationship` method: + +```php +public function getRelationship($model, $name) +{ + // resolve Relationship called $name for $model +} +``` + ### Meta & Links The `Document`, `Resource`, and `Relationship` classes allow you to add meta information: diff --git a/src/AbstractSerializer.php b/src/AbstractSerializer.php index c5055fe..6e343cc 100644 --- a/src/AbstractSerializer.php +++ b/src/AbstractSerializer.php @@ -85,8 +85,7 @@ public function getRelationship($model, $name) /** * Get the serializer method name for the given relationship. * - * - * underscore, kebab-case is converted into camelCase. + * snake_case and kebab-case are converted into camelCase. * * @param string $name * From f43770e9bdf4473d54705780ea2612ad1bedca35 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sun, 5 Mar 2017 17:13:45 +1030 Subject: [PATCH 17/23] Update changelog --- CHANGELOG.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 194e320..c9d7e92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,19 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +## [Unreleased] +### Added +- [#96](https://github.com/tobscure/json-api/pull/96): Improve invalid parameter exceptions with more information about what's invalid. ([@nubs](https://github.com/nubs)) + +### Changed +- [#106](https://github.com/tobscure/json-api/pull/106): Convert snake_case relationship names into camelCase serializer methods. ([@ustrugany](https://github.com/ustrugany)) + +### Fixed +- [#94](https://github.com/tobscure/json-api/pull/94): Properly support empty to-one relationships. +- [#99](https://github.com/tobscure/json-api/pull/99): Omit attributes on resource identifier objects. ([@anthonyhendrickson](https://github.com/anthonyhendrickson)) +- [#103](https://github.com/tobscure/json-api/issues/103): Prevent resource relationships from being built twice per resource. +- [#110](https://github.com/tobscure/json-api/pull/110): Omit empty attributes on resources. ([@f3ath](https://github.com/f3ath)) + ## [0.3.0] - 2016-03-31 ### Added - [#79](https://github.com/tobscure/json-api/pull/79), [#81](https://github.com/tobscure/json-api/pull/81): Allow serializers to add links and metadata to resources. ([@bwaidelich](https://github.com/bwaidelich)) @@ -39,6 +52,7 @@ Completely rewrite to improve all the things. ## 0.1.0 - 2015-08-07 - Initial release +[Unreleased]: https://github.com/tobscure/json-api/compare/v0.3.0...HEAD [0.3.0]: https://github.com/tobscure/json-api/compare/v0.2.1...v0.3.0 [0.2.1]: https://github.com/tobscure/json-api/compare/v0.2.0...v0.2.1 [0.2.0]: https://github.com/tobscure/json-api/compare/v0.1.1...v0.2.0 From 25a62d3afcc9c2f0190e4d6ec98bc9fd202cef72 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Sun, 5 Mar 2017 17:31:23 +1030 Subject: [PATCH 18/23] Release v0.4.0 --- CHANGELOG.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9d7e92..f75a48e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,12 +2,12 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). -## [Unreleased] +## [0.4.0] - 2017-03-05 ### Added - [#96](https://github.com/tobscure/json-api/pull/96): Improve invalid parameter exceptions with more information about what's invalid. ([@nubs](https://github.com/nubs)) ### Changed -- [#106](https://github.com/tobscure/json-api/pull/106): Convert snake_case relationship names into camelCase serializer methods. ([@ustrugany](https://github.com/ustrugany)) +- [#106](https://github.com/tobscure/json-api/pull/106): Convert snake_case into camelCase when calling a relationship method. ([@ustrugany](https://github.com/ustrugany)) ### Fixed - [#94](https://github.com/tobscure/json-api/pull/94): Properly support empty to-one relationships. @@ -24,7 +24,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - [#85](https://github.com/tobscure/json-api/pull/85): Allow creation of relationships without data. ([@josephmcdermott](https://github.com/josephmcdermott)) ### Fixed -- [#65](https://github.com/tobscure/json-api/pull/65): Convert snake-case into camelCase when calling a relationship method. ([@avoelpel](https://github.com/avoelpel)) +- [#65](https://github.com/tobscure/json-api/pull/65): Convert kebab-case into camelCase when calling a relationship method. ([@avoelpel](https://github.com/avoelpel)) - [#70](https://github.com/tobscure/json-api/pull/70): Include related resources even if relationship is not listed in sparse fieldset. ([@Damith88](https://github.com/Damith88)) - [#72](https://github.com/tobscure/json-api/pull/72): Return `null` in `Parameters::getLimit` if no limit is set. ([@byCedric](https://github.com/byCedric)) - [46142e5](https://github.com/tobscure/json-api/commit/46142e5823da3bebbd9dfc38833af4d808a5e3f3): Prevent primary "data" resources from showing up again in the "included" array. ([@tobscure](https://github.com/tobscure)) @@ -52,7 +52,7 @@ Completely rewrite to improve all the things. ## 0.1.0 - 2015-08-07 - Initial release -[Unreleased]: https://github.com/tobscure/json-api/compare/v0.3.0...HEAD +[0.4.0]: https://github.com/tobscure/json-api/compare/v0.3.0...v0.4.0 [0.3.0]: https://github.com/tobscure/json-api/compare/v0.2.1...v0.3.0 [0.2.1]: https://github.com/tobscure/json-api/compare/v0.2.0...v0.2.1 [0.2.0]: https://github.com/tobscure/json-api/compare/v0.1.1...v0.2.0 From cb66839da0c7c0de72b4afbf6f80784bcb98b73c Mon Sep 17 00:00:00 2001 From: Alexey Karapetov Date: Sun, 5 Mar 2017 12:26:28 -0800 Subject: [PATCH 19/23] Do not call getAttributes() twice --- src/Resource.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Resource.php b/src/Resource.php index 407d358..b52230b 100644 --- a/src/Resource.php +++ b/src/Resource.php @@ -80,7 +80,7 @@ public function toArray() if (! $this->isIdentifier()) { $attributes = $this->getAttributes(); if ($attributes) { - $array['attributes'] = $this->getAttributes(); + $array['attributes'] = $attributes; } } From d4ba8437977c33a5189d95d9ffa751997f13b104 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Mon, 6 Mar 2017 07:46:24 +1030 Subject: [PATCH 20/23] Release v0.4.1 --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f75a48e..0110ce6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +## [0.4.1] - 2017-03-06 +### Fixed +- [#114](https://github.com/tobscure/json-api/pull/114): Do not call `getAttributes()` twice. ([@f3ath](https://github.com/f3ath)) + ## [0.4.0] - 2017-03-05 ### Added - [#96](https://github.com/tobscure/json-api/pull/96): Improve invalid parameter exceptions with more information about what's invalid. ([@nubs](https://github.com/nubs)) @@ -52,6 +56,7 @@ Completely rewrite to improve all the things. ## 0.1.0 - 2015-08-07 - Initial release +[0.4.1]: https://github.com/tobscure/json-api/compare/v0.4.0...v0.4.1 [0.4.0]: https://github.com/tobscure/json-api/compare/v0.3.0...v0.4.0 [0.3.0]: https://github.com/tobscure/json-api/compare/v0.2.1...v0.3.0 [0.2.1]: https://github.com/tobscure/json-api/compare/v0.2.0...v0.2.1 From 66962f958501c9d8c69a5108174b0c19b7ef667e Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Mon, 6 Mar 2017 08:12:20 +1030 Subject: [PATCH 21/23] Remove old method --- src/Collection.php | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/Collection.php b/src/Collection.php index 33f20ba..4d6707b 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -88,22 +88,6 @@ public function with($relationships) return $this; } - /** - * Request a relationship to be identified for all resources. - * - * @param string|array $relationships - * - * @return $this - */ - public function identify($relationships) - { - foreach ($this->resources as $resource) { - $resource->identify($relationships); - } - - return $this; - } - /** * Request a restricted set of fields. * From 355b213927a1412ede81b67ac2a5605753382379 Mon Sep 17 00:00:00 2001 From: Alexey Date: Wed, 8 Mar 2017 17:29:28 -0800 Subject: [PATCH 22/23] Document:MEDIA_TYPE (#118) * Document:MEDIA_TYPE As proposed in the comments to #117 * Update Document.php --- src/Document.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Document.php b/src/Document.php index cc524d3..aa350ea 100644 --- a/src/Document.php +++ b/src/Document.php @@ -18,6 +18,8 @@ class Document implements JsonSerializable use LinksTrait; use MetaTrait; + const MEDIA_TYPE = 'application/vnd.api+json'; + /** * The included array. * From ff381ed89bcfcb58c4ce333428c0cb6f6fed0539 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Thu, 4 Mar 2021 21:08:05 +1030 Subject: [PATCH 23/23] Add note about project future --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 311cdda..3feee89 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,8 @@ Works with version 1.0 of the spec. +**⚠️ The project is no longer maintained.** Check out [tobyzerner/json-api-server](https://github.com/tobyzerner/json-api-server) instead. + ## Install via Composer: