Skip to content

Commit 4497afe

Browse files
authored
Merge pull request #120 from apisearch-io/feature/added-optional-repository-reference-to-item
Added optional repository reference to item
2 parents 2180ab1 + 9d531bf commit 4497afe

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

Model/Item.php

+29
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
namespace Apisearch\Model;
1717

1818
use Apisearch\Exception\InvalidFormatException;
19+
use Apisearch\Repository\RepositoryReference;
1920

2021
/**
2122
* Class Item.
@@ -99,6 +100,11 @@ class Item implements HttpTransportable, UUIDReference
99100
*/
100101
private $score;
101102

103+
/**
104+
* @var RepositoryReference
105+
*/
106+
private $repositoryReference;
107+
102108
/**
103109
* Item constructor.
104110
*
@@ -489,6 +495,22 @@ public function setScore(float $score)
489495
return $this;
490496
}
491497

498+
/**
499+
* @return RepositoryReference|null
500+
*/
501+
public function getRepositoryReference():? RepositoryReference
502+
{
503+
return $this->repositoryReference;
504+
}
505+
506+
/**
507+
* @param RepositoryReference $repositoryReference
508+
*/
509+
public function setRepositoryReference(RepositoryReference $repositoryReference)
510+
{
511+
$this->repositoryReference = $repositoryReference;
512+
}
513+
492514
/**
493515
* To array.
494516
*
@@ -510,6 +532,9 @@ public function toArray(): array
510532
'highlights' => $this->highlights,
511533
'is_promoted' => !$this->promoted ? null : true,
512534
'score' => $this->score,
535+
'repository_reference' => $this->repositoryReference instanceof RepositoryReference
536+
? $this->repositoryReference->compose()
537+
: null,
513538
], function ($element) {
514539
return
515540
!(
@@ -577,6 +602,10 @@ public static function createFromArray(array $array): self
577602
$item->setScore((float) $array['score']);
578603
}
579604

605+
if (isset($array['repository_reference']) && !is_null($array['repository_reference'])) {
606+
$item->setRepositoryReference(RepositoryReference::createFromComposed($array['repository_reference']));
607+
}
608+
580609
return $item;
581610
}
582611

Tests/Model/ItemTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515

1616
namespace Apisearch\Tests\Model;
1717

18+
use Apisearch\Model\AppUUID;
1819
use Apisearch\Model\Coordinate;
20+
use Apisearch\Model\IndexUUID;
1921
use Apisearch\Model\Item;
2022
use Apisearch\Model\ItemUUID;
23+
use Apisearch\Repository\RepositoryReference;
2124
use PHPUnit\Framework\TestCase;
2225

2326
/**
@@ -426,4 +429,33 @@ public function testScore()
426429
$this->assertEquals(5.5, $item->toArray()['score']);
427430
$this->assertEquals(5.5, Item::createFromArray($item->toArray())->getScore());
428431
}
432+
433+
/**
434+
* Test index and app UUID
435+
*/
436+
public function testIndexAndAppUUID()
437+
{
438+
$appUUID = AppUUID::createById('app1');
439+
$indexUUID = IndexUUID::createById('index1');
440+
$repositoryReference = RepositoryReference::create($appUUID, $indexUUID);
441+
442+
$item = Item::create(ItemUUID::createByComposedUUID('1~item'));
443+
$this->assertNull($item->getRepositoryReference());
444+
$item = Item::createFromArray($item->toArray());
445+
$this->assertNull($item->getRepositoryReference());
446+
447+
448+
449+
$item = Item::create(ItemUUID::createByComposedUUID('1~item'));
450+
$item->setRepositoryReference($repositoryReference);
451+
$this->assertEquals(
452+
$repositoryReference,
453+
$item->getRepositoryReference()
454+
);
455+
$item = Item::createFromArray($item->toArray());
456+
$this->assertEquals(
457+
$repositoryReference,
458+
$item->getRepositoryReference()
459+
);
460+
}
429461
}

0 commit comments

Comments
 (0)