From 3ca1f1b98927076b912006f0d1fed71db4918c7f Mon Sep 17 00:00:00 2001 From: Priit Perna Date: Fri, 27 Sep 2019 15:54:08 +0300 Subject: [PATCH 1/3] Added support for PARTIAL and NOCREATE --- src/Document/Document.php | 42 +++++++++++++++++++++- src/Document/DocumentInterface.php | 4 +++ tests/RediSearch/Document/DocumentTest.php | 20 +++++++---- 3 files changed, 58 insertions(+), 8 deletions(-) diff --git a/src/Document/Document.php b/src/Document/Document.php index 75fe544..eef3a31 100644 --- a/src/Document/Document.php +++ b/src/Document/Document.php @@ -11,6 +11,8 @@ class Document implements DocumentInterface protected $score = 1.0; protected $noSave = false; protected $replace = false; + protected $partial = false; + protected $noCreate = false; protected $payload; protected $language; @@ -33,6 +35,14 @@ public function getHashDefinition(): array if ($this->isReplace()) { $properties[] = 'REPLACE'; + + if ($this->isPartial()) { + $properties[] = 'PARTIAL'; + } + + if ($this->isNoCreate()) { + $properties[] = 'NOCREATE'; + } } return $properties; @@ -51,6 +61,14 @@ public function getDefinition(): array if ($this->isReplace()) { $properties[] = 'REPLACE'; + + if ($this->isPartial()) { + $properties[] = 'PARTIAL'; + } + + if ($this->isNoCreate()) { + $properties[] = 'NOCREATE'; + } } if (!is_null($this->getLanguage())) { @@ -122,7 +140,29 @@ public function setReplace(bool $replace): Document return $this; } - public function getPayload() + public function isPartial(): bool + { + return $this->partial; + } + + public function setPartial(bool $partial): Document + { + $this->partial = $partial; + return $this; + } + + public function isNoCreate(): bool + { + return $this->noCreate; + } + + public function setNoCreate(bool $noCreate): Document + { + $this->$noCreate = $noCreate; + return $this; + } + + public function getPayload() { return $this->payload; } diff --git a/src/Document/DocumentInterface.php b/src/Document/DocumentInterface.php index 3e52e91..bc68470 100644 --- a/src/Document/DocumentInterface.php +++ b/src/Document/DocumentInterface.php @@ -14,6 +14,10 @@ public function isNoSave(): bool; public function setNoSave(bool $noSave): Document; public function isReplace(): bool; public function setReplace(bool $replace): Document; + public function isPartial(): bool; + public function setPartial(bool $partial): Document; + public function isNoCreate(): bool; + public function setNoCreate(bool $noCreate): Document; public function getPayload(); public function setPayload($payload); public function getLanguage(); diff --git a/tests/RediSearch/Document/DocumentTest.php b/tests/RediSearch/Document/DocumentTest.php index 647b4c2..2d83d52 100644 --- a/tests/RediSearch/Document/DocumentTest.php +++ b/tests/RediSearch/Document/DocumentTest.php @@ -29,6 +29,8 @@ public function testShouldGetDefinitionWithOptions() $expectedPayload = 'foo'; $isNoSave = true; $shouldReplace = true; + $shouldPartial = true; + $shouldNoCreate = true; $expectedId = '9999'; $expectedScore = 0.2; $expectedLanguage = 'EN'; @@ -40,6 +42,8 @@ public function testShouldGetDefinitionWithOptions() ->setLanguage($expectedLanguage) ->setPayload($expectedPayload) ->setReplace($shouldReplace) + ->setPartial($shouldPartial) + ->setNoCreate($shouldNoCreate) ->setScore($expectedScore); $subject->customField = FieldFactory::make($expectedFieldName, $expectedFieldValue); @@ -50,13 +54,15 @@ public function testShouldGetDefinitionWithOptions() $this->assertEquals($expectedScore, $definition[1]); $this->assertEquals('NOSAVE', $definition[2]); $this->assertEquals('REPLACE', $definition[3]); - $this->assertEquals('LANGUAGE', $definition[4]); - $this->assertEquals($expectedLanguage, $definition[5]); - $this->assertEquals('PAYLOAD', $definition[6]); - $this->assertEquals($expectedPayload, $definition[7]); - $this->assertEquals('FIELDS', $definition[8]); - $this->assertEquals($expectedFieldName, $definition[9]); - $this->assertEquals($expectedFieldValue, $definition[10]); + $this->assertEquals('PARTIAL', $definition[4]); + $this->assertEquals('NOCREATE', $definition[5]); + $this->assertEquals('LANGUAGE', $definition[6]); + $this->assertEquals($expectedLanguage, $definition[7]); + $this->assertEquals('PAYLOAD', $definition[8]); + $this->assertEquals($expectedPayload, $definition[9]); + $this->assertEquals('FIELDS', $definition[10]); + $this->assertEquals($expectedFieldName, $definition[11]); + $this->assertEquals($expectedFieldValue, $definition[12]); } public function testShouldThrowExceptionWhenScoreIsTooLow() From 2b7584bc73f2f02bc0682d021f8cfabb45220de2 Mon Sep 17 00:00:00 2001 From: Priit Perna Date: Mon, 30 Sep 2019 12:03:21 +0300 Subject: [PATCH 2/3] Fixed Document tests --- src/Document/Document.php | 2 +- tests/RediSearch/Document/DocumentTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Document/Document.php b/src/Document/Document.php index eef3a31..b15b5e5 100644 --- a/src/Document/Document.php +++ b/src/Document/Document.php @@ -158,7 +158,7 @@ public function isNoCreate(): bool public function setNoCreate(bool $noCreate): Document { - $this->$noCreate = $noCreate; + $this->noCreate = $noCreate; return $this; } diff --git a/tests/RediSearch/Document/DocumentTest.php b/tests/RediSearch/Document/DocumentTest.php index 2d83d52..2601a14 100644 --- a/tests/RediSearch/Document/DocumentTest.php +++ b/tests/RediSearch/Document/DocumentTest.php @@ -25,7 +25,7 @@ public function testShouldGetDefinition() public function testShouldGetDefinitionWithOptions() { - $expectedNumberOfElements = 11; + $expectedNumberOfElements = 13; $expectedPayload = 'foo'; $isNoSave = true; $shouldReplace = true; From 30f55349f8bd07c43e78efea8fe7c2e2cfb09489 Mon Sep 17 00:00:00 2001 From: Priit Perna Date: Mon, 30 Sep 2019 12:12:44 +0300 Subject: [PATCH 3/3] Fixed hash definition --- src/Document/Document.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/Document/Document.php b/src/Document/Document.php index b15b5e5..82e3834 100644 --- a/src/Document/Document.php +++ b/src/Document/Document.php @@ -35,14 +35,6 @@ public function getHashDefinition(): array if ($this->isReplace()) { $properties[] = 'REPLACE'; - - if ($this->isPartial()) { - $properties[] = 'PARTIAL'; - } - - if ($this->isNoCreate()) { - $properties[] = 'NOCREATE'; - } } return $properties;