diff --git a/src/Entity/DatasetHandler.php b/src/Entity/DatasetHandler.php index 1a3b32b..0faba5f 100644 --- a/src/Entity/DatasetHandler.php +++ b/src/Entity/DatasetHandler.php @@ -78,7 +78,6 @@ class DatasetHandler } } elseif ( EntityField::isScalarType($type->type) ) { - if ( $type->type === 'string' ) { $attribute = $this->entityResolver->searchFieldAnnotation($field->name, [ Field::class ] ); @@ -102,8 +101,10 @@ class DatasetHandler try { yield $field->name => Ulmus::instanciateObject($type->type, [ $value ]); } - catch(\Error $e) { - throw new \Error(sprintf("%s for class '%s' on field '%s'", $e->getMessage(), get_class($this), $field->name)); + catch(\Throwable $e) { + $type = $e::class; + + throw new $type(sprintf("%s for class '%s' on field '%s'", $e->getMessage(), get_class($this), $field->name), 101, $e); } } } diff --git a/src/Entity/ObjectInstanciator.php b/src/Entity/ObjectInstanciator.php index 507dce6..fc81d82 100644 --- a/src/Entity/ObjectInstanciator.php +++ b/src/Entity/ObjectInstanciator.php @@ -5,22 +5,43 @@ namespace Ulmus\Entity; class ObjectInstanciator { protected array $objectCallbackDefinition; - + public function instanciate(string $type, array $arguments) : object { - if ( isset($this->objectCallbackDefinition[$type]) ) { + if (is_object($arguments[0]) && is_a($arguments[0], $type)) { + return $arguments[0]; + } + elseif ( isset($this->objectCallbackDefinition[$type]) ) { return $this->objectCallbackDefinition[$type](...$arguments); } - elseif ( ($obj = new $type() ) instanceof EntityObjectInterface ) { - return $obj->load(...$arguments); - } - elseif ( ($obj = new $type() ) instanceof JsonUnserializable ) { - $obj->jsonUnserialize(json_decode($arguments[0], true)); - - return $obj; - } else { - return new $type(...$arguments); + $reflect = new \ReflectionClass($type); + + if ( is_a($type, EntityObjectInterface::class, true) ) { + try { + $obj = $reflect->newInstance(); + } + catch(\Throwable $t) { + $obj = $reflect->newInstanceWithoutConstructor(); + } + + return $obj->load(...$arguments); + } + elseif ( is_a($type, JsonUnserializable::class, true)) { + try { + $obj = $reflect->newInstance(); + } + catch(\Throwable $t) { + $obj = $reflect->newInstanceWithoutConstructor(); + } + + $obj->jsonUnserialize(json_decode($arguments[0], true)); + + return $obj; + } + else { + return new $type(...$arguments); + } } } diff --git a/src/SearchRequest/Attribute/SearchOrderBy.php b/src/SearchRequest/Attribute/SearchOrderBy.php index c23866f..f7e14c8 100644 --- a/src/SearchRequest/Attribute/SearchOrderBy.php +++ b/src/SearchRequest/Attribute/SearchOrderBy.php @@ -16,5 +16,6 @@ class SearchOrderBy extends SearchParameter public null|string|\Stringable|array $field = null, public null|SearchMethodEnum $order = null, public string $description = "", + public PropertyValueSource|array $source = PropertyValueSource::QueryParams, ) {} } \ No newline at end of file diff --git a/src/SearchRequest/SearchRequestFromRequestTrait.php b/src/SearchRequest/SearchRequestFromRequestTrait.php index 08871d4..3eb848d 100644 --- a/src/SearchRequest/SearchRequestFromRequestTrait.php +++ b/src/SearchRequest/SearchRequestFromRequestTrait.php @@ -23,6 +23,11 @@ trait SearchRequestFromRequestTrait protected array $parsedParameters = []; + public function toArray() : array + { + return json_decode(json_encode($this), true); + } + public function fromParsedParameters(Repository $repository) : SearchRequestInterface { foreach($this->parsedParameters as $property => $param) { @@ -69,6 +74,7 @@ trait SearchRequestFromRequestTrait $this->applyConditions( fn($propertyName) => $data["$propertyName:condition"] ?? null ); + return $this; } @@ -171,7 +177,7 @@ trait SearchRequestFromRequestTrait case $attribute instanceof SearchOrderBy: if ($value !== null) { - $this->$propertyName = $value; + $this->$propertyName = $this->parseValue($property, $value); } $this->parseAttributeOrderBy($attribute, $field, $propertyName); @@ -184,24 +190,7 @@ trait SearchRequestFromRequestTrait $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->$propertyName = $this->parseValue($property, $value); } $this->parseAttributeMethod($attribute, $field, $propertyName); @@ -211,6 +200,28 @@ trait SearchRequestFromRequestTrait } } + protected function parseValue(\Notes\Common\ReflectedProperty $property, mixed $value) : mixed + { + foreach ($property->getTypes() as $type) { + $enum = $type->type; + + if (enum_exists($enum)) { + try { + return $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 ) { + return $value; + } + } + } + protected function getEntityField(array|string $fieldName) : string|\Stringable { # Field could be defined for another entity class diff --git a/src/SearchRequest/SearchRequestPaginationTrait.php b/src/SearchRequest/SearchRequestPaginationTrait.php index 66016e5..d241557 100644 --- a/src/SearchRequest/SearchRequestPaginationTrait.php +++ b/src/SearchRequest/SearchRequestPaginationTrait.php @@ -3,16 +3,19 @@ namespace Ulmus\SearchRequest; use Ulmus\Repository; +use Ulmus\SearchRequest\Attribute\SearchManual; trait SearchRequestPaginationTrait { use DeprecatedSearchRequestFormatTrait; public int $count = 0; + #[SearchManual] public int $page = 1; public int $pageCount = 0; - + + #[SearchManual] public int $limit = 25; public bool $skipCount = false;