- Added callable field in search, searchOne, remove, removeOne, etc...

This commit is contained in:
Dave Mc Nicoll committed 2025-06-26 14:34:12 +00:00
1 parent a5d65025f8
commit 2f482324b2
2 files changed
+99 -8

No files matched your search

@@ -25,7 +25,91 @@ trait SearchRequestFromRequestTrait
protected array $orders = [];
public function fromRequest(ServerRequestInterface $request)
public function fromArray(array|\ArrayAccess $data) : static
{
$this->page = $data['page'] ?? 1;
$classReflection = ObjectReflection::fromClass(static::class)->reflectClass();
foreach($classReflection->getProperties() as $propertyName => $property) {
$attributeList = $property->getAttributes(Attribute\SearchParameter::class);
if ($attributeList) {
$attribute = $attributeList[0]->object;
$fieldName = $attribute->field ?? $propertyName;
# Field could be defined for another entity class
if (is_array($fieldName)) {
$field = \Ulmus\Attribute\Attribute::handleArrayField($fieldName, false);
}
# Default class using it, if SearchRequestParameter is defined
elseif ($classAttributes = $classReflection->getAttributes(SearchRequestParameter::class)) {
$searchRequestAttribute = $classAttributes[0]->object;
$className = $searchRequestAttribute->class;
$field = $className::field($fieldName, $searchRequestAttribute->alias);
}
# Untouched string from the attribute
else {
$field = $fieldName;
}
$value = $data[$propertyName] ?? null;
if ($value !== null) {
$value = $this->transformValue($property->getAttributes(Attribute\PropertyValueModifier::class), $value);
}
switch(true) {
case $attribute instanceof SearchGroupBy:
$this->parseAttributeGroupBy($attribute, $field, $propertyName);
break;
case $attribute instanceof SearchWhere:
case $attribute instanceof SearchLike:
case $attribute instanceof SearchManual:
if ($attribute->toggle) {
$this->$propertyName = ! empty($value);
}
elseif ($value !== null) {
foreach ($property->getTypes() as $type) {
$enum = $type->type;
if (enum_exists($enum)) {
try {
$this->$propertyName = $value instanceof \UnitEnum ? $value : $type->type::from($value);
} catch (\ValueError $ex) {
$cases = implode(', ', array_map(fn($e) => $e->name, $enum::cases()));
throw new \ValueError(
sprintf("Given value '%s' do not exists within enum '%s'. Try one of those values instead : %s", $value, $enum, $cases)
);
}
}
elseif ($type->builtIn || $value instanceof \Stringable ) {
$this->$propertyName = $value;
}
}
}
$this->parseAttributeMethod($attribute, $field, $propertyName);
break;
case $attribute instanceof SearchOrderBy:
if ($value !== null) {
$this->$propertyName = $value;
}
$this->parseAttributeOrderBy($attribute, $field, $propertyName);
break;
}
}
}
return $this;
}
public function fromRequest(ServerRequestInterface $request) : static
{
if (method_exists($this, 'prepare')) {
$this->prepare($request);