Skip to content

Commit 4337d43

Browse files
committed
add HasDefaultOptions trait
1 parent 07f55c5 commit 4337d43

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

src/Features/HasDefaultOptions.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Erichard\ElasticQueryBuilder\Features;
6+
7+
trait HasDefaultOptions
8+
{
9+
protected array $defaultOptions = [];
10+
11+
public function getDefaultOptions(): array
12+
{
13+
return $this->defaultOptions;
14+
}
15+
16+
public function setDefaultOptions(array $defaultOptions): self
17+
{
18+
$this->defaultOptions = $defaultOptions;
19+
20+
return $this;
21+
}
22+
}

src/Query/QueryStringQuery.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Erichard\ElasticQueryBuilder\Contracts\QueryInterface;
88
use Erichard\ElasticQueryBuilder\Features\HasBoost;
9+
use Erichard\ElasticQueryBuilder\Features\HasDefaultOptions;
910
use Erichard\ElasticQueryBuilder\Features\HasMinimumShouldMatch;
1011
use Erichard\ElasticQueryBuilder\Features\HasRewrite;
1112

@@ -14,6 +15,7 @@ class QueryStringQuery implements QueryInterface
1415
use HasBoost;
1516
use HasMinimumShouldMatch;
1617
use HasRewrite;
18+
use HasDefaultOptions;
1719

1820
public function __construct(
1921
protected string $query,
@@ -59,9 +61,8 @@ public function setFields(?array $fields): self
5961

6062
public function build(): array
6163
{
62-
$build = [
63-
'query' => $this->query,
64-
];
64+
$build = $this->getDefaultOptions();
65+
$build['query'] = $this->query;
6566

6667
if (null !== $this->defaultField) {
6768
$build['default_field'] = $this->defaultField;

src/Query/RangeQuery.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Erichard\ElasticQueryBuilder\Contracts\QueryInterface;
88
use Erichard\ElasticQueryBuilder\Features\HasBoost;
9+
use Erichard\ElasticQueryBuilder\Features\HasDefaultOptions;
910
use Erichard\ElasticQueryBuilder\Features\HasField;
1011
use Erichard\ElasticQueryBuilder\Features\HasFormat;
1112
use Erichard\ElasticQueryBuilder\QueryException;
@@ -15,6 +16,7 @@ class RangeQuery implements QueryInterface
1516
use HasField;
1617
use HasFormat;
1718
use HasBoost;
19+
use HasDefaultOptions;
1820

1921
public function __construct(
2022
string $field,
@@ -60,7 +62,7 @@ public function lte(int|float|string|null $value): self
6062

6163
public function build(): array
6264
{
63-
$query = [];
65+
$query = $this->getDefaultOptions();
6466

6567
if (null !== $this->gt) {
6668
$query['gt'] = $this->gt;

0 commit comments

Comments
 (0)