-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
58 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,156 +1,81 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the ICanBoogie package. | ||
* | ||
* (c) Olivier Laviale <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace ICanBoogie\ActiveRecord; | ||
|
||
use ArrayAccess; | ||
use Closure; | ||
use ICanBoogie\ActiveRecord; | ||
use ICanBoogie\OffsetNotWritable; | ||
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' | ||
*/ | ||
class RelationCollection implements ArrayAccess | ||
class RelationCollection | ||
{ | ||
/** | ||
* Relations. | ||
* | ||
* @var array<string, Relation> | ||
* Where _key_ is a getter name e.g. 'comments' | ||
* Where _key_ is a getter name; for example, 'comments' | ||
*/ | ||
private array $relations; | ||
private readonly array $relations; | ||
|
||
public function __construct( | ||
public readonly Model $model, | ||
?ActiveRecord\Config\Association $association, | ||
?Association $association | ||
) { | ||
$this->apply_association($association); | ||
} | ||
$relations = []; | ||
|
||
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, | ||
); | ||
} | ||
|
||
private function apply_association(?ActiveRecord\Config\Association $association): void | ||
{ | ||
if (!$association) { | ||
return; | ||
} | ||
if ($association->has_many) { | ||
assert(is_string($this->model->primary)); | ||
} | ||
|
||
foreach ($association->belongs_to as $r) { | ||
$this->belongs_to( | ||
related: $r->associate, | ||
local_key: $r->local_key, | ||
foreign_key: $r->foreign_key, | ||
as: $r->as, | ||
); | ||
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, | ||
); | ||
} | ||
} | ||
|
||
foreach ($association->has_many as $r) { | ||
$this->has_many( | ||
related: $r->associate, | ||
foreign_key: $r->foreign_key, | ||
as: $r->as, | ||
through: $r->through, | ||
); | ||
} | ||
$this->relations = $relations; | ||
} | ||
|
||
/** | ||
* Checks if a relation exists. | ||
* Whether a relation exists. | ||
* | ||
* @param string $offset Relation name. | ||
* @param non-empty-string $relation_name | ||
*/ | ||
public function offsetExists(mixed $offset): bool | ||
public function has(string $relation_name): bool | ||
{ | ||
return isset($this->relations[$offset]); | ||
return isset($this->relations[$relation_name]); | ||
} | ||
|
||
/** | ||
* Returns a relation. | ||
* Returns an existing relation. | ||
* | ||
* @param string $offset Relation name. | ||
* @param non-empty-string $relation_name | ||
* | ||
* @throws RelationNotDefined if the relation is not defined. | ||
*/ | ||
public function offsetGet(mixed $offset): Relation | ||
{ | ||
return $this->relations[$offset] | ||
?? throw new RelationNotDefined($offset, $this); | ||
} | ||
|
||
/** | ||
* @throws OffsetNotWritable because relations cannot be set. | ||
* @throws RelationNotDefined if the relation doesn't exist. | ||
*/ | ||
public function offsetSet(mixed $offset, mixed $value): void | ||
public function get(string $relation_name): ?Relation | ||
{ | ||
throw new OffsetNotWritable($offset, $this); | ||
} | ||
|
||
/** | ||
* @throws OffsetNotWritable because relations cannot be unset. | ||
*/ | ||
public function offsetUnset(mixed $offset): void | ||
{ | ||
throw new OffsetNotWritable($offset, $this); | ||
} | ||
|
||
/** | ||
* 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 | ||
*/ | ||
public function belongs_to( | ||
string $related, | ||
string $local_key, | ||
string $foreign_key, | ||
string $as, | ||
): void { | ||
$this->relations[$as] = 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 | ||
*/ | ||
public function has_many( | ||
string $related, | ||
string $foreign_key, | ||
string $as, | ||
?string $through = null, | ||
): void { | ||
assert(is_string($this->model->primary)); | ||
|
||
$this->relations[$as] = new HasManyRelation( | ||
owner: $this->model, | ||
related: $related, | ||
foreign_key: $foreign_key, | ||
as: $as, | ||
through: $through, | ||
); | ||
return $this->relations[$relation_name] | ||
?? throw new RelationNotDefined($relation_name, $this); | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters