- Still working on SearchRequest for optimized and more advanced query manipulations

- Minor bugfixes in DatasetHandler and ObjectInstanciator
This commit is contained in:
Dave M. 2026-07-01 13:00:01 -04:00
parent 5d32d9f635
commit 709ed63323
5 changed files with 71 additions and 34 deletions

View File

@ -78,7 +78,6 @@ class DatasetHandler
} }
} }
elseif ( EntityField::isScalarType($type->type) ) { elseif ( EntityField::isScalarType($type->type) ) {
if ( $type->type === 'string' ) { if ( $type->type === 'string' ) {
$attribute = $this->entityResolver->searchFieldAnnotation($field->name, [ Field::class ] ); $attribute = $this->entityResolver->searchFieldAnnotation($field->name, [ Field::class ] );
@ -102,8 +101,10 @@ class DatasetHandler
try { try {
yield $field->name => Ulmus::instanciateObject($type->type, [ $value ]); yield $field->name => Ulmus::instanciateObject($type->type, [ $value ]);
} }
catch(\Error $e) { catch(\Throwable $e) {
throw new \Error(sprintf("%s for class '%s' on field '%s'", $e->getMessage(), get_class($this), $field->name)); $type = $e::class;
throw new $type(sprintf("%s for class '%s' on field '%s'", $e->getMessage(), get_class($this), $field->name), 101, $e);
} }
} }
} }

View File

@ -8,13 +8,33 @@ class ObjectInstanciator {
public function instanciate(string $type, array $arguments) : object 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); return $this->objectCallbackDefinition[$type](...$arguments);
} }
elseif ( ($obj = new $type() ) instanceof EntityObjectInterface ) { else {
$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); return $obj->load(...$arguments);
} }
elseif ( ($obj = new $type() ) instanceof JsonUnserializable ) { elseif ( is_a($type, JsonUnserializable::class, true)) {
try {
$obj = $reflect->newInstance();
}
catch(\Throwable $t) {
$obj = $reflect->newInstanceWithoutConstructor();
}
$obj->jsonUnserialize(json_decode($arguments[0], true)); $obj->jsonUnserialize(json_decode($arguments[0], true));
return $obj; return $obj;
@ -23,6 +43,7 @@ class ObjectInstanciator {
return new $type(...$arguments); return new $type(...$arguments);
} }
} }
}
public function convert(object $obj) public function convert(object $obj)
{ {

View File

@ -16,5 +16,6 @@ class SearchOrderBy extends SearchParameter
public null|string|\Stringable|array $field = null, public null|string|\Stringable|array $field = null,
public null|SearchMethodEnum $order = null, public null|SearchMethodEnum $order = null,
public string $description = "", public string $description = "",
public PropertyValueSource|array $source = PropertyValueSource::QueryParams,
) {} ) {}
} }

View File

@ -23,6 +23,11 @@ trait SearchRequestFromRequestTrait
protected array $parsedParameters = []; protected array $parsedParameters = [];
public function toArray() : array
{
return json_decode(json_encode($this), true);
}
public function fromParsedParameters(Repository $repository) : SearchRequestInterface public function fromParsedParameters(Repository $repository) : SearchRequestInterface
{ {
foreach($this->parsedParameters as $property => $param) { foreach($this->parsedParameters as $property => $param) {
@ -69,6 +74,7 @@ trait SearchRequestFromRequestTrait
$this->applyConditions( $this->applyConditions(
fn($propertyName) => $data["$propertyName:condition"] ?? null fn($propertyName) => $data["$propertyName:condition"] ?? null
); );
return $this; return $this;
} }
@ -171,7 +177,7 @@ trait SearchRequestFromRequestTrait
case $attribute instanceof SearchOrderBy: case $attribute instanceof SearchOrderBy:
if ($value !== null) { if ($value !== null) {
$this->$propertyName = $value; $this->$propertyName = $this->parseValue($property, $value);
} }
$this->parseAttributeOrderBy($attribute, $field, $propertyName); $this->parseAttributeOrderBy($attribute, $field, $propertyName);
@ -184,12 +190,24 @@ trait SearchRequestFromRequestTrait
$this->$propertyName = !empty($value); $this->$propertyName = !empty($value);
} }
elseif ($value !== null) { elseif ($value !== null) {
$this->$propertyName = $this->parseValue($property, $value);
}
$this->parseAttributeMethod($attribute, $field, $propertyName);
break;
}
}
}
}
protected function parseValue(\Notes\Common\ReflectedProperty $property, mixed $value) : mixed
{
foreach ($property->getTypes() as $type) { foreach ($property->getTypes() as $type) {
$enum = $type->type; $enum = $type->type;
if (enum_exists($enum)) { if (enum_exists($enum)) {
try { try {
$this->$propertyName = $value instanceof \UnitEnum ? $value : $type->type::from($value); return $value instanceof \UnitEnum ? $value : $type->type::from($value);
} catch (\ValueError $ex) { } catch (\ValueError $ex) {
$cases = implode(', ', array_map(fn($e) => $e->name, $enum::cases())); $cases = implode(', ', array_map(fn($e) => $e->name, $enum::cases()));
@ -199,14 +217,7 @@ trait SearchRequestFromRequestTrait
} }
} }
elseif ($type->builtIn || $value instanceof \Stringable ) { elseif ($type->builtIn || $value instanceof \Stringable ) {
$this->$propertyName = $value; return $value;
}
}
}
$this->parseAttributeMethod($attribute, $field, $propertyName);
break;
}
} }
} }
} }

View File

@ -3,16 +3,19 @@
namespace Ulmus\SearchRequest; namespace Ulmus\SearchRequest;
use Ulmus\Repository; use Ulmus\Repository;
use Ulmus\SearchRequest\Attribute\SearchManual;
trait SearchRequestPaginationTrait { trait SearchRequestPaginationTrait {
use DeprecatedSearchRequestFormatTrait; use DeprecatedSearchRequestFormatTrait;
public int $count = 0; public int $count = 0;
#[SearchManual]
public int $page = 1; public int $page = 1;
public int $pageCount = 0; public int $pageCount = 0;
#[SearchManual]
public int $limit = 25; public int $limit = 25;
public bool $skipCount = false; public bool $skipCount = false;