generated from spatie/package-skeleton-php
-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from floristenhove/feature/percolate-query
Add percolate query
- Loading branch information
Showing
3 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace Spatie\ElasticsearchQueryBuilder\Queries; | ||
|
||
use Exception; | ||
|
||
class PercolateQuery implements Query | ||
{ | ||
protected string $field; | ||
protected array $document; | ||
protected string $index; | ||
protected string $id; | ||
|
||
public static function create(string $field): static | ||
{ | ||
return new self($field); | ||
} | ||
|
||
public function __construct(string $field) | ||
{ | ||
$this->field = $field; | ||
} | ||
|
||
public function setInlineDocument(array $document): static | ||
{ | ||
$this->document = $document; | ||
|
||
return $this; | ||
} | ||
|
||
public function setDocument(string $index, string|int $id): static | ||
{ | ||
$this->index = $index; | ||
$this->id = (string) $id; | ||
|
||
return $this; | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
if(isset($this->document) && isset($this->index)) { | ||
Check failure on line 41 in src/Queries/PercolateQuery.php GitHub Actions / psalmRedundantPropertyInitializationCheck
|
||
throw new Exception('You can only set an inline document or a document, not both.'); | ||
} | ||
|
||
if (!isset($this->document) && !isset($this->index)) { | ||
Check failure on line 45 in src/Queries/PercolateQuery.php GitHub Actions / psalmRedundantPropertyInitializationCheck
|
||
throw new Exception('You must set an inline document or a document.'); | ||
} | ||
|
||
$query = [ | ||
'percolate' => [ | ||
'field' => $this->field, | ||
], | ||
]; | ||
|
||
if (isset($this->document)) { | ||
$query['percolate']['document'] = $this->document; | ||
|
||
return $query; | ||
} | ||
|
||
$query['percolate']['index'] = $this->index; | ||
$query['percolate']['id'] = $this->id; | ||
|
||
return $query; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Spatie\ElasticsearchQueryBuilder\Tests\Queries; | ||
|
||
use Exception; | ||
use PHPUnit\Framework\TestCase; | ||
use Spatie\ElasticsearchQueryBuilder\Queries\PercolateQuery; | ||
|
||
class PercolateQueryTest extends TestCase | ||
{ | ||
public function testCreateReturnsNewInstance(): void | ||
{ | ||
$query = PercolateQuery::create('test_field'); | ||
|
||
self::assertInstanceOf(PercolateQuery::class, $query); | ||
} | ||
|
||
public function testInlineDocumentToArrayBuildsCorrectQuery(): void | ||
{ | ||
$query = (new PercolateQuery('test_field')) | ||
->setInlineDocument(['foo' => 'bar', 'baz' => 'qux']); | ||
|
||
self::assertEquals([ | ||
'percolate' => [ | ||
'field' => 'test_field', | ||
'document' => ['foo' => 'bar', 'baz' => 'qux'], | ||
], | ||
], $query->toArray()); | ||
} | ||
|
||
public function testDocumentToArrayBuildsCorrectQuery(): void | ||
{ | ||
$query = (new PercolateQuery('test_field')) | ||
->setDocument('test_index', 123); | ||
|
||
self::assertEquals([ | ||
'percolate' => [ | ||
'field' => 'test_field', | ||
'index' => 'test_index', | ||
'id' => '123', | ||
], | ||
], $query->toArray()); | ||
} | ||
|
||
public function testToArrayThrowsExceptionWhenBothInlineDocumentAndDocumentAreSet(): void | ||
{ | ||
$this->expectException(Exception::class); | ||
$this->expectExceptionMessage('You can only set an inline document or a document, not both.'); | ||
|
||
(new PercolateQuery('test_field')) | ||
->setInlineDocument(['foo' => 'bar', 'baz' => 'qux']) | ||
->setDocument('test_index', 123) | ||
->toArray(); | ||
} | ||
|
||
public function testToArrayThrowsExceptionWhenNeitherInlineDocumentNorDocumentAreSet(): void | ||
{ | ||
$this->expectException(Exception::class); | ||
$this->expectExceptionMessage('You must set an inline document or a document.'); | ||
|
||
(new PercolateQuery('test_field'))->toArray(); | ||
} | ||
} |