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

39 lines
1.4 KiB
PHP

<?php
namespace Ulmus\Attribute\Property;
use Ulmus\Attribute\Attribute;
use Ulmus\Query;
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)]
class Where {
public function __construct(
public string|\Stringable|array $field,
public mixed $value = null,
public string $operator = Query\Where::OPERATOR_EQUAL,
public string $condition = Query\Where::CONDITION_AND,
public string|\Stringable|array|null $fieldValue = null,
public null|array|\Closure $generateValue = null,
) {
$this->field = Attribute::handleArrayField($field);
$this->fieldValue = Attribute::handleArrayField($fieldValue);
}
public function getValue(/* null|EntityInterface */ $entity = null) : mixed
{
if ($this->generateValue) {
if ($entity) {
return call_user_func_array($this->generateValue, [ $entity ]);
}
else {
throw new \Exception(sprintf("Could not generate value from non-instanciated entity for field %s.", (string) $this->field));
}
}
elseif ($this->fieldValue && $entity) {
throw new \Exception(sprintf("Field value, from %s, could not be included in query since the entity is already loaded; it is meant to be used with a OneToOne relation loaded within a join.", (string) $this->fieldValue));
}
return $this->fieldValue ?? $this->value;
}
}