Skip to content

Commit

Permalink
Remove conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
MGatner committed Aug 26, 2022
1 parent 12838fc commit d29d1dc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 53 deletions.
13 changes: 12 additions & 1 deletion src/Conditions/Condition.php → src/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<Entity> $class
*/
public static function fromIdentity(string $class, int|string $id): self
{
return new self($class::IDENTIFIER, '=', $id);
}

/**
* @param array<int, scalar|null>|scalar|null $value
*/
Expand Down
37 changes: 0 additions & 37 deletions src/Conditions/Conditions.php

This file was deleted.

22 changes: 16 additions & 6 deletions src/Persistence/SQLDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Config\Database;
use Generator;
use RuntimeException;
use Tatter\Repositories\Conditions\Conditions;
use Tatter\Repositories\Condition;

/**
* SQL Database Persistence Class
Expand All @@ -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)
Expand All @@ -49,9 +51,11 @@ public function first(Conditions $conditions): ?array
/**
* Yields matching results from persistence.
*
* @param Condition[] $conditions
*
* @returns iterable<array>
*/
public function get(Conditions $conditions): Generator
public function get(array $conditions): Generator
{
$result = $this->builderFromConditions($conditions)->get();
if ($result === false) {
Expand All @@ -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;

Expand Down
6 changes: 4 additions & 2 deletions src/Repository/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Tatter\Repositories\Repository;

use Tatter\Repositories\Conditions\Conditions;
use Tatter\Repositories\Condition;

/**
* @template T of Entity
Expand All @@ -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<T>
*/
public function list(?Conditions $conditions = null): iterable;
public function list(array $conditions = []): iterable;

/**
* Adds or updates an item in persistence.
Expand Down
14 changes: 7 additions & 7 deletions src/Repository/TableRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Tatter\Repositories\Repository;

use Tatter\Repositories\Conditions\Conditions;
use Tatter\Repositories\Condition;
use Tatter\Repositories\Persistence\SQLDatabase;

/**
Expand Down Expand Up @@ -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;
Expand All @@ -60,12 +60,12 @@ public function get($id): ?Entity
/**
* Gets all items, optionally filtering on the set of criteria.
*
* @param Condition[] $conditions
*
* @returns iterable<T>
*/
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);
}
Expand All @@ -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());
}

Expand All @@ -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);
}
}

0 comments on commit d29d1dc

Please sign in to comment.