Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions src/objects/LinkObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 === []) {
$this->setHreflang($language);
}
else {
$this->setHreflang(...array_merge($this->hreflang, [$language]));
}
}

/**
* @param string $key
* @param mixed $value
Expand All @@ -54,6 +83,45 @@ public function setHref($href) {
$this->href = $href;
}

/**
* @todo validate according to https://tools.ietf.org/html/rfc8288#section-2.1
*
* @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;
}

/**
* @todo validate according to https://tools.ietf.org/html/rfc5646
*
* @param string ...$hreflang
*/
public function setHreflang(...$hreflang) {
$this->hreflang = $hreflang;
}

/**
* @param MetaObject $metaObject
*/
Expand All @@ -72,6 +140,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;
}
Expand All @@ -90,6 +173,26 @@ public function toArray() {

$array['href'] = $this->href;

if ($this->rel !== null) {
$array['rel'] = $this->rel;
}
if ($this->title !== null) {
$array['title'] = $this->title;
}
if ($this->type !== null) {
$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();
}
Expand Down
120 changes: 120 additions & 0 deletions tests/objects/LinkObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand Down