diff --git a/src/Conditions/Condition.php b/src/Condition.php similarity index 83% rename from src/Conditions/Condition.php rename to src/Condition.php index 45a8a85..b7246ea 100644 --- a/src/Conditions/Condition.php +++ b/src/Condition.php @@ -2,9 +2,10 @@ declare(strict_types=1); -namespace Tatter\Repositories\Conditions; +namespace Tatter\Repositories; use InvalidArgumentException; +use Tatter\Repositories\Repository\Entity; final class Condition { @@ -40,6 +41,16 @@ public static function fromString(string $input): self return new self($field, $operator, $value); } + /** + * Returns a condition to match an Entity by the given identifier. + * + * @param class-string $class + */ + public static function fromIdentity(string $class, int|string $id): self + { + return new self($class::IDENTIFIER, '=', $id); + } + /** * @param array|scalar|null $value */ diff --git a/src/Conditions/Conditions.php b/src/Conditions/Conditions.php deleted file mode 100644 index e76f427..0000000 --- a/src/Conditions/Conditions.php +++ /dev/null @@ -1,37 +0,0 @@ - $class - */ - public static function fromIdentity(string $class, int|string $id): self - { - $condition = new Condition($class::IDENTIFIER, '=', $id); - - return new self($condition); - } - - public function __construct(Condition ...$conditions) - { - $this->conditions = $conditions; - } - - public function getIterator(): ArrayIterator - { - return new ArrayIterator($this->conditions); - } -} diff --git a/src/Persistence/SQLDatabase.php b/src/Persistence/SQLDatabase.php index d195aff..f1060f4 100644 --- a/src/Persistence/SQLDatabase.php +++ b/src/Persistence/SQLDatabase.php @@ -8,7 +8,7 @@ use Config\Database; use Generator; use RuntimeException; -use Tatter\Repositories\Conditions\Conditions; +use Tatter\Repositories\Condition; /** * SQL Database Persistence Class @@ -33,8 +33,10 @@ private function __construct(private BaseBuilder $builder) /** * Gets the first row matching the conditions. + * + * @param Condition[] $conditions */ - public function first(Conditions $conditions): ?array + public function first(array $conditions): ?array { $result = $this->builderFromConditions($conditions) ->limit(1) @@ -49,9 +51,11 @@ public function first(Conditions $conditions): ?array /** * Yields matching results from persistence. * + * @param Condition[] $conditions + * * @returns iterable */ - public function get(Conditions $conditions): Generator + public function get(array $conditions): Generator { $result = $this->builderFromConditions($conditions)->get(); if ($result === false) { @@ -73,24 +77,30 @@ public function insert(array $data): void /** * Updates rows in the database. + * + * @param Condition[] $conditions */ - public function update(Conditions $conditions, array $data): void + public function update(array $conditions, array $data): void { $this->builderFromConditions($conditions)->update($data); } /** * Deletes matching items from the database. + * + * @param Condition[] $conditions */ - public function delete(Conditions $conditions): void + public function delete(array $conditions): void { $this->builderFromConditions($conditions)->delete(); } /** * Preps a Builder with "where" conditions. + * + * @param Condition[] $conditions */ - private function builderFromConditions(Conditions $conditions): BaseBuilder + private function builderFromConditions(array $conditions): BaseBuilder { $builder = clone $this->builder; diff --git a/src/Repository/RepositoryInterface.php b/src/Repository/RepositoryInterface.php index 4203a2d..ceba435 100644 --- a/src/Repository/RepositoryInterface.php +++ b/src/Repository/RepositoryInterface.php @@ -4,7 +4,7 @@ namespace Tatter\Repositories\Repository; -use Tatter\Repositories\Conditions\Conditions; +use Tatter\Repositories\Condition; /** * @template T of Entity @@ -21,9 +21,11 @@ public function get(int|string $id): ?Entity; /** * Gets all items, optionally filtering on the set of criteria. * + * @param Condition[] $conditions + * * @return iterable */ - public function list(?Conditions $conditions = null): iterable; + public function list(array $conditions = []): iterable; /** * Adds or updates an item in persistence. diff --git a/src/Repository/TableRepository.php b/src/Repository/TableRepository.php index 6582bdf..c2653a4 100644 --- a/src/Repository/TableRepository.php +++ b/src/Repository/TableRepository.php @@ -4,7 +4,7 @@ namespace Tatter\Repositories\Repository; -use Tatter\Repositories\Conditions\Conditions; +use Tatter\Repositories\Condition; use Tatter\Repositories\Persistence\SQLDatabase; /** @@ -46,7 +46,7 @@ public function initialize(): void */ public function get($id): ?Entity { - $conditions = Conditions::fromIdentity(static::ENTITY, $id); + $conditions = [Condition::fromIdentity(static::ENTITY, $id)]; $result = $this->database->first($conditions); if ($result === null) { return null; @@ -60,12 +60,12 @@ public function get($id): ?Entity /** * Gets all items, optionally filtering on the set of criteria. * + * @param Condition[] $conditions + * * @returns iterable */ - public function list(?Conditions $conditions = null): iterable + public function list(array $conditions = []): iterable { - $conditions ??= new Conditions(); - foreach ($this->database->get($conditions) as $array) { yield Entity::fromArray($array); } @@ -86,7 +86,7 @@ public function save(Entity $entity): void return; } - $conditions = Conditions::fromIdentity($entity::class, $id); + $conditions = [Condition::fromIdentity($entity::class, $id)]; $this->database->update($conditions, $entity->toArray()); } @@ -103,7 +103,7 @@ public function remove(Entity $entity): void return; } - $conditions = Conditions::fromIdentity($entity::class, $id); + $conditions = [Condition::fromIdentity($entity::class, $id)]; $this->database->delete($conditions); } }