From 692ab4ae69dc906737ff53ce17b45a6b431cfcff Mon Sep 17 00:00:00 2001 From: Paul Vogel Date: Wed, 6 Aug 2025 18:39:04 +0200 Subject: [PATCH 1/8] add types to constants --- src/Driver/Cassandra/Cassandra.php | 2 +- src/Driver/Mysqli/Mysqli.php | 2 +- src/Driver/OpenSearch/OpenSearch.php | 2 +- src/Driver/Redis/Redis.php | 2 +- src/Driver/Test/TestDriver.php | 2 +- src/Query/CountField.php | 2 +- src/Query/OrderField.php | 4 ++-- src/Query/WhereGroup.php | 4 ++-- 8 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Driver/Cassandra/Cassandra.php b/src/Driver/Cassandra/Cassandra.php index 039b08d..97f3abe 100644 --- a/src/Driver/Cassandra/Cassandra.php +++ b/src/Driver/Cassandra/Cassandra.php @@ -26,7 +26,7 @@ */ class Cassandra extends Driver implements CRUDAbleInterface, QueryableInterface { - public const ID = "cassandra"; + public const string ID = "cassandra"; protected string $id = self::ID; /** diff --git a/src/Driver/Mysqli/Mysqli.php b/src/Driver/Mysqli/Mysqli.php index 8f36209..bea4b6b 100644 --- a/src/Driver/Mysqli/Mysqli.php +++ b/src/Driver/Mysqli/Mysqli.php @@ -27,7 +27,7 @@ */ class Mysqli extends Driver implements CRUDAbleInterface, CRUDQueryableInterface { - public const ID = "mysqli"; + public const string ID = "mysqli"; protected string $id = self::ID; /** diff --git a/src/Driver/OpenSearch/OpenSearch.php b/src/Driver/OpenSearch/OpenSearch.php index 4ca5672..d746f70 100644 --- a/src/Driver/OpenSearch/OpenSearch.php +++ b/src/Driver/OpenSearch/OpenSearch.php @@ -30,7 +30,7 @@ */ class OpenSearch extends Driver implements CRUDAbleInterface, SearchableInterface { - public const ID = "opensearch"; + public const string ID = "opensearch"; protected string $id = self::ID; /** diff --git a/src/Driver/Redis/Redis.php b/src/Driver/Redis/Redis.php index 85cca3f..6e292f9 100644 --- a/src/Driver/Redis/Redis.php +++ b/src/Driver/Redis/Redis.php @@ -20,7 +20,7 @@ */ class Redis extends Driver implements CRUDAbleInterface, CacheableInterface { - public const ID = "redis"; + public const string ID = "redis"; protected string $id = self::ID; /** diff --git a/src/Driver/Test/TestDriver.php b/src/Driver/Test/TestDriver.php index dde1fdd..794412d 100644 --- a/src/Driver/Test/TestDriver.php +++ b/src/Driver/Test/TestDriver.php @@ -13,7 +13,7 @@ class TestDriver extends Driver implements CRUDAbleInterface, CRUDQueryableInterface { - public const ID = "test"; + public const string ID = "test"; protected string $id = self::ID; /** diff --git a/src/Query/CountField.php b/src/Query/CountField.php index f54e1e6..43f0991 100644 --- a/src/Query/CountField.php +++ b/src/Query/CountField.php @@ -9,7 +9,7 @@ */ class CountField extends SelectField { - public const COUNT_FIELD = "count"; + public const string COUNT_FIELD = "count"; public ?int $function = self::COUNT; public ?string $alias = self::COUNT_FIELD; diff --git a/src/Query/OrderField.php b/src/Query/OrderField.php index b74ca52..af9d334 100644 --- a/src/Query/OrderField.php +++ b/src/Query/OrderField.php @@ -12,8 +12,8 @@ class OrderField /** * Direction constants */ - const ASCENDING = 0; - const DESCENDING = 1; + const int ASCENDING = 0; + const int DESCENDING = 1; /** * Field name to order by diff --git a/src/Query/WhereGroup.php b/src/Query/WhereGroup.php index 0ea6d60..ad3c7f0 100644 --- a/src/Query/WhereGroup.php +++ b/src/Query/WhereGroup.php @@ -15,8 +15,8 @@ class WhereGroup implements Iterator, Countable /** * Conjunction values */ - const AND = 0; - const OR = 1; + const int AND = 0; + const int OR = 1; /** * Multiple WhereGroup or WhereCondition objects From 81bc09a7787bb91ad987e71ec553fe7d70c7087f Mon Sep 17 00:00:00 2001 From: Paul Vogel Date: Thu, 7 Aug 2025 15:31:51 +0200 Subject: [PATCH 2/8] add enum for direction of order --- README.md | 6 +++--- src/Query/Direction.php | 9 +++++++++ src/Query/Generator/SQL.php | 6 +++--- src/Query/OrderField.php | 17 ++++++----------- src/Query/Query.php | 4 ++-- test/tests/TestDriverTest.php | 3 ++- 6 files changed, 25 insertions(+), 20 deletions(-) create mode 100644 src/Query/Direction.php diff --git a/README.md b/README.md index 2d6779d..6ea1618 100644 --- a/README.md +++ b/README.md @@ -228,9 +228,9 @@ User::query(new \Aternos\Model\Query\SelectQuery( ], \Aternos\Model\Query\WhereGroup:: OR) ]), [ - new \Aternos\Model\Query\OrderField("field", \Aternos\Model\Query\OrderField::DESCENDING), - new \Aternos\Model\Query\OrderField("hello", \Aternos\Model\Query\OrderField::ASCENDING), - new \Aternos\Model\Query\OrderField("foo", \Aternos\Model\Query\OrderField::DESCENDING) + new \Aternos\Model\Query\OrderField("field", \Aternos\Model\Query\Direction::DESCENDING), + new \Aternos\Model\Query\OrderField("hello", \Aternos\Model\Query\Direction::ASCENDING), + new \Aternos\Model\Query\OrderField("foo", \Aternos\Model\Query\Direction::DESCENDING) ], [ new \Aternos\Model\Query\SelectField("field"), diff --git a/src/Query/Direction.php b/src/Query/Direction.php new file mode 100644 index 0000000..27aef82 --- /dev/null +++ b/src/Query/Direction.php @@ -0,0 +1,9 @@ +direction) { - OrderField::ASCENDING => "ASC", - OrderField::DESCENDING => "DESC", - default => throw new UnexpectedValueException("Invalid direction: " . $orderField->direction), + Direction::ASCENDING => "ASC", + Direction::DESCENDING => "DESC", }; if ($orderField->raw) { diff --git a/src/Query/OrderField.php b/src/Query/OrderField.php index af9d334..3cbd9b7 100644 --- a/src/Query/OrderField.php +++ b/src/Query/OrderField.php @@ -9,11 +9,6 @@ */ class OrderField { - /** - * Direction constants - */ - const int ASCENDING = 0; - const int DESCENDING = 1; /** * Field name to order by @@ -30,17 +25,17 @@ class OrderField /** * Order direction * - * @var int + * @var Direction */ - public int $direction = self::ASCENDING; + public Direction $direction = Direction::ASCENDING; /** * OrderField constructor. * * @param string|null $field - * @param int $direction + * @param Direction $direction */ - public function __construct(?string $field = null, int $direction = self::ASCENDING) + public function __construct(?string $field = null, Direction $direction = Direction::ASCENDING) { $this->field = $field; $this->direction = $direction; @@ -57,10 +52,10 @@ public function setRaw(bool $raw): OrderField } /** - * @param int $direction + * @param Direction $direction * @return OrderField */ - public function setDirection(int $direction): OrderField + public function setDirection(Direction $direction): OrderField { $this->direction = $direction; return $this; diff --git a/src/Query/Query.php b/src/Query/Query.php index 0023752..660451d 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -111,8 +111,8 @@ public function orderBy(array $order): static if (!is_int($value)) { $value = match (strtoupper($value)) { - "ASCENDING", "ASC" => OrderField::ASCENDING, - "DESCENDING", "DESC" => OrderField::DESCENDING, + "ASCENDING", "ASC" => Direction::ASCENDING, + "DESCENDING", "DESC" => Direction::DESCENDING, default => throw new InvalidArgumentException('Argument $order contains invalid order direction: ' . $value), }; } diff --git a/test/tests/TestDriverTest.php b/test/tests/TestDriverTest.php index a09cd5c..e6d105b 100644 --- a/test/tests/TestDriverTest.php +++ b/test/tests/TestDriverTest.php @@ -6,6 +6,7 @@ use Aternos\Model\Driver\Test\TestDriver; use Aternos\Model\Query\CountField; use Aternos\Model\Query\DeleteQuery; +use Aternos\Model\Query\Direction; use Aternos\Model\Query\MaxField; use Aternos\Model\Query\MinField; use Aternos\Model\Query\OrderField; @@ -337,7 +338,7 @@ public function testSelectLikeEscaping(): void public function testSelectOrder(): void { - $models = TestModel::select(order: ["number" => OrderField::DESCENDING]); + $models = TestModel::select(order: ["number" => Direction::DESCENDING]); $this->assertCount(10, $models); $this->assertEquals("9J", $models[0]->id); $this->assertEquals("J", $models[0]->text); From 044100b011f707c61a67795b24c6621812bbe9a5 Mon Sep 17 00:00:00 2001 From: Paul Vogel Date: Thu, 7 Aug 2025 15:38:00 +0200 Subject: [PATCH 3/8] add enum for conjunction of WhereGroup --- README.md | 2 +- src/Driver/Test/TestTableEntry.php | 8 ++++---- src/GenericModel.php | 8 ++++---- src/Query/Conjunction.php | 9 +++++++++ src/Query/Generator/SQL.php | 9 ++++----- src/Query/WhereGroup.php | 13 ++++--------- test/tests/SQLTest.php | 3 ++- test/tests/TestDriverTest.php | 4 ++-- 8 files changed, 30 insertions(+), 26 deletions(-) create mode 100644 src/Query/Conjunction.php diff --git a/README.md b/README.md index 6ea1618..af6d908 100644 --- a/README.md +++ b/README.md @@ -225,7 +225,7 @@ User::query(new \Aternos\Model\Query\SelectQuery( new \Aternos\Model\Query\WhereGroup([ new \Aternos\Model\Query\WhereCondition("hello", "world"), new \Aternos\Model\Query\WhereCondition("foo", "bar") - ], \Aternos\Model\Query\WhereGroup:: OR) + ], \Aternos\Model\Query\Conjunction::OR) ]), [ new \Aternos\Model\Query\OrderField("field", \Aternos\Model\Query\Direction::DESCENDING), diff --git a/src/Driver/Test/TestTableEntry.php b/src/Driver/Test/TestTableEntry.php index e43b604..99311af 100644 --- a/src/Driver/Test/TestTableEntry.php +++ b/src/Driver/Test/TestTableEntry.php @@ -3,7 +3,7 @@ namespace Aternos\Model\Driver\Test; use Aternos\Model\ModelInterface; -use Aternos\Model\Query\Field; +use Aternos\Model\Query\Conjunction; use Aternos\Model\Query\SelectField; use Aternos\Model\Query\UpdateField; use Aternos\Model\Query\WhereCondition; @@ -42,14 +42,14 @@ public function matchesWhereGroup(?WhereGroup $where): bool } else if ($condition instanceof WhereCondition) { $matches = $this->matchesWhereCondition($condition); } - if ($where->conjunction === WhereGroup::AND && !$matches) { + if ($where->conjunction === Conjunction::AND && !$matches) { return false; } - if ($where->conjunction === WhereGroup::OR && $matches) { + if ($where->conjunction === Conjunction::OR && $matches) { return true; } } - return $where->conjunction === WhereGroup::AND; + return $where->conjunction === Conjunction::AND; } /** diff --git a/src/GenericModel.php b/src/GenericModel.php index 30c3b11..9f55f5c 100644 --- a/src/GenericModel.php +++ b/src/GenericModel.php @@ -18,7 +18,8 @@ }; use Aternos\Model\Driver\Mysqli\Mysqli; use Aternos\Model\Driver\Redis\Redis; -use Aternos\Model\Query\{CountField, +use Aternos\Model\Query\{Conjunction, + CountField, DeleteQuery, GroupField, Limit, @@ -28,8 +29,7 @@ SelectQuery, UpdateQuery, WhereCondition, - WhereGroup -}; + WhereGroup}; use Aternos\Model\Search\Search; use Aternos\Model\Search\SearchResult; use BadMethodCallException; @@ -441,7 +441,7 @@ public static function query(Query $query): QueryResult $query->modelClassName = static::class; if (static::$filters !== null && count(static::$filters) > 0) { - $wrappedWhereGroup = new WhereGroup(conjunction: WhereGroup::AND); + $wrappedWhereGroup = new WhereGroup(conjunction: Conjunction::AND); foreach (static::$filters as $key => $value) { $wrappedWhereGroup->add(new WhereCondition($key, $value)); } diff --git a/src/Query/Conjunction.php b/src/Query/Conjunction.php new file mode 100644 index 0000000..976dcef --- /dev/null +++ b/src/Query/Conjunction.php @@ -0,0 +1,9 @@ +operator . " " . $value; } elseif ($where instanceof WhereGroup) { $conjunction = match ($where->conjunction) { - WhereGroup:: AND => " AND ", - WhereGroup:: OR => " OR ", - default => throw new UnexpectedValueException("Invalid conjunction: " . $where->conjunction), + Conjunction::AND => " AND ", + Conjunction::OR => " OR ", }; $whereStrings = []; diff --git a/src/Query/WhereGroup.php b/src/Query/WhereGroup.php index ad3c7f0..6b90e09 100644 --- a/src/Query/WhereGroup.php +++ b/src/Query/WhereGroup.php @@ -12,11 +12,6 @@ */ class WhereGroup implements Iterator, Countable { - /** - * Conjunction values - */ - const int AND = 0; - const int OR = 1; /** * Multiple WhereGroup or WhereCondition objects @@ -26,9 +21,9 @@ class WhereGroup implements Iterator, Countable protected array $group = []; /** - * @var int + * @var Conjunction */ - public int $conjunction = self:: AND; + public Conjunction $conjunction = Conjunction::AND; /** * Group iterator @@ -41,9 +36,9 @@ class WhereGroup implements Iterator, Countable * WhereGroup constructor. * * @param array $conditions - * @param int $conjunction + * @param Conjunction $conjunction */ - public function __construct(array $conditions = [], int $conjunction = self:: AND) + public function __construct(array $conditions = [], Conjunction $conjunction = Conjunction::AND) { $this->group = $conditions; $this->conjunction = $conjunction; diff --git a/test/tests/SQLTest.php b/test/tests/SQLTest.php index 647b627..acb8de3 100644 --- a/test/tests/SQLTest.php +++ b/test/tests/SQLTest.php @@ -2,6 +2,7 @@ namespace Aternos\Model\Test\Tests; +use Aternos\Model\Query\Conjunction; use Aternos\Model\Query\DeleteQuery; use Aternos\Model\Query\Generator\SQL; use Aternos\Model\Query\Limit; @@ -142,7 +143,7 @@ public function testSelectWhereGroupOR() new WhereGroup([ new WhereCondition('number', 1, '>'), new WhereCondition('number', 0, '<'), - ], WhereGroup::OR), + ], Conjunction::OR), ); $query->modelClassName = TestModel::class; diff --git a/test/tests/TestDriverTest.php b/test/tests/TestDriverTest.php index e6d105b..827df2d 100644 --- a/test/tests/TestDriverTest.php +++ b/test/tests/TestDriverTest.php @@ -4,12 +4,12 @@ use Aternos\Model\Driver\DriverRegistry; use Aternos\Model\Driver\Test\TestDriver; +use Aternos\Model\Query\Conjunction; use Aternos\Model\Query\CountField; use Aternos\Model\Query\DeleteQuery; use Aternos\Model\Query\Direction; use Aternos\Model\Query\MaxField; use Aternos\Model\Query\MinField; -use Aternos\Model\Query\OrderField; use Aternos\Model\Query\SelectField; use Aternos\Model\Query\SumField; use Aternos\Model\Query\WhereCondition; @@ -145,7 +145,7 @@ public function testSelectWhereOr(): void $models = TestModel::select(new WhereGroup([ new WhereCondition("number", 1), new WhereCondition("number", 2) - ], WhereGroup::OR)); + ], Conjunction::OR)); $this->assertCount(2, $models); $this->assertEquals("1B", $models[0]->id); $this->assertEquals("B", $models[0]->text); From 07a0d0f81270b2c10db52562aca37db37faed439 Mon Sep 17 00:00:00 2001 From: Paul Vogel Date: Thu, 7 Aug 2025 15:40:31 +0200 Subject: [PATCH 4/8] check for is_string instead of !is_int --- src/Query/Query.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Query/Query.php b/src/Query/Query.php index 660451d..29f09a0 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -109,7 +109,7 @@ public function orderBy(array $order): static continue; } - if (!is_int($value)) { + if (is_string($value)) { $value = match (strtoupper($value)) { "ASCENDING", "ASC" => Direction::ASCENDING, "DESCENDING", "DESC" => Direction::DESCENDING, From a9d901d83697ca87ffb1be296e91c01138fa314d Mon Sep 17 00:00:00 2001 From: Paul Vogel Date: Thu, 7 Aug 2025 15:42:33 +0200 Subject: [PATCH 5/8] throw InvalidArgumentException if value in orderBy is not OrderField and not string --- src/Query/Query.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Query/Query.php b/src/Query/Query.php index 29f09a0..28fc19b 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -115,6 +115,8 @@ public function orderBy(array $order): static "DESCENDING", "DESC" => Direction::DESCENDING, default => throw new InvalidArgumentException('Argument $order contains invalid order direction: ' . $value), }; + } else { + throw new InvalidArgumentException('Argument $order contains invalid order direction: ' . $value); } $this->order[] = new OrderField($key, $value); From e3ad1b144fb6ad8b1ab4b7dc4e21c8ba653b0fd3 Mon Sep 17 00:00:00 2001 From: Paul Vogel Date: Thu, 7 Aug 2025 15:45:39 +0200 Subject: [PATCH 6/8] use $value as direction it $value is instance of Direction --- src/Query/Query.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Query/Query.php b/src/Query/Query.php index 28fc19b..47a8760 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -109,6 +109,11 @@ public function orderBy(array $order): static continue; } + if ($value instanceof Direction) { + $this->order[] = new OrderField($key, $value); + continue; + } + if (is_string($value)) { $value = match (strtoupper($value)) { "ASCENDING", "ASC" => Direction::ASCENDING, From 561b59f9ddd6bdc9d7fde1a63deb104ae1334c99 Mon Sep 17 00:00:00 2001 From: Paul Vogel Date: Thu, 7 Aug 2025 15:48:43 +0200 Subject: [PATCH 7/8] fix: use Direction::ASCENDING instead of OrderField::ASCENDING --- src/Driver/Test/TestTable.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Driver/Test/TestTable.php b/src/Driver/Test/TestTable.php index a60bbb6..b9bc04f 100644 --- a/src/Driver/Test/TestTable.php +++ b/src/Driver/Test/TestTable.php @@ -5,6 +5,7 @@ use Aternos\Model\ModelInterface; use Aternos\Model\ModelRegistry; use Aternos\Model\Query\DeleteQuery; +use Aternos\Model\Query\Direction; use Aternos\Model\Query\GroupField; use Aternos\Model\Query\OrderField; use Aternos\Model\Query\Query; @@ -167,7 +168,7 @@ protected function compareEntries(TestTableEntry $a, TestTableEntry $b, array $o if ($aValue === $bValue) { continue; } - if ($orderField->direction === OrderField::ASCENDING) { + if ($orderField->direction === Direction::ASCENDING) { return $aValue > $bValue ? 1 : -1; } else { return $aValue < $bValue ? 1 : -1; From d131c83be319a0a90f783711cb1ea7c7b219afa5 Mon Sep 17 00:00:00 2001 From: Paul Vogel Date: Fri, 8 Aug 2025 09:44:19 +0200 Subject: [PATCH 8/8] use else if --- src/Query/Query.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Query/Query.php b/src/Query/Query.php index 47a8760..9acc187 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -109,18 +109,13 @@ public function orderBy(array $order): static continue; } - if ($value instanceof Direction) { - $this->order[] = new OrderField($key, $value); - continue; - } - if (is_string($value)) { $value = match (strtoupper($value)) { "ASCENDING", "ASC" => Direction::ASCENDING, "DESCENDING", "DESC" => Direction::DESCENDING, default => throw new InvalidArgumentException('Argument $order contains invalid order direction: ' . $value), }; - } else { + } else if (!($value instanceof Direction)) { throw new InvalidArgumentException('Argument $order contains invalid order direction: ' . $value); }