Files
ulmus/src/Attribute/Property/Join.php
T

52 lines
1.4 KiB
PHP

<?php
namespace Ulmus\Attribute\Property;
use Ulmus\Attribute\Attribute;
use Ulmus\Repository;
#[\Attribute]
class Join implements ResettablePropertyInterface {
public function __construct(
public string $type,
public null|string|\Stringable|array $key = null,
public null|string|\Stringable|array $foreignKey = null,
public null|string $entity = null,
public null|string $alias = null,
) {
$this->key = Attribute::handleArrayField($this->key);
$this->foreignKey = Attribute::handleArrayField($this->foreignKey);
}
## AWAITING THE Closures in constant expression from PHP 8.5 !
public function foreignKey(? Repository $repository = null) : mixed
{
if (is_string($this->foreignKey)) {
if (str_starts_with($this->foreignKey, 'fn(') && str_contains($this->foreignKey, '=>')) {
$closure = eval("return {$this->foreignKey};");
return $repository ? $closure($repository) : $closure;
}
return $this->entity::field($this->foreignKey, $this->alias);
}
return $this->foreignKey;
}
public function isOneToOne() : bool
{
return true;
}
public function isOneToMany() : bool
{
return false;
}
public function isManyToMany() : bool
{
return false;
}
}