From 7d2bb966c29e123b097f317ae1f2d22a9e054a90 Mon Sep 17 00:00:00 2001 From: Marc Morera Date: Mon, 23 Jan 2023 13:50:07 +0100 Subject: [PATCH] Added item for deletion model class --- Model/Item.php | 8 +++---- Model/ItemForDeletion.php | 29 +++++++++++++++++++++++++ Tests/Model/ItemForDeletionTest.php | 33 +++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 Model/ItemForDeletion.php create mode 100644 Tests/Model/ItemForDeletionTest.php diff --git a/Model/Item.php b/Model/Item.php index 313cde1..c720384 100644 --- a/Model/Item.php +++ b/Model/Item.php @@ -159,7 +159,7 @@ public static function create( array $exactMatchingMetadata = [], array $suggest = [] ) { - return new self( + return new static( $uuid, null, $metadata, @@ -192,7 +192,7 @@ public static function createLocated( array $exactMatchingMetadata = [], array $suggest = [] ) { - return new self( + return new static( $uuid, $coordinate, $metadata, @@ -617,7 +617,7 @@ public static function createFromArray(array $array): self } $item = isset($array['coordinate']) - ? self::createLocated( + ? static::createLocated( ItemUUID::createFromArray($array['uuid']), Coordinate::createFromArray($array['coordinate']), $array['metadata'] ?? [], @@ -626,7 +626,7 @@ public static function createFromArray(array $array): self $array['exact_matching_metadata'] ?? [], $array['suggest'] ?? [] ) - : self::create( + : static::create( ItemUUID::createFromArray($array['uuid']), $array['metadata'] ?? [], $array['indexed_metadata'] ?? [], diff --git a/Model/ItemForDeletion.php b/Model/ItemForDeletion.php new file mode 100644 index 0000000..d8d81d4 --- /dev/null +++ b/Model/ItemForDeletion.php @@ -0,0 +1,29 @@ + + */ + +declare(strict_types=1); + +namespace Apisearch\Model; + +class ItemForDeletion extends Item +{ + /** + * @param ItemUUID $uuid + * + * @return Item + */ + public static function createByUUID(ItemUUID $uuid) + { + return static::create($uuid); + } +} diff --git a/Tests/Model/ItemForDeletionTest.php b/Tests/Model/ItemForDeletionTest.php new file mode 100644 index 0000000..37455b5 --- /dev/null +++ b/Tests/Model/ItemForDeletionTest.php @@ -0,0 +1,33 @@ + + */ + +declare(strict_types=1); + +namespace Apisearch\Tests\Model; + +use Apisearch\Model\ItemForDeletion; +use Apisearch\Model\ItemUUID; +use PHPUnit\Framework\TestCase; + +class ItemForDeletionTest extends TestCase +{ + public function testCreation() + { + $item = ItemForDeletion::createByUUID(ItemUUID::createByComposedUUID('1~product')); + $this->assertEquals('1~product', $item->composeUUID()); + $this->assertInstanceOf(ItemForDeletion::class, $item); + $item = ItemForDeletion::createFromArray($item->toArray()); + $this->assertEquals('1~product', $item->composeUUID()); + $this->assertInstanceOf(ItemForDeletion::class, $item); + } +}