Skip to content

Introduce Direction enum for OrderField + Introduce Conjunction enum for WhereGroup #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/Cassandra/Cassandra.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/Mysqli/Mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/OpenSearch/OpenSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/Redis/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Driver/Test/TestDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Driver/Test/TestTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/Driver/Test/TestTableEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/GenericModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -28,8 +29,7 @@
SelectQuery,
UpdateQuery,
WhereCondition,
WhereGroup
};
WhereGroup};
use Aternos\Model\Search\Search;
use Aternos\Model\Search\SearchResult;
use BadMethodCallException;
Expand Down Expand Up @@ -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));
}
Expand Down
9 changes: 9 additions & 0 deletions src/Query/Conjunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Aternos\Model\Query;

enum Conjunction
{
case AND;
case OR;
}
2 changes: 1 addition & 1 deletion src/Query/CountField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions src/Query/Direction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Aternos\Model\Query;

enum Direction
{
case ASCENDING;
case DESCENDING;
}
15 changes: 7 additions & 8 deletions src/Query/Generator/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Aternos\Model\Query\Generator;

use Aternos\Model\Query\{DeleteQuery,
use Aternos\Model\Query\{Conjunction,
DeleteQuery,
Direction,
OrderField,
Query,
SelectField,
Expand All @@ -11,7 +13,6 @@
UpdateQuery,
WhereCondition,
WhereGroup};
use UnexpectedValueException;

/**
* Class SQL
Expand Down Expand Up @@ -139,9 +140,8 @@ private function generateWhere(Query $query, WhereCondition|WhereGroup|null $whe
return $field . " " . $where->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 = [];
Expand Down Expand Up @@ -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) {
Expand Down
17 changes: 6 additions & 11 deletions src/Query/OrderField.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
*/
class OrderField
{
/**
* Direction constants
*/
const ASCENDING = 0;
const DESCENDING = 1;

/**
* Field name to order by
Expand All @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions src/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 4 additions & 9 deletions src/Query/WhereGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
*/
class WhereGroup implements Iterator, Countable
{
/**
* Conjunction values
*/
const AND = 0;
const OR = 1;

/**
* Multiple WhereGroup or WhereCondition objects
Expand All @@ -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
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion test/tests/SQLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
7 changes: 4 additions & 3 deletions test/tests/TestDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Loading