diff --git a/examples/errors_all_options.php b/examples/errors_all_options.php index 5be9ab7..4f628cc 100644 --- a/examples/errors_all_options.php +++ b/examples/errors_all_options.php @@ -38,7 +38,7 @@ $errorSpecApi->setHumanTitle($genericTitle='Too much options'); $errorSpecApi->setHumanDetails($specificDetails='Please, choose a bit less. Consult your ...'); $errorSpecApi->setAboutLink($specificAboutLink='https://www.example.com/explanation.html', ['foo'=>'bar']); -$errorSpecApi->appendTypeLink($genericTypeLink='https://www.example.com/documentation.html', ['foo'=>'bar']); +$errorSpecApi->setTypeLink($genericTypeLink='https://www.example.com/documentation.html', ['foo'=>'bar']); /** * prepare multiple error objects for the errors response diff --git a/src/helpers/LinksManager.php b/src/helpers/LinksManager.php index 97c1c7f..ab234a4 100644 --- a/src/helpers/LinksManager.php +++ b/src/helpers/LinksManager.php @@ -29,6 +29,8 @@ public function addLink($key, $href, array $meta=[]) { /** * append a link to a key with an array of links * + * @deprecated array links are not supported anymore {@see ->addLink()} + * * @param string $key * @param string $href * @param array $meta optional, if given a LinkObject is added, otherwise a link string is added @@ -56,6 +58,8 @@ public function addLinkObject($key, LinkObject $linkObject) { /** * set a key containing a LinksArray * + * @deprecated array links are not supported anymore {@see ->addLinkObject()} + * * @param string $key * @param LinksArray $linksArray */ @@ -67,6 +71,8 @@ public function addLinksArray($key, LinksArray $linksArray) { /** * append a LinkObject to a key with a LinksArray * + * @deprecated array links are not supported anymore {@see ->addLinkObject()} + * * @param string $key * @param LinkObject $linkObject */ diff --git a/src/objects/ErrorObject.php b/src/objects/ErrorObject.php index 9f0eade..3d40d78 100644 --- a/src/objects/ErrorObject.php +++ b/src/objects/ErrorObject.php @@ -138,7 +138,7 @@ public function setHumanExplanation($genericTitle, $specificDetails=null, $speci $this->setAboutLink($specificAboutLink); } if ($genericTypeLink !== null) { - $this->appendTypeLink($genericTypeLink); + $this->setTypeLink($genericTypeLink); } } @@ -152,9 +152,21 @@ public function setAboutLink($href, array $meta=[]) { $this->addLink('about', $href, $meta); } + /** + * set the link of the generic type of this error, explained in a human-friendly way + * + * @param string $href + * @param array $meta optional, if given a LinkObject is added, otherwise a link string is added + */ + public function setTypeLink($href, array $meta=[]) { + $this->addLink('type', $href, $meta); + } + /** * append a link of the generic type of this error, explained in a human-friendly way * + * @deprecated array links are not supported anymore {@see ->setTypeLink()} + * * @param string $href * @param array $meta optional, if given a LinkObject is added, otherwise a link string is added */ diff --git a/src/objects/LinksObject.php b/src/objects/LinksObject.php index 67e9640..1e759ee 100644 --- a/src/objects/LinksObject.php +++ b/src/objects/LinksObject.php @@ -64,6 +64,8 @@ public function add($key, $href, array $meta=[]) { * * @see LinksArray for use cases * + * @deprecated array links are not supported anymore {@see ->add()} + * * @param string $key * @param string $href * @param array $meta optional, if given a LinkObject is added, otherwise a link string is added @@ -120,6 +122,8 @@ public function addLinkObject($key, LinkObject $linkObject) { } /** + * @deprecated array links are not supported anymore {@see ->addLinkObject()} + * * @param string $key * @param LinksArray $linksArray * @@ -136,6 +140,8 @@ public function addLinksArray($key, LinksArray $linksArray) { } /** + * @deprecated array links are not supported anymore {@see ->addLinkObject()} + * * @param string $key * @param LinkObject $linkObject * diff --git a/src/profiles/CursorPaginationProfile.php b/src/profiles/CursorPaginationProfile.php index 77d9fc2..e95f523 100644 --- a/src/profiles/CursorPaginationProfile.php +++ b/src/profiles/CursorPaginationProfile.php @@ -248,7 +248,7 @@ public function setPaginationMeta(PaginableInterface $paginable, $exactTotal=nul */ public function getUnsupportedSortErrorObject($genericTitle=null, $specificDetails=null) { $errorObject = new ErrorObject('Unsupported sort'); - $errorObject->appendTypeLink('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/unsupported-sort'); + $errorObject->setTypeLink('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/unsupported-sort'); $errorObject->blameQueryParameter('sort'); $errorObject->setHttpStatusCode(400); @@ -278,7 +278,7 @@ public function getUnsupportedSortErrorObject($genericTitle=null, $specificDetai */ public function getMaxPageSizeExceededErrorObject($maxSize, $genericTitle=null, $specificDetails=null) { $errorObject = new ErrorObject('Max page size exceeded'); - $errorObject->appendTypeLink('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/max-size-exceeded'); + $errorObject->setTypeLink('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/max-size-exceeded'); $errorObject->blameQueryParameter('page[size]'); $errorObject->setHttpStatusCode(400); $errorObject->addMeta('page', $value=['maxSize' => $maxSize]); @@ -313,7 +313,7 @@ public function getInvalidParameterValueErrorObject($queryParameter, $typeLink=n $errorObject->setHttpStatusCode(400); if ($typeLink !== null) { - $errorObject->appendTypeLink($typeLink); + $errorObject->setTypeLink($typeLink); } if ($genericTitle !== null) { @@ -337,7 +337,7 @@ public function getInvalidParameterValueErrorObject($queryParameter, $typeLink=n */ public function getRangePaginationNotSupportedErrorObject($genericTitle=null, $specificDetails=null) { $errorObject = new ErrorObject('Range pagination not supported'); - $errorObject->appendTypeLink('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/range-pagination-not-supported'); + $errorObject->setTypeLink('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/range-pagination-not-supported'); $errorObject->setHttpStatusCode(400); if ($genericTitle !== null) { diff --git a/tests/example_output/errors_all_options/errors_all_options.json b/tests/example_output/errors_all_options/errors_all_options.json index b0cd4a3..11c8e33 100644 --- a/tests/example_output/errors_all_options/errors_all_options.json +++ b/tests/example_output/errors_all_options/errors_all_options.json @@ -17,9 +17,7 @@ "detail": "Please, choose a bit less. Consult your ...", "links": { "about": "https://www.example.com/explanation.html", - "type": [ - "https://www.example.com/documentation.html" - ] + "type": "https://www.example.com/documentation.html" } }, { @@ -35,14 +33,12 @@ "foo": "bar" } }, - "type": [ - { - "href": "https://www.example.com/documentation.html", - "meta": { - "foo": "bar" - } + "type": { + "href": "https://www.example.com/documentation.html", + "meta": { + "foo": "bar" } - ] + } }, "source": { "pointer": "/data/attributes/title", diff --git a/tests/example_output/errors_all_options/errors_all_options.php b/tests/example_output/errors_all_options/errors_all_options.php index 7ca6e96..f0a4076 100644 --- a/tests/example_output/errors_all_options/errors_all_options.php +++ b/tests/example_output/errors_all_options/errors_all_options.php @@ -19,7 +19,7 @@ public static function createJsonapiDocument() { $errorSpecApi->setHumanTitle($genericTitle='Too much options'); $errorSpecApi->setHumanDetails($specificDetails='Please, choose a bit less. Consult your ...'); $errorSpecApi->setAboutLink($specificAboutLink='https://www.example.com/explanation.html', ['foo'=>'bar']); - $errorSpecApi->appendTypeLink($genericTypeLink='https://www.example.com/documentation.html', ['foo'=>'bar']); + $errorSpecApi->setTypeLink($genericTypeLink='https://www.example.com/documentation.html', ['foo'=>'bar']); $metaObject = new \stdClass(); $metaObject->property = 'value'; diff --git a/tests/helpers/LinksManagerTest.php b/tests/helpers/LinksManagerTest.php index 980f253..14bf66b 100644 --- a/tests/helpers/LinksManagerTest.php +++ b/tests/helpers/LinksManagerTest.php @@ -82,6 +82,9 @@ public function testAddLinkObject_HappyPath() { $this->assertSame('https://jsonapi.org', $array['foo']['href']); } + /** + * @deprecated array links are not supported anymore + */ public function testAddLinksArray_HappyPath() { $linksArray = new LinksArray(); $linksArray->add('https://jsonapi.org'); @@ -98,6 +101,9 @@ public function testAddLinksArray_HappyPath() { $this->assertSame('https://jsonapi.org', $array['foo'][0]); } + /** + * @deprecated array links are not supported anymore + */ public function testAppendLinkObject_HappyPath() { $linksManager = new LinksManager(); $linksManager->addLinksArray('foo', LinksArray::fromArray(['https://jsonapi.org/1'])); diff --git a/tests/objects/ErrorObjectTest.php b/tests/objects/ErrorObjectTest.php index 7046e31..98a3125 100644 --- a/tests/objects/ErrorObjectTest.php +++ b/tests/objects/ErrorObjectTest.php @@ -114,6 +114,24 @@ public function testFromException_BlocksNonException() { ErrorObject::fromException(new \stdClass()); } + /** + * @deprecated array links are not supported anymore + */ + public function testAppendTypeLink_HappyPath() { + $errorObject = new ErrorObject(); + $this->assertTrue($errorObject->isEmpty()); + + $errorObject->appendTypeLink('https://jsonapi.org'); + + $this->assertFalse($errorObject->isEmpty()); + + $array = $errorObject->toArray(); + + $this->assertArrayHasKey('links', $array); + $this->assertArrayHasKey('type', $array['links']); + $this->assertSame(['https://jsonapi.org'], $array['links']['type']); + } + public function testIsEmpty_All() { $errorObject = new ErrorObject(); $this->assertTrue($errorObject->isEmpty()); diff --git a/tests/objects/LinksArrayTest.php b/tests/objects/LinksArrayTest.php index 5b9945c..f0d0c7d 100644 --- a/tests/objects/LinksArrayTest.php +++ b/tests/objects/LinksArrayTest.php @@ -5,6 +5,9 @@ use alsvanzelf\jsonapi\objects\LinksArray; use PHPUnit\Framework\TestCase; +/** + * @deprecated array links are not supported anymore + */ class LinksArrayTest extends TestCase { public function testFromObject_HappyPath() { $object = new \stdClass(); diff --git a/tests/objects/LinksObjectTest.php b/tests/objects/LinksObjectTest.php index ac7b166..ac2995f 100644 --- a/tests/objects/LinksObjectTest.php +++ b/tests/objects/LinksObjectTest.php @@ -109,6 +109,9 @@ public function testAddLinkObject_ExistingKey() { $linksObject->addLinkObject($key='foo', $linkObject); } + /** + * @deprecated array links are not supported anymore + */ public function testAddLinksArray_HappyPath() { $linksObject = new LinksObject(); $linksObject->addLinksArray('foo', LinksArray::fromArray(['https://jsonapi.org'])); @@ -122,6 +125,9 @@ public function testAddLinksArray_HappyPath() { $this->assertSame('https://jsonapi.org', $array['foo'][0]); } + /** + * @deprecated array links are not supported anymore + */ public function testAddLinksArray_BlocksReusingNonArray() { $linksObject = new LinksObject(); $linksObject->add('foo', 'https://jsonapi.org'); diff --git a/tests/profiles/CursorPaginationProfileTest.php b/tests/profiles/CursorPaginationProfileTest.php index 4fb3db8..97704d3 100644 --- a/tests/profiles/CursorPaginationProfileTest.php +++ b/tests/profiles/CursorPaginationProfileTest.php @@ -229,12 +229,11 @@ public function testGetUnsupportedSortErrorObject_HappyPath() { $this->assertArrayHasKey('type', $array['links']); $this->assertArrayHasKey('source', $array); $this->assertArrayHasKey('parameter', $array['source']); - $this->assertCount(1, $array['links']['type']); $this->assertSame('400', $array['status']); $this->assertSame('Unsupported sort', $array['code']); $this->assertSame($genericTitle, $array['title']); $this->assertSame($specificDetails, $array['detail']); - $this->assertSame('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/unsupported-sort', $array['links']['type'][0]); + $this->assertSame('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/unsupported-sort', $array['links']['type']); $this->assertSame('sort', $array['source']['parameter']); } @@ -259,13 +258,12 @@ public function testGetMaxPageSizeExceededErrorObject_HappyPath() { $this->assertArrayHasKey('meta', $array); $this->assertArrayHasKey('page', $array['meta']); $this->assertArrayHasKey('maxSize', $array['meta']['page']); - $this->assertCount(1, $array['links']['type']); $this->assertSame('400', $array['status']); $this->assertSame('Max page size exceeded', $array['code']); $this->assertSame($genericTitle, $array['title']); $this->assertSame($specificDetails, $array['detail']); $this->assertSame('page[size]', $array['source']['parameter']); - $this->assertSame('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/max-size-exceeded', $array['links']['type'][0]); + $this->assertSame('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/max-size-exceeded', $array['links']['type']); $this->assertSame(42, $array['meta']['page']['maxSize']); } @@ -288,13 +286,12 @@ public function testGetInvalidParameterValueErrorObject_HappyPath() { $this->assertArrayHasKey('type', $array['links']); $this->assertArrayHasKey('source', $array); $this->assertArrayHasKey('parameter', $array['source']); - $this->assertCount(1, $array['links']['type']); $this->assertSame('400', $array['status']); $this->assertSame('Invalid parameter value', $array['code']); $this->assertSame($genericTitle, $array['title']); $this->assertSame($specificDetails, $array['detail']); $this->assertSame('page[size]', $array['source']['parameter']); - $this->assertSame('https://jsonapi.org', $array['links']['type'][0]); + $this->assertSame('https://jsonapi.org', $array['links']['type']); } public function testGetRangePaginationNotSupportedErrorObject_HappyPath() { @@ -312,12 +309,11 @@ public function testGetRangePaginationNotSupportedErrorObject_HappyPath() { $this->assertArrayHasKey('detail', $array); $this->assertArrayHasKey('links', $array); $this->assertArrayHasKey('type', $array['links']); - $this->assertCount(1, $array['links']['type']); $this->assertSame('400', $array['status']); $this->assertSame('Range pagination not supported', $array['code']); $this->assertSame($genericTitle, $array['title']); $this->assertSame($specificDetails, $array['detail']); - $this->assertSame('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/range-pagination-not-supported', $array['links']['type'][0]); + $this->assertSame('https://jsonapi.org/profiles/ethanresnick/cursor-pagination/range-pagination-not-supported', $array['links']['type']); } public function testSetQueryParameter_HappyPath() {