diff --git a/README.md b/README.md index 2d6779d..af6d908 100644 --- a/README.md +++ b/README.md @@ -225,12 +225,12 @@ 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\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/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/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; 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 = []; @@ -171,9 +171,8 @@ private function generateOrder(Query $query): string foreach ($orderFields as $orderField) { /** @var OrderField $orderField */ $direction = match ($orderField->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 b74ca52..3cbd9b7 100644 --- a/src/Query/OrderField.php +++ b/src/Query/OrderField.php @@ -9,11 +9,6 @@ */ class OrderField { - /** - * Direction constants - */ - const ASCENDING = 0; - const 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..9acc187 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -109,12 +109,14 @@ public function orderBy(array $order): static continue; } - if (!is_int($value)) { + if (is_string($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), }; + } else if (!($value instanceof Direction)) { + throw new InvalidArgumentException('Argument $order contains invalid order direction: ' . $value); } $this->order[] = new OrderField($key, $value); diff --git a/src/Query/WhereGroup.php b/src/Query/WhereGroup.php index 0ea6d60..6b90e09 100644 --- a/src/Query/WhereGroup.php +++ b/src/Query/WhereGroup.php @@ -12,11 +12,6 @@ */ class WhereGroup implements Iterator, Countable { - /** - * Conjunction values - */ - const AND = 0; - const 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 a09cd5c..827df2d 100644 --- a/test/tests/TestDriverTest.php +++ b/test/tests/TestDriverTest.php @@ -4,11 +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; @@ -144,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); @@ -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);