- 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) ) {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,22 +5,43 @@ namespace Ulmus\Entity;
|
|||||||
class ObjectInstanciator {
|
class ObjectInstanciator {
|
||||||
|
|
||||||
protected array $objectCallbackDefinition;
|
protected array $objectCallbackDefinition;
|
||||||
|
|
||||||
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 ) {
|
|
||||||
return $obj->load(...$arguments);
|
|
||||||
}
|
|
||||||
elseif ( ($obj = new $type() ) instanceof JsonUnserializable ) {
|
|
||||||
$obj->jsonUnserialize(json_decode($arguments[0], true));
|
|
||||||
|
|
||||||
return $obj;
|
|
||||||
}
|
|
||||||
else {
|
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|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,
|
||||||
) {}
|
) {}
|
||||||
}
|
}
|
||||||
@ -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,24 +190,7 @@ trait SearchRequestFromRequestTrait
|
|||||||
$this->$propertyName = !empty($value);
|
$this->$propertyName = !empty($value);
|
||||||
}
|
}
|
||||||
elseif ($value !== null) {
|
elseif ($value !== null) {
|
||||||
foreach ($property->getTypes() as $type) {
|
$this->$propertyName = $this->parseValue($property, $value);
|
||||||
$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->parseAttributeMethod($attribute, $field, $propertyName);
|
$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
|
protected function getEntityField(array|string $fieldName) : string|\Stringable
|
||||||
{
|
{
|
||||||
# Field could be defined for another entity class
|
# Field could be defined for another entity class
|
||||||
|
|||||||
@ -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;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user