From e886ae42ca5d5b94a12fb6431ef2363be094fee5 Mon Sep 17 00:00:00 2001 From: Lode Claassen Date: Tue, 20 Oct 2020 22:31:06 +0200 Subject: [PATCH 1/6] support the new link 'native-meta' properties --- src/objects/LinkObject.php | 84 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/src/objects/LinkObject.php b/src/objects/LinkObject.php index 1eab36d..bc23cb4 100644 --- a/src/objects/LinkObject.php +++ b/src/objects/LinkObject.php @@ -11,6 +11,16 @@ class LinkObject implements ObjectInterface { /** @var string */ protected $href; + /** @var string */ + protected $rel; + /** @var LinkObject */ + protected $describedby; + /** @var string */ + protected $title; + /** @var string */ + protected $type; + /** @var string[] */ + protected $hreflang; /** @var MetaObject */ protected $meta; @@ -31,6 +41,25 @@ public function __construct($href=null, array $meta=[]) { * human api */ + /** + * @param string $href + */ + public function setDescribedBy($href) { + $this->setDescribedByLinkObject(new LinkObject($href)); + } + + /** + * @param string $language + */ + public function addLanguage($language) { + if ($this->hreflang === null) { + $this->setHreflang($language); + } + else { + $this->setHreflang(...$this->hreflang, $language); + } + } + /** * @param string $key * @param mixed $value @@ -54,6 +83,41 @@ public function setHref($href) { $this->href = $href; } + /** + * @param string $relationType + */ + public function setRelationType($relationType) { + $this->rel = $relationType; + } + + /** + * @param LinkObject $describedBy + */ + public function setDescribedByLinkObject(LinkObject $describedBy) { + $this->describedby = $describedBy; + } + + /** + * @param string $friendlyTitle + */ + public function setHumanTitle($humanTitle) { + $this->title = $humanTitle; + } + + /** + * @param string $mediaType + */ + public function setMediaType($mediaType) { + $this->type = $mediaType; + } + + /** + * @param string ...$hreflang + */ + public function setHreflang(...$hreflang) { + $this->hreflang = $hreflang; + } + /** * @param MetaObject $metaObject */ @@ -90,6 +154,26 @@ public function toArray() { $array['href'] = $this->href; + if ($this->rel) { + $array['rel'] = $this->rel; + } + if ($this->title) { + $array['title'] = $this->title; + } + if ($this->type) { + $array['type'] = $this->type; + } + if ($this->hreflang) { + if (count($this->hreflang) === 1) { + $array['hreflang'] = $this->hreflang[0]; + } + else { + $array['hreflang'] = $this->hreflang; + } + } + if ($this->describedby !== null && $this->describedby->isEmpty() === false) { + $array['describedby'] = $this->describedby->toArray(); + } if ($this->meta !== null && $this->meta->isEmpty() === false) { $array['meta'] = $this->meta->toArray(); } From b13f3269d1636c7e040a436b61b98fb6107aebe5 Mon Sep 17 00:00:00 2001 From: Lode Claassen Date: Sat, 24 Oct 2020 11:40:40 +0200 Subject: [PATCH 2/6] fix type checking --- src/objects/LinkObject.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/objects/LinkObject.php b/src/objects/LinkObject.php index bc23cb4..63fd793 100644 --- a/src/objects/LinkObject.php +++ b/src/objects/LinkObject.php @@ -163,7 +163,7 @@ public function toArray() { if ($this->type) { $array['type'] = $this->type; } - if ($this->hreflang) { + if ($this->hreflang !== []) { if (count($this->hreflang) === 1) { $array['hreflang'] = $this->hreflang[0]; } From 3c3f011024244bf6ea22ea48126d7410c8c27ba9 Mon Sep 17 00:00:00 2001 From: Lode Claassen Date: Sat, 15 May 2021 12:49:21 +0200 Subject: [PATCH 3/6] fix adding link languages --- src/objects/LinkObject.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/objects/LinkObject.php b/src/objects/LinkObject.php index 63fd793..08efd81 100644 --- a/src/objects/LinkObject.php +++ b/src/objects/LinkObject.php @@ -20,7 +20,7 @@ class LinkObject implements ObjectInterface { /** @var string */ protected $type; /** @var string[] */ - protected $hreflang; + protected $hreflang = []; /** @var MetaObject */ protected $meta; @@ -52,11 +52,11 @@ public function setDescribedBy($href) { * @param string $language */ public function addLanguage($language) { - if ($this->hreflang === null) { + if ($this->hreflang === []) { $this->setHreflang($language); } else { - $this->setHreflang(...$this->hreflang, $language); + $this->setHreflang(...array_merge($this->hreflang, [$language])); } } From 7e53ef2b7a01cfc4fbea4f74a88b28b55ccb3f98 Mon Sep 17 00:00:00 2001 From: Lode Claassen Date: Sat, 15 May 2021 12:49:40 +0200 Subject: [PATCH 4/6] test emptyness --- src/objects/LinkObject.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/objects/LinkObject.php b/src/objects/LinkObject.php index 08efd81..f7025a7 100644 --- a/src/objects/LinkObject.php +++ b/src/objects/LinkObject.php @@ -136,6 +136,21 @@ public function isEmpty() { if ($this->href !== null) { return false; } + if ($this->rel !== null) { + return false; + } + if ($this->title !== null) { + return false; + } + if ($this->type !== null) { + return false; + } + if ($this->hreflang !== []) { + return false; + } + if ($this->describedby !== null && $this->describedby->isEmpty() === false) { + return false; + } if ($this->meta !== null && $this->meta->isEmpty() === false) { return false; } @@ -154,13 +169,13 @@ public function toArray() { $array['href'] = $this->href; - if ($this->rel) { + if ($this->rel !== null) { $array['rel'] = $this->rel; } - if ($this->title) { + if ($this->title !== null) { $array['title'] = $this->title; } - if ($this->type) { + if ($this->type !== null) { $array['type'] = $this->type; } if ($this->hreflang !== []) { From 03f34f50cf5037eb05e3fd244650cdb3f2d1c2e2 Mon Sep 17 00:00:00 2001 From: Lode Claassen Date: Sat, 15 May 2021 12:49:48 +0200 Subject: [PATCH 5/6] add some future validation ideas --- src/objects/LinkObject.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/objects/LinkObject.php b/src/objects/LinkObject.php index f7025a7..837875e 100644 --- a/src/objects/LinkObject.php +++ b/src/objects/LinkObject.php @@ -84,6 +84,8 @@ public function setHref($href) { } /** + * @todo validate according to https://tools.ietf.org/html/rfc8288#section-2.1 + * * @param string $relationType */ public function setRelationType($relationType) { @@ -112,6 +114,8 @@ public function setMediaType($mediaType) { } /** + * @todo validate according to https://tools.ietf.org/html/rfc5646 + * * @param string ...$hreflang */ public function setHreflang(...$hreflang) { From 353ef77344138c5e78e000657eab1d326b96325e Mon Sep 17 00:00:00 2001 From: Lode Claassen Date: Sat, 15 May 2021 12:49:54 +0200 Subject: [PATCH 6/6] test the new link properties --- tests/objects/LinkObjectTest.php | 120 +++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) diff --git a/tests/objects/LinkObjectTest.php b/tests/objects/LinkObjectTest.php index bdf7e89..ad41068 100644 --- a/tests/objects/LinkObjectTest.php +++ b/tests/objects/LinkObjectTest.php @@ -6,6 +6,49 @@ use PHPUnit\Framework\TestCase; class LinkObjectTest extends TestCase { + public function testSetDescribedBy_HappyPath() { + $linkObject = new LinkObject(); + + $this->assertTrue($linkObject->isEmpty()); + + $linkObject->setDescribedBy('https://jsonapi.org'); + + $this->assertFalse($linkObject->isEmpty()); + + $array = $linkObject->toArray(); + + $this->assertArrayHasKey('describedby', $array); + $this->assertArrayHasKey('href', $array['describedby']); + $this->assertSame('https://jsonapi.org', $array['describedby']['href']); + } + + public function testAddLanguage_HappyPath() { + $linkObject = new LinkObject(); + + $this->assertTrue($linkObject->isEmpty()); + + $linkObject->addLanguage('nl-NL'); + + $this->assertFalse($linkObject->isEmpty()); + + $array = $linkObject->toArray(); + + $this->assertArrayHasKey('hreflang', $array); + $this->assertSame('nl-NL', $array['hreflang']); + } + + public function testAddLanguage_Multiple() { + $linkObject = new LinkObject(); + + $linkObject->addLanguage('nl-NL'); + $array = $linkObject->toArray(); + $this->assertSame('nl-NL', $array['hreflang']); + + $linkObject->addLanguage('en-US'); + $array = $linkObject->toArray(); + $this->assertSame(['nl-NL', 'en-US'], $array['hreflang']); + } + public function testAddMeta_HappyPath() { $linkObject = new LinkObject(); @@ -22,6 +65,83 @@ public function testAddMeta_HappyPath() { $this->assertSame('bar', $array['meta']['foo']); } + public function testSetRelationType_HappyPath() { + $linkObject = new LinkObject(); + + $this->assertTrue($linkObject->isEmpty()); + + $linkObject->setRelationType('external'); + + $this->assertFalse($linkObject->isEmpty()); + + $array = $linkObject->toArray(); + + $this->assertArrayHasKey('rel', $array); + $this->assertSame('external', $array['rel']); + } + + public function testSetDescribedByLinkObject_HappyPath() { + $linkObject = new LinkObject(); + + $this->assertTrue($linkObject->isEmpty()); + + $describedBy = new LinkObject('https://jsonapi.org'); + $linkObject->setDescribedByLinkObject($describedBy); + + $this->assertFalse($linkObject->isEmpty()); + + $array = $linkObject->toArray(); + + $this->assertArrayHasKey('describedby', $array); + $this->assertArrayHasKey('href', $array['describedby']); + $this->assertSame('https://jsonapi.org', $array['describedby']['href']); + } + + public function testSetHumanTitle_HappyPath() { + $linkObject = new LinkObject(); + + $this->assertTrue($linkObject->isEmpty()); + + $linkObject->setHumanTitle('A link'); + + $this->assertFalse($linkObject->isEmpty()); + + $array = $linkObject->toArray(); + + $this->assertArrayHasKey('title', $array); + $this->assertSame('A link', $array['title']); + } + + public function testSetMediaType_HappyPath() { + $linkObject = new LinkObject(); + + $this->assertTrue($linkObject->isEmpty()); + + $linkObject->setMediaType('text/html'); + + $this->assertFalse($linkObject->isEmpty()); + + $array = $linkObject->toArray(); + + $this->assertArrayHasKey('type', $array); + $this->assertSame('text/html', $array['type']); + } + + public function testSetHreflang_HappyPath() { + $linkObject = new LinkObject(); + + $this->assertTrue($linkObject->isEmpty()); + + $linkObject->setHreflang('nl-NL', 'en-US'); + + $this->assertFalse($linkObject->isEmpty()); + + $array = $linkObject->toArray(); + + $this->assertArrayHasKey('hreflang', $array); + $this->assertSame(['nl-NL', 'en-US'], $array['hreflang']); + } + public function testIsEmpty_WithAtMembers() { $linkObject = new LinkObject();