- WIP on SearchRequest enums and manual searchs

This commit is contained in:
Dave Mc Nicoll committed 2024-06-28 11:46:09 +00:00
1 parent 0c8591f238
commit 62998faf40
6 files changed
+88 -22

No files matched your search

@@ -2,10 +2,12 @@
namespace Ulmus\SearchRequest;
use Notes\ObjectReflection;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ServerRequestInterface;
use Ulmus\SearchRequest\Attribute\{PropertyValueSource,
SearchManual,
SearchParameter,
SearchWhere,
SearchLike,
@@ -33,15 +35,14 @@ trait SearchRequestFromRequestTrait
$this->page = $queryParams->offsetExists('page') ? $queryParams['page'] : 1;
$classReflection = new \ReflectionClass($this);
$classReflection = ObjectReflection::fromClass(static::class)->reflectClass();
foreach($classReflection->getProperties() as $property) {
$attributeList = $property->getAttributes();
$attributeReflection = array_filter($attributeList, fn($e) => $e->newInstance() instanceof Attribute\SearchParameter);
foreach($classReflection->getProperties() as $propertyName => $property) {
$attributeList = $property->getAttributes(Attribute\SearchParameter::class);
if ($attributeList) {
$attribute = $attributeList[0]->object;
if ($attributeReflection) {
$attribute = $attributeReflection[0]->newInstance();
$propertyName = $property->getName();
$fieldName = $attribute->field ?? $propertyName;
# Field could be defined for another entity class
@@ -50,7 +51,7 @@ trait SearchRequestFromRequestTrait
}
# Default class using it, if SearchRequestParameter is defined
elseif ($classAttributes = $classReflection->getAttributes(SearchRequestParameter::class)) {
$searchRequestAttribute = $classAttributes[0]->newInstance();
$searchRequestAttribute = $classAttributes[0]->object;
$className = $searchRequestAttribute->class;
$field = $className::field($fieldName, $searchRequestAttribute->alias);
}
@@ -62,15 +63,33 @@ trait SearchRequestFromRequestTrait
$value = $this->getValueFromSource($request, $propertyName, $attribute);
if ($value !== null) {
$value = $this->transformValue($attributeList, $value);
$value = $this->transformValue($property->getAttributes(Attribute\PropertyValueModifier::class), $value);
}
if ($attribute instanceof SearchWhere || $attribute instanceof SearchLike) {
if ($attribute instanceof SearchWhere || $attribute instanceof SearchLike || $attribute instanceof SearchManual) {
if ($attribute->toggle) {
$this->$propertyName = ! empty($value);
}
elseif ($value !== null) {
$this->$propertyName = $value;
foreach($property->getTypes() as $type) {
$enum = $type->type;
if (enum_exists($enum)) {
try {
$this->$propertyName = $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) {
$this->$propertyName = $value;
}
}
}
$this->parseAttributeMethod($attribute, $field, $propertyName);
@@ -93,7 +112,7 @@ trait SearchRequestFromRequestTrait
protected function getValueFromSource(RequestInterface $request, string $propertyName, SearchParameter $attribute) : mixed
{
$queryParamName = $attribute->getParameters() ?? [ $propertyName ];
$queryParamName = $attribute->getParameters() ?: [ $propertyName ];
foreach($attribute->getSources() as $source) {
switch($source) {
@@ -124,15 +143,10 @@ trait SearchRequestFromRequestTrait
return null;
}
protected function transformValue(array $attributeList, mixed $value, ) : mixed
protected function transformValue(array $valueModifierAttributes, mixed $value, ) : mixed
{
$transforms = array_map(function($e) {
$instance = $e->newInstance();
return $instance instanceof Attribute\PropertyValueModifier ? $e->newInstance() : null;
}, $attributeList);
foreach(array_filter($transforms) as $transform) {
$value = $transform->run($value);
foreach($valueModifierAttributes as $transform) {
$value = $transform->object->run($value);
}
return $value;