- Still working on SearchRequest for optimized and more advanced query manipulations
- Minor bugfixes in DatasetHandler and ObjectInstanciator
This commit is contained in:
parent
5d32d9f635
commit
709ed63323
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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,
|
||||
) {}
|
||||
}
|
||||
@ -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
|
||||
|
||||
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user