Skip to content

Commit 044100b

Browse files
committed
add enum for conjunction of WhereGroup
1 parent 81bc09a commit 044100b

File tree

8 files changed

+30
-26
lines changed

8 files changed

+30
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ User::query(new \Aternos\Model\Query\SelectQuery(
225225
new \Aternos\Model\Query\WhereGroup([
226226
new \Aternos\Model\Query\WhereCondition("hello", "world"),
227227
new \Aternos\Model\Query\WhereCondition("foo", "bar")
228-
], \Aternos\Model\Query\WhereGroup:: OR)
228+
], \Aternos\Model\Query\Conjunction::OR)
229229
]),
230230
[
231231
new \Aternos\Model\Query\OrderField("field", \Aternos\Model\Query\Direction::DESCENDING),

src/Driver/Test/TestTableEntry.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Aternos\Model\Driver\Test;
44

55
use Aternos\Model\ModelInterface;
6-
use Aternos\Model\Query\Field;
6+
use Aternos\Model\Query\Conjunction;
77
use Aternos\Model\Query\SelectField;
88
use Aternos\Model\Query\UpdateField;
99
use Aternos\Model\Query\WhereCondition;
@@ -42,14 +42,14 @@ public function matchesWhereGroup(?WhereGroup $where): bool
4242
} else if ($condition instanceof WhereCondition) {
4343
$matches = $this->matchesWhereCondition($condition);
4444
}
45-
if ($where->conjunction === WhereGroup::AND && !$matches) {
45+
if ($where->conjunction === Conjunction::AND && !$matches) {
4646
return false;
4747
}
48-
if ($where->conjunction === WhereGroup::OR && $matches) {
48+
if ($where->conjunction === Conjunction::OR && $matches) {
4949
return true;
5050
}
5151
}
52-
return $where->conjunction === WhereGroup::AND;
52+
return $where->conjunction === Conjunction::AND;
5353
}
5454

5555
/**

src/GenericModel.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
};
1919
use Aternos\Model\Driver\Mysqli\Mysqli;
2020
use Aternos\Model\Driver\Redis\Redis;
21-
use Aternos\Model\Query\{CountField,
21+
use Aternos\Model\Query\{Conjunction,
22+
CountField,
2223
DeleteQuery,
2324
GroupField,
2425
Limit,
@@ -28,8 +29,7 @@
2829
SelectQuery,
2930
UpdateQuery,
3031
WhereCondition,
31-
WhereGroup
32-
};
32+
WhereGroup};
3333
use Aternos\Model\Search\Search;
3434
use Aternos\Model\Search\SearchResult;
3535
use BadMethodCallException;
@@ -441,7 +441,7 @@ public static function query(Query $query): QueryResult
441441
$query->modelClassName = static::class;
442442

443443
if (static::$filters !== null && count(static::$filters) > 0) {
444-
$wrappedWhereGroup = new WhereGroup(conjunction: WhereGroup::AND);
444+
$wrappedWhereGroup = new WhereGroup(conjunction: Conjunction::AND);
445445
foreach (static::$filters as $key => $value) {
446446
$wrappedWhereGroup->add(new WhereCondition($key, $value));
447447
}

src/Query/Conjunction.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Aternos\Model\Query;
4+
5+
enum Conjunction
6+
{
7+
case AND;
8+
case OR;
9+
}

src/Query/Generator/SQL.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace Aternos\Model\Query\Generator;
44

5-
use Aternos\Model\Query\{DeleteQuery,
5+
use Aternos\Model\Query\{Conjunction,
6+
DeleteQuery,
67
Direction,
78
OrderField,
89
Query,
@@ -12,7 +13,6 @@
1213
UpdateQuery,
1314
WhereCondition,
1415
WhereGroup};
15-
use UnexpectedValueException;
1616

1717
/**
1818
* Class SQL
@@ -140,9 +140,8 @@ private function generateWhere(Query $query, WhereCondition|WhereGroup|null $whe
140140
return $field . " " . $where->operator . " " . $value;
141141
} elseif ($where instanceof WhereGroup) {
142142
$conjunction = match ($where->conjunction) {
143-
WhereGroup:: AND => " AND ",
144-
WhereGroup:: OR => " OR ",
145-
default => throw new UnexpectedValueException("Invalid conjunction: " . $where->conjunction),
143+
Conjunction::AND => " AND ",
144+
Conjunction::OR => " OR ",
146145
};
147146

148147
$whereStrings = [];

src/Query/WhereGroup.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@
1212
*/
1313
class WhereGroup implements Iterator, Countable
1414
{
15-
/**
16-
* Conjunction values
17-
*/
18-
const int AND = 0;
19-
const int OR = 1;
2015

2116
/**
2217
* Multiple WhereGroup or WhereCondition objects
@@ -26,9 +21,9 @@ class WhereGroup implements Iterator, Countable
2621
protected array $group = [];
2722

2823
/**
29-
* @var int
24+
* @var Conjunction
3025
*/
31-
public int $conjunction = self:: AND;
26+
public Conjunction $conjunction = Conjunction::AND;
3227

3328
/**
3429
* Group iterator
@@ -41,9 +36,9 @@ class WhereGroup implements Iterator, Countable
4136
* WhereGroup constructor.
4237
*
4338
* @param array $conditions
44-
* @param int $conjunction
39+
* @param Conjunction $conjunction
4540
*/
46-
public function __construct(array $conditions = [], int $conjunction = self:: AND)
41+
public function __construct(array $conditions = [], Conjunction $conjunction = Conjunction::AND)
4742
{
4843
$this->group = $conditions;
4944
$this->conjunction = $conjunction;

test/tests/SQLTest.php

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

33
namespace Aternos\Model\Test\Tests;
44

5+
use Aternos\Model\Query\Conjunction;
56
use Aternos\Model\Query\DeleteQuery;
67
use Aternos\Model\Query\Generator\SQL;
78
use Aternos\Model\Query\Limit;
@@ -142,7 +143,7 @@ public function testSelectWhereGroupOR()
142143
new WhereGroup([
143144
new WhereCondition('number', 1, '>'),
144145
new WhereCondition('number', 0, '<'),
145-
], WhereGroup::OR),
146+
], Conjunction::OR),
146147
);
147148
$query->modelClassName = TestModel::class;
148149

test/tests/TestDriverTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
use Aternos\Model\Driver\DriverRegistry;
66
use Aternos\Model\Driver\Test\TestDriver;
7+
use Aternos\Model\Query\Conjunction;
78
use Aternos\Model\Query\CountField;
89
use Aternos\Model\Query\DeleteQuery;
910
use Aternos\Model\Query\Direction;
1011
use Aternos\Model\Query\MaxField;
1112
use Aternos\Model\Query\MinField;
12-
use Aternos\Model\Query\OrderField;
1313
use Aternos\Model\Query\SelectField;
1414
use Aternos\Model\Query\SumField;
1515
use Aternos\Model\Query\WhereCondition;
@@ -145,7 +145,7 @@ public function testSelectWhereOr(): void
145145
$models = TestModel::select(new WhereGroup([
146146
new WhereCondition("number", 1),
147147
new WhereCondition("number", 2)
148-
], WhereGroup::OR));
148+
], Conjunction::OR));
149149
$this->assertCount(2, $models);
150150
$this->assertEquals("1B", $models[0]->id);
151151
$this->assertEquals("B", $models[0]->text);

0 commit comments

Comments
 (0)