From c56b5841403f0158b1550b8e0b0dff41eb67e673 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Wed, 2 Oct 2024 14:18:35 -0400 Subject: [PATCH] - Merges --- .../SearchRequestFromRequestTrait.php | 82 +++++++++---------- 1 file changed, 38 insertions(+), 44 deletions(-) diff --git a/src/SearchRequest/SearchRequestFromRequestTrait.php b/src/SearchRequest/SearchRequestFromRequestTrait.php index 7bee2f4..3bbe2f9 100644 --- a/src/SearchRequest/SearchRequestFromRequestTrait.php +++ b/src/SearchRequest/SearchRequestFromRequestTrait.php @@ -60,49 +60,53 @@ trait SearchRequestFromRequestTrait $field = $fieldName; } - $value = $this->getValueFromSource($request, $propertyName, $attribute); + switch(true) { + case $attribute instanceof SearchGroupBy: + $this->parseAttributeGroupBy($attribute, $field, $propertyName); + break; - if ($value !== null) { - $value = $this->transformValue($property->getAttributes(Attribute\PropertyValueModifier::class), $value); - } + case $attribute instanceof SearchWhere: + case $attribute instanceof SearchLike: + case $attribute instanceof SearchManuel: - if ($attribute instanceof SearchWhere || $attribute instanceof SearchLike || $attribute instanceof SearchManual) { - if ($attribute->toggle) { - $this->$propertyName = ! empty($value); - } - elseif ($value !== null) { - foreach($property->getTypes() as $type) { - $enum = $type->type; + $value = $this->getValueFromSource($request, $propertyName, $attribute); - if (enum_exists($enum)) { - try { - $this->$propertyName = $type->type::from($value); + if ($value !== null) { + $value = $this->transformValue($property->getAttributes(Attribute\PropertyValueModifier::class), $value); + } + + 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) { + $this->$propertyName = $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); - } - elseif ($attribute instanceof SearchOrderBy) { - if ($value !== null) { - $this->$propertyName = $value; + $this->parseAttributeMethod($attribute, $field, $propertyName); + break; + + case $attribute instanceof SearchOrderBy: + if ($value !== null) { + $this->$propertyName = $value; + } $this->parseAttributeOrderBy($attribute, $field, $propertyName); - } - } - elseif ($attribute instanceof SearchGroupBy) { - $this->parseAttributeGroupBy($attribute, $field, $propertyName); + break; } } } @@ -188,14 +192,4 @@ trait SearchRequestFromRequestTrait $this->groups[] = $field; } } - - # @TODO ! - /* protected function parseAttributeGroupBy(object $attribute, string $field, string $propertyName,) : void - { - switch ($attribute->order) { - case SearchMethodEnum::GroupBy: - $this->groups[$field]; - break; - } - }*/ } \ No newline at end of file