diff --git a/src/Driver/Test/TestTable.php b/src/Driver/Test/TestTable.php index 32cb3e6..8de18c3 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; @@ -163,7 +164,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 18e1761..b9e6dba 100644 --- a/src/Driver/Test/TestTableEntry.php +++ b/src/Driver/Test/TestTableEntry.php @@ -4,6 +4,7 @@ use ArrayAccess; use Aternos\Model\ModelInterface; +use Aternos\Model\Query\Conjunction; use Aternos\Model\Query\SelectField; use Aternos\Model\Query\UpdateField; use Aternos\Model\Query\WhereCondition; @@ -42,14 +43,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/Driver/Test/TestTableEntryGroup.php b/src/Driver/Test/TestTableEntryGroup.php index d0f1764..3dfae7c 100644 --- a/src/Driver/Test/TestTableEntryGroup.php +++ b/src/Driver/Test/TestTableEntryGroup.php @@ -2,6 +2,7 @@ namespace Aternos\Model\Driver\Test; +use Aternos\Model\Query\AggregateFunction; use Aternos\Model\Query\GroupField; use Aternos\Model\Query\SelectField; @@ -88,9 +89,9 @@ public function aggregateAndAlias(?array $fields, bool $collapse = true): static $aggregatedEntry[$key] = $entry[$field->key] ?? null; // init functions - if ($field->function === SelectField::COUNT) { + if ($field->function === AggregateFunction::COUNT) { $aggregatedEntry[$key] = 1; - } elseif ($field->function === SelectField::AVERAGE) { + } elseif ($field->function === AggregateFunction::AVERAGE) { $averageFields[$key] = 1; } } @@ -102,17 +103,17 @@ public function aggregateAndAlias(?array $fields, bool $collapse = true): static $entry[$key] = $entry[$field->key] ?? null; // handle functions - if ($field->function === SelectField::SUM) { + if ($field->function === AggregateFunction::SUM) { $aggregatedEntry[$key] += $entry[$field->key] ?? 0; - } elseif ($field->function === SelectField::COUNT) { + } elseif ($field->function === AggregateFunction::COUNT) { // ++ does not work with ArrayAccess $aggregatedEntry[$key] += 1; - } elseif ($field->function === SelectField::AVERAGE) { + } elseif ($field->function === AggregateFunction::AVERAGE) { $averageFields[$key]++; $aggregatedEntry[$key] += $entry[$field->key] ?? 0; - } elseif ($field->function === SelectField::MIN) { + } elseif ($field->function === AggregateFunction::MIN) { $aggregatedEntry[$key] = min($aggregatedEntry[$key] ?? PHP_INT_MAX, $entry[$field->key] ?? PHP_INT_MAX); - } elseif ($field->function === SelectField::MAX) { + } elseif ($field->function === AggregateFunction::MAX) { $aggregatedEntry[$key] = max($aggregatedEntry[$key] ?? PHP_INT_MIN, $entry[$field->key] ?? PHP_INT_MIN); } } diff --git a/src/GenericModel.php b/src/GenericModel.php index d29cef0..0cf83c8 100644 --- a/src/GenericModel.php +++ b/src/GenericModel.php @@ -18,6 +18,7 @@ use Aternos\Model\Driver\Mysqli\Mysqli; use Aternos\Model\Driver\Redis\Redis; 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\GroupField; @@ -455,7 +456,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/AggregateFunction.php b/src/Query/AggregateFunction.php new file mode 100644 index 0000000..6f537f8 --- /dev/null +++ b/src/Query/AggregateFunction.php @@ -0,0 +1,12 @@ +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 = []; @@ -172,9 +174,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) { @@ -206,26 +207,14 @@ private function generateFields(Query $query): string if ($field->raw === true) { $fieldStrings[] = $field->key; } else { - $fieldString = ""; - if ($field->function !== null) { - switch ($field->function) { - case SelectField::COUNT: - $fieldString .= "COUNT("; - break; - case SelectField::SUM: - $fieldString .= "SUM("; - break; - case SelectField::AVERAGE: - $fieldString .= "AVG("; - break; - case SelectField::MIN: - $fieldString .= "MIN("; - break; - case SelectField::MAX: - $fieldString .= "MAX("; - break; - } - } + $fieldString = match($field->function) { + AggregateFunction::COUNT => "COUNT(", + AggregateFunction::SUM => "SUM(", + AggregateFunction::AVERAGE => "AVG(", + AggregateFunction::MIN => "MIN(", + AggregateFunction::MAX => "MAX(", + default => "" + }; if ($field->key !== "*") { $fieldString .= $this->columnEnclosure . $field->key . $this->columnEnclosure; diff --git a/src/Query/MaxField.php b/src/Query/MaxField.php index 91979c1..2aba7be 100644 --- a/src/Query/MaxField.php +++ b/src/Query/MaxField.php @@ -4,7 +4,7 @@ class MaxField extends SelectField { - public ?int $function = self::MAX; + public ?AggregateFunction $function = AggregateFunction::MAX; /** * MaxField constructor. diff --git a/src/Query/MinField.php b/src/Query/MinField.php index de9b472..13c3454 100644 --- a/src/Query/MinField.php +++ b/src/Query/MinField.php @@ -4,7 +4,7 @@ class MinField extends SelectField { - public ?int $function = self::MIN; + public ?AggregateFunction $function = AggregateFunction::MIN; /** * MinField constructor. diff --git a/src/Query/OrderField.php b/src/Query/OrderField.php index b49eb6d..7871c28 100644 --- a/src/Query/OrderField.php +++ b/src/Query/OrderField.php @@ -9,41 +9,26 @@ */ class OrderField { - /** - * Direction constants - */ - const int ASCENDING = 0; - const int DESCENDING = 1; - - /** - * Field name to order by - * - * @var string|null - */ - public ?string $field = null; + /** @deprecated */ + const Direction ASCENDING = Direction::ASCENDING; + /** @deprecated */ + const Direction DESCENDING = Direction::DESCENDING; /** * @var bool */ public bool $raw = false; - /** - * Order direction - * - * @var int - */ - public int $direction = self::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( + public ?string $field = null, + public Direction $direction = Direction::ASCENDING) { - $this->field = $field; - $this->direction = $direction; } /** @@ -57,10 +42,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 76e275b..51c672d 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -110,10 +110,10 @@ public function orderBy(array $order): static continue; } - if (!is_int($value)) { + if (!$value instanceof Direction) { $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/src/Query/SelectField.php b/src/Query/SelectField.php index 1ddff9a..87fee6a 100644 --- a/src/Query/SelectField.php +++ b/src/Query/SelectField.php @@ -9,21 +9,23 @@ */ class SelectField extends Field { - const int COUNT = 0, - SUM = 1, - AVERAGE = 2, - MIN = 3, - MAX = 4; + /** @deprecated */ + const AggregateFunction COUNT = AggregateFunction::COUNT; + /** @deprecated */ + const AggregateFunction SUM = AggregateFunction::SUM; + /** @deprecated */ + const AggregateFunction AVERAGE = AggregateFunction::AVERAGE; + /** @deprecated */ + const AggregateFunction MIN = AggregateFunction::MIN; + /** @deprecated */ + const AggregateFunction MAX = AggregateFunction::MAX; /** * @var string|null */ public ?string $alias = null; - /** - * @var int|null - */ - public ?int $function = null; + public ?AggregateFunction $function = null; /** * @var bool @@ -32,19 +34,19 @@ class SelectField extends Field /** * @param string|null $alias - * @return SelectField + * @return $this */ - public function setAlias(?string $alias): SelectField + public function setAlias(?string $alias): static { $this->alias = $alias; return $this; } /** - * @param int|null $function - * @return SelectField + * @param AggregateFunction $function + * @return $this */ - public function setFunction(?int $function): SelectField + public function setFunction(AggregateFunction $function): static { $this->function = $function; return $this; @@ -52,9 +54,9 @@ public function setFunction(?int $function): SelectField /** * @param bool $raw - * @return SelectField + * @return $this */ - public function setRaw(bool $raw = true): SelectField + public function setRaw(bool $raw = true): static { $this->raw = $raw; return $this; diff --git a/src/Query/SumField.php b/src/Query/SumField.php index b96283a..b60d018 100644 --- a/src/Query/SumField.php +++ b/src/Query/SumField.php @@ -9,7 +9,7 @@ */ class SumField extends SelectField { - public ?int $function = self::SUM; + public ?AggregateFunction $function = AggregateFunction::SUM; /** * Field constructor. diff --git a/src/Query/WhereGroup.php b/src/Query/WhereGroup.php index 82f860b..f9e8d56 100644 --- a/src/Query/WhereGroup.php +++ b/src/Query/WhereGroup.php @@ -13,11 +13,10 @@ */ class WhereGroup implements Iterator, Countable, Validatable { - /** - * Conjunction values - */ - const int AND = 0; - const int OR = 1; + /** @deprecated */ + const Conjunction AND = Conjunction::AND; + /** @deprecated */ + const Conjunction OR = Conjunction::OR; /** * Multiple WhereGroup or WhereCondition objects @@ -26,11 +25,6 @@ class WhereGroup implements Iterator, Countable, Validatable */ protected array $group = []; - /** - * @var int - */ - public int $conjunction = self:: AND; - /** * Group iterator * @@ -42,12 +36,11 @@ class WhereGroup implements Iterator, Countable, Validatable * WhereGroup constructor. * * @param (WhereCondition|WhereGroup)[] $conditions - * @param int $conjunction + * @param Conjunction $conjunction */ - public function __construct(array $conditions = [], int $conjunction = self:: AND) + public function __construct(array $conditions = [], public Conjunction $conjunction = Conjunction::AND) { $this->group = $conditions; - $this->conjunction = $conjunction; } /** diff --git a/test/tests/SQLTest.php b/test/tests/SQLTest.php index 0eb7599..6e2c39e 100644 --- a/test/tests/SQLTest.php +++ b/test/tests/SQLTest.php @@ -2,6 +2,8 @@ namespace Aternos\Model\Test\Tests; +use Aternos\Model\Query\AggregateFunction; +use Aternos\Model\Query\Conjunction; use Aternos\Model\Query\DeleteQuery; use Aternos\Model\Query\Generator\SQL; use Aternos\Model\Query\Limit; @@ -152,7 +154,7 @@ public function testSelectWhereGroupOR() new WhereGroup([ new WhereCondition('number', 1, '>'), new WhereCondition('number', 0, '<'), - ], WhereGroup::OR), + ], Conjunction::OR), ); $query->modelClassName = TestModel::class; @@ -189,7 +191,7 @@ public function testSelectOrder() public function testSelectCount() { $query = new SelectQuery(fields: [ - (new SelectField('number'))->setFunction(SelectField::COUNT), + (new SelectField('number'))->setFunction(AggregateFunction::COUNT), ]); $query->modelClassName = TestModel::class; @@ -199,7 +201,7 @@ public function testSelectCount() public function testSelectCountStar() { $query = new SelectQuery(fields: [ - (new SelectField('*'))->setFunction(SelectField::COUNT), + (new SelectField('*'))->setFunction(AggregateFunction::COUNT), ]); $query->modelClassName = TestModel::class; @@ -209,7 +211,7 @@ public function testSelectCountStar() public function testSelectSum() { $query = new SelectQuery(fields: [ - (new SelectField('number'))->setFunction(SelectField::SUM), + (new SelectField('number'))->setFunction(AggregateFunction::SUM), ]); $query->modelClassName = TestModel::class; @@ -219,7 +221,7 @@ public function testSelectSum() public function testSelectSumAs() { $query = new SelectQuery(fields: [ - (new SelectField('number'))->setFunction(SelectField::SUM)->setAlias('sum'), + (new SelectField('number'))->setFunction(AggregateFunction::SUM)->setAlias('sum'), ]); $query->modelClassName = TestModel::class; @@ -229,7 +231,7 @@ public function testSelectSumAs() public function testSelectAVG() { $query = new SelectQuery(fields: [ - (new SelectField('number'))->setFunction(SelectField::AVERAGE), + (new SelectField('number'))->setFunction(AggregateFunction::AVERAGE), ]); $query->modelClassName = TestModel::class; @@ -239,7 +241,7 @@ public function testSelectAVG() public function testSelectMin() { $query = new SelectQuery(fields: [ - (new SelectField('number'))->setFunction(SelectField::MIN), + (new SelectField('number'))->setFunction(AggregateFunction::MIN), ]); $query->modelClassName = TestModel::class; @@ -249,7 +251,7 @@ public function testSelectMin() public function testSelectMinAs() { $query = new SelectQuery(fields: [ - (new SelectField('number'))->setFunction(SelectField::MIN)->setAlias('minNumber'), + (new SelectField('number'))->setFunction(AggregateFunction::MIN)->setAlias('minNumber'), ]); $query->modelClassName = TestModel::class; @@ -259,7 +261,7 @@ public function testSelectMinAs() public function testSelectMax() { $query = new SelectQuery(fields: [ - (new SelectField('number'))->setFunction(SelectField::MAX), + (new SelectField('number'))->setFunction(AggregateFunction::MAX), ]); $query->modelClassName = TestModel::class; @@ -269,7 +271,7 @@ public function testSelectMax() public function testSelectMaxAs() { $query = new SelectQuery(fields: [ - (new SelectField('number'))->setFunction(SelectField::MAX)->setAlias('maxNumber'), + (new SelectField('number'))->setFunction(AggregateFunction::MAX)->setAlias('maxNumber'), ]); $query->modelClassName = TestModel::class; diff --git a/test/tests/TestDriverTest.php b/test/tests/TestDriverTest.php index d78c917..598f9bb 100644 --- a/test/tests/TestDriverTest.php +++ b/test/tests/TestDriverTest.php @@ -4,11 +4,13 @@ use Aternos\Model\Driver\DriverRegistry; use Aternos\Model\Driver\Test\TestDriver; +use Aternos\Model\Query\AggregateFunction; +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; @@ -117,7 +119,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); @@ -310,7 +312,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); @@ -380,7 +382,7 @@ public function testSelectAverage(): void $result = TestModel::select(fields: [ (new SelectField("number")) ->setAlias("average") - ->setFunction(SelectField::AVERAGE)]); + ->setFunction(AggregateFunction::AVERAGE)]); $this->assertEquals(4.5, $result[0]->getField("average")); } @@ -449,7 +451,7 @@ public function testSelectGroupAverage(): void $models = TestModel::select(fields: [ (new SelectField("number")) ->setAlias("average") - ->setFunction(SelectField::AVERAGE), + ->setFunction(AggregateFunction::AVERAGE), new SelectField("number"), new SelectField("text"), ], group: ["text"]); @@ -555,7 +557,7 @@ public function testDeleteQuery(): void public function testOrderBeforeLimit(): void { - $result = TestModel::select(order: ["number" => OrderField::DESCENDING], limit: 3); + $result = TestModel::select(order: ["number" => Direction::DESCENDING], limit: 3); $this->assertEquals(9, $result[0]->number); $this->assertEquals(8, $result[1]->number);