Skip to content

Commit

Permalink
Tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Nov 2, 2024
1 parent dfd8721 commit 80bf3c6
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 117 deletions.
2 changes: 1 addition & 1 deletion lib/ActiveRecord/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

final readonly class Config
{
public const DEFAULT_CONNECTION_ID = 'primary';
public const string DEFAULT_CONNECTION_ID = 'primary';

/**
* @param array{
Expand Down
1 change: 1 addition & 0 deletions lib/ActiveRecord/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ public function quote(string $string, int $type = PDO::PARAM_STR): string
{
$quoted = $this->pdo->quote($string, $type);

// @phpstan-ignore-next-line
if ($quoted === false) {
throw new InvalidArgumentException("Unsupported quote type: $type");
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ActiveRecord/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private function resolve_parent(ModelProvider $models): ?Model
* @throws RecordNotFound when the record, or one or more records of the records
* set couldn't be found.
*/
public function find(int|string ... $keys)
public function find(int|string ...$keys)
{
if (count($keys) == 1) {
$key = current($keys);
Expand Down
113 changes: 33 additions & 80 deletions lib/ActiveRecord/RelationCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,110 +11,63 @@

namespace ICanBoogie\ActiveRecord;

use ArrayAccess;
use Closure;
use ICanBoogie\ActiveRecord;
use ICanBoogie\ActiveRecord\Config\Association;

use function is_string;

/**
* Relation collection of a model.
*
* @implements ArrayAccess<string, Relation>
* Where _key_ is a getter name e.g. 'comments'
*/
readonly class RelationCollection
{
/**
* Relations.
*
* @var array<string, Relation>
* Where _key_ is a getter name e.g. 'comments'
*/
private array $relations;

public function __construct(public Model $model, ?Association $association)
{
if (!$association) {
return;
}

public function __construct(
public Model $model,
?Association $association
) {
$relations = [];

foreach ($association->belongs_to as $r) {
$as = $r->as;
$relations[$as] = $this->belongs_to(
related: $r->associate,
local_key: $r->local_key,
foreign_key: $r->foreign_key,
as: $as,
);
}
if ($association) {
foreach ($association->belongs_to as $r) {
$as = $r->as;
$relations[$as] = new BelongsToRelation(
owner: $this->model,
related: $r->associate,
local_key: $r->local_key,
foreign_key: $r->foreign_key,
as: $as,
);
}

if ($association->has_many) {
assert(is_string($this->model->primary));
}

foreach ($association->has_many as $r) {
$as = $r->as;
$relations[$as] = $this->has_many(
related: $r->associate,
foreign_key: $r->foreign_key,
as: $as,
through: $r->through,
);
foreach ($association->has_many as $r) {
$as = $r->as;
$relations[$as] = new HasManyRelation(
owner: $this->model,
related: $r->associate,
foreign_key: $r->foreign_key,
as: $as,
through: $r->through,
);
}
}

$this->relations = $relations;
}

/**
* Adds a {@link BelongsToRelation} relation.
*
* @param class-string<ActiveRecord> $related
* @param non-empty-string $local_key
* @param non-empty-string $foreign_key
* @param non-empty-string $as
*/
private function belongs_to(
string $related,
string $local_key,
string $foreign_key,
string $as,
): BelongsToRelation {
return new BelongsToRelation(
owner: $this->model,
related: $related,
local_key: $local_key,
foreign_key: $foreign_key,
as: $as,
);
}

/**
* Adds a {@link HasManyRelation} relation.
*
* @param class-string<ActiveRecord> $related
* @param non-empty-string $foreign_key
* @param non-empty-string $as
* @param class-string<ActiveRecord>|null $through
*/
private function has_many(
string $related,
string $foreign_key,
string $as,
?string $through = null,
): HasManyRelation {
assert(is_string($this->model->primary));

return new HasManyRelation(
owner: $this->model,
related: $related,
foreign_key: $foreign_key,
as: $as,
through: $through,
);
}

/**
* Whether a relation exists.
*
* @param non-empty-string $relation_name
*/
public function has(string $relation_name): bool
{
Expand All @@ -124,6 +77,8 @@ public function has(string $relation_name): bool
/**
* Returns an existing relation.
*
* @param non-empty-string $relation_name
*
* @throws RelationNotDefined if the relation doesn't exist.
*/
public function get(string $relation_name): ?Relation
Expand All @@ -134,8 +89,6 @@ public function get(string $relation_name): ?Relation

/**
* @param (Closure(Relation, string $as): bool) $predicate
*
* @return Relation|null
*/
public function find(Closure $predicate): ?Relation
{
Expand Down
34 changes: 0 additions & 34 deletions lib/ActiveRecord/ScopeNotDefined.php

This file was deleted.

2 changes: 2 additions & 0 deletions lib/ActiveRecord/StatementInvocationFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public function __construct(

/**
* Formats a message from a statement and its arguments.
*
* @param mixed[] $args
*/
private function format_message(Statement $statement, array $args): string
{
Expand Down
7 changes: 6 additions & 1 deletion lib/ActiveRecord/StaticConnectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static function get(): ?callable
}

/**
* Undefines the {@link ConnectionProvider} factory.
* Unsets the {@link ConnectionProvider} factory.
*/
public static function unset(): void
{
Expand All @@ -76,4 +76,9 @@ public static function connection_for_id(string $id): Connection

return (self::$provider ??= $factory())->connection_for_id($id);
}

/** @codeCoverageIgnore */
private function __construct()
{
}
}
2 changes: 2 additions & 0 deletions lib/ActiveRecord/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,8 @@ public function update(array $values, int|string $key): void
$values[] = $key;

$this->execute($query, $values);

return true;
}

/**
Expand Down
37 changes: 37 additions & 0 deletions tests/StaticConnectionProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Test\ICanBoogie;

use ICanBoogie\ActiveRecord\Config\ConnectionDefinition;
use ICanBoogie\ActiveRecord\ConnectionCollection;
use ICanBoogie\ActiveRecord\StaticConnectionProvider;
use PHPUnit\Framework\TestCase;

final class StaticConnectionProviderTest extends TestCase
{
public function test_set_unset(): void
{
$factory = fn() => new ConnectionCollection([]);
StaticConnectionProvider::set($factory);
$this->assertNotNull(StaticConnectionProvider::get());

StaticConnectionProvider::unset();
$this->assertNull(StaticConnectionProvider::get());
}

public function test_connection_for_id(): void
{
$id = "foo";
$factory = fn() => new ConnectionCollection([
new ConnectionDefinition(
id: $id,
dsn: "sqlite::memory:"
)
]);
StaticConnectionProvider::set($factory);

$actual = StaticConnectionProvider::connection_for_id($id);

$this->assertEquals($id, $actual->id);
}
}

0 comments on commit 80bf3c6

Please sign in to comment.