Skip to content

Commit d033bb3

Browse files
authored
v3.0.0
__BREAKING CHANGES__ * Require php 8.1. * Use new php enum instead of the home grown versions.
1 parent d35cb9a commit d033bb3

26 files changed

+168
-350
lines changed

.travis.yml

Lines changed: 0 additions & 8 deletions
This file was deleted.

CHANGELOG-3.x.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# CHANGELOG for 3.x
2+
This changelog references the relevant changes done in 3.x versions.
3+
4+
5+
## v3.0.0
6+
__BREAKING CHANGES__
7+
8+
* Require php 8.1.
9+
* Use new php enum instead of the home grown versions.

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
"type": "library",
66
"license": "Apache-2.0",
77
"require": {
8-
"php": ">=7.4"
8+
"php": ">=8.1"
99
},
1010
"require-dev": {
11-
"phpunit/phpunit": "^9.2",
12-
"ruflin/elastica": "^7.0"
11+
"phpunit/phpunit": "^9.5",
12+
"ruflin/elastica": "^7.1"
1313
},
1414
"autoload": {
1515
"psr-4": {

src/Builder/ElasticaQueryBuilder.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ protected function handleRange(Range $range, Field $field, bool $cacheable = fal
140140
$boost = $field->getBoost();
141141
$boolOp = $field->getBoolOperator();
142142

143-
if ($boolOp->equals(BoolOperator::REQUIRED())) {
143+
if ($boolOp === BoolOperator::REQUIRED) {
144144
$method = 'addMust';
145-
} elseif ($boolOp->equals(BoolOperator::PROHIBITED())) {
145+
} elseif ($boolOp === BoolOperator::PROHIBITED) {
146146
$method = 'addMustNot';
147147
} else {
148148
$method = 'addShould';
@@ -222,9 +222,9 @@ protected function endSubquery(Subquery $subquery, ?Field $field = null): void
222222
$this->boolQuery->setBoost($boost);
223223
}
224224

225-
if ($boolOp->equals(BoolOperator::REQUIRED())) {
225+
if ($boolOp === BoolOperator::REQUIRED) {
226226
$this->outerBoolQuery->addMust($this->boolQuery);
227-
} elseif ($boolOp->equals(BoolOperator::PROHIBITED())) {
227+
} elseif ($boolOp === BoolOperator::PROHIBITED) {
228228
$this->outerBoolQuery->addMustNot($this->boolQuery);
229229
} else {
230230
$this->outerBoolQuery->addShould($this->boolQuery);
@@ -389,7 +389,7 @@ protected function addTermToQuery(string $method, Node $node, ?Field $field = nu
389389
$useBoost ? $boost : Date::DEFAULT_BOOST
390390
);
391391
} elseif ($node instanceof Numbr && $node->useComparisonOperator()) {
392-
$data = [$node->getComparisonOperator()->getValue() => $value];
392+
$data = [$node->getComparisonOperator()->value => $value];
393393
if ($useBoost) {
394394
$data['boost'] = $boost;
395395
}
@@ -431,7 +431,7 @@ protected function createDateRangeForSingleNode(
431431
bool $cacheable = false,
432432
float $boost = Date::DEFAULT_BOOST
433433
): RangeQuery {
434-
$operator = $node->getComparisonOperator()->getValue();
434+
$operator = $node->getComparisonOperator();
435435

436436
if ($operator === ComparisonOperator::EQ) {
437437
$date = $node->toDateTime($this->localTimeZone);
@@ -440,7 +440,7 @@ protected function createDateRangeForSingleNode(
440440
'lt' => $date->modify('+1 day')->format('Y-m-d'),
441441
];
442442
} else {
443-
$data = [$operator => $node->toDateTime($this->localTimeZone)->format('Y-m-d')];
443+
$data = [$operator->value => $node->toDateTime($this->localTimeZone)->format('Y-m-d')];
444444
}
445445

446446
if ($cacheable) {
@@ -453,7 +453,7 @@ protected function createDateRangeForSingleNode(
453453

454454
protected function addToBoolQuery(string $method, string $fieldName, AbstractQuery $query): void
455455
{
456-
if (false === strpos($fieldName, '.')) {
456+
if (!str_contains($fieldName, '.')) {
457457
$this->boolQuery->$method($query);
458458
return;
459459
}

src/Builder/XmlQueryBuilder.php

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected function startField(Field $field, bool $cacheable = false): void
5858
$tag = sprintf('field name="%s"', $field->getName());
5959

6060
if (!$field->isOptional()) {
61-
$tag .= sprintf(' bool_operator="%s"', strtolower($field->getBoolOperator()->getName()));
61+
$tag .= sprintf(' bool_operator="%s"', strtolower($field->getBoolOperator()->name));
6262
}
6363

6464
if ($cacheable) {
@@ -188,27 +188,13 @@ protected function printSimpleNode(string $rule, Node $node, ?Field $field = nul
188188
$tag .= sprintf(' rule="%s"', $snaked);
189189

190190
if ($node instanceof Numbr || $node instanceof Date) {
191-
switch ($node->getComparisonOperator()->getValue()) {
192-
case ComparisonOperator::GT:
193-
$comparisonOperator = 'gt';
194-
break;
195-
196-
case ComparisonOperator::GTE:
197-
$comparisonOperator = 'gte';
198-
break;
199-
200-
case ComparisonOperator::LT:
201-
$comparisonOperator = 'lt';
202-
break;
203-
204-
case ComparisonOperator::LTE:
205-
$comparisonOperator = 'lte';
206-
break;
207-
208-
default:
209-
$comparisonOperator = null;
210-
break;
211-
}
191+
$comparisonOperator = match ($node->getComparisonOperator()) {
192+
ComparisonOperator::GT => 'gt',
193+
ComparisonOperator::GTE => 'gte',
194+
ComparisonOperator::LT => 'lt',
195+
ComparisonOperator::LTE => 'lte',
196+
default => null,
197+
};
212198

213199
if (null !== $comparisonOperator) {
214200
$tag .= sprintf(' comparison_operator="%s"', $comparisonOperator);

src/Enum/AbstractEnum.php

Lines changed: 0 additions & 157 deletions
This file was deleted.

src/Enum/BoolOperator.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,9 @@
33

44
namespace Gdbots\QueryParser\Enum;
55

6-
/**
7-
* @method static BoolOperator OPTIONAL()
8-
* @method static BoolOperator REQUIRED()
9-
* @method static BoolOperator PROHIBITED()
10-
*/
11-
final class BoolOperator extends AbstractEnum
6+
enum BoolOperator: int
127
{
13-
const OPTIONAL = 0;
14-
const REQUIRED = 1;
15-
const PROHIBITED = 2;
8+
case OPTIONAL = 0;
9+
case REQUIRED = 1;
10+
case PROHIBITED = 2;
1611
}

src/Enum/ComparisonOperator.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,11 @@
33

44
namespace Gdbots\QueryParser\Enum;
55

6-
/**
7-
* @method static ComparisonOperator EQ()
8-
* @method static ComparisonOperator GT()
9-
* @method static ComparisonOperator GTE()
10-
* @method static ComparisonOperator LT()
11-
* @method static ComparisonOperator LTE()
12-
*/
13-
final class ComparisonOperator extends AbstractEnum
6+
enum ComparisonOperator: string
147
{
15-
const EQ = 'eq';
16-
const GT = 'gt';
17-
const GTE = 'gte';
18-
const LT = 'lt';
19-
const LTE = 'lte';
8+
case EQ = 'eq';
9+
case GT = 'gt';
10+
case GTE = 'gte';
11+
case LT = 'lt';
12+
case LTE = 'lte';
2013
}

src/Node/Date.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct(
3030
?ComparisonOperator $comparisonOperator = null
3131
) {
3232
parent::__construct($value, $boolOperator, $useBoost, $boost, $useFuzzy, $fuzzy);
33-
$this->comparisonOperator = $comparisonOperator ?: ComparisonOperator::EQ();
33+
$this->comparisonOperator = $comparisonOperator ?: ComparisonOperator::EQ;
3434
}
3535

3636
public static function fromArray(array $data = []): self
@@ -42,13 +42,13 @@ public static function fromArray(array $data = []): self
4242
$fuzzy = (int)($data['fuzzy'] ?? self::DEFAULT_FUZZY);
4343

4444
try {
45-
$boolOperator = isset($data['bool_operator']) ? BoolOperator::create($data['bool_operator']) : null;
45+
$boolOperator = isset($data['bool_operator']) ? BoolOperator::from($data['bool_operator']) : null;
4646
} catch (\Throwable $e) {
4747
$boolOperator = null;
4848
}
4949

5050
try {
51-
$comparisonOperator = isset($data['comparison_operator']) ? ComparisonOperator::create($data['comparison_operator']) : null;
51+
$comparisonOperator = isset($data['comparison_operator']) ? ComparisonOperator::from($data['comparison_operator']) : null;
5252
} catch (\Throwable $e) {
5353
$comparisonOperator = null;
5454
}
@@ -59,17 +59,17 @@ public static function fromArray(array $data = []): self
5959
public function toArray(): array
6060
{
6161
$array = parent::toArray();
62-
if ($this->comparisonOperator->equals(ComparisonOperator::EQ())) {
62+
if ($this->comparisonOperator === ComparisonOperator::EQ) {
6363
return $array;
6464
}
6565

66-
$array['comparison_operator'] = $this->comparisonOperator;
66+
$array['comparison_operator'] = $this->comparisonOperator->value;
6767
return $array;
6868
}
6969

7070
public function useComparisonOperator(): bool
7171
{
72-
return !$this->comparisonOperator->equals(ComparisonOperator::EQ());
72+
return $this->comparisonOperator !== ComparisonOperator::EQ;
7373
}
7474

7575
public function getComparisonOperator(): ComparisonOperator

src/Node/Emoji.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static function fromArray(array $data = []): self
2626
$boost = (float)($data['boost'] ?? self::DEFAULT_BOOST);
2727

2828
try {
29-
$boolOperator = isset($data['bool_operator']) ? BoolOperator::create($data['bool_operator']) : null;
29+
$boolOperator = isset($data['bool_operator']) ? BoolOperator::from($data['bool_operator']) : null;
3030
} catch (\Throwable $e) {
3131
$boolOperator = null;
3232
}

0 commit comments

Comments
 (0)