- WIP on SearchRequest enums and manual searchs
This commit is contained in:
parent
0c8591f238
commit
62998faf40
|
@ -129,7 +129,7 @@ class SQLite implements AdapterInterface, MigrateInterface, SqlAdapterInterface
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $typeOnly ? $type : $type . ( $length ? "($length" . ( isset($precision) ? ",$precision" : "" ) . ")" : "" );
|
return $typeOnly ? $type : $type . ( $length ? "($length" . ")" : "" );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function tableSyntax() : array
|
public function tableSyntax() : array
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace Ulmus\Entity\Sqlite;
|
namespace Ulmus\Entity\Sqlite;
|
||||||
|
|
||||||
|
use Notes\Common\ReflectedProperty;
|
||||||
use Ulmus\EntityCollection;
|
use Ulmus\EntityCollection;
|
||||||
|
|
||||||
use Ulmus\{Attribute\Obj\Table};
|
use Ulmus\{Attribute\Obj\Table};
|
||||||
|
@ -32,4 +33,29 @@ class Column
|
||||||
|
|
||||||
#[Field(name: "pk")]
|
#[Field(name: "pk")]
|
||||||
public bool $primaryKey;
|
public bool $primaryKey;
|
||||||
|
|
||||||
|
public function matchFieldDefinition(ReflectedProperty $definition) : bool
|
||||||
|
{
|
||||||
|
if ($this->notNull === $definition->allowsNull()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($definition->value)) {
|
||||||
|
if ( $definition->value !== $this->defaultValue) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
elseif (! isset($definition->value)) {
|
||||||
|
if ( ! $this->defaultValueIsNull() ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function defaultValueIsNull() : bool
|
||||||
|
{
|
||||||
|
return $this->defaultValue === null || $this->defaultValue === "NULL";
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -57,7 +57,7 @@ class EntityCollection extends \ArrayObject implements \JsonSerializable {
|
||||||
return $this->filtersCollection($callback, true, false)->toArray();
|
return $this->filtersCollection($callback, true, false)->toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function filtersCount(Callable $callback) : array
|
public function filtersCount(Callable $callback) : int
|
||||||
{
|
{
|
||||||
return $this->filtersCollection($callback, true, false)->count();
|
return $this->filtersCollection($callback, true, false)->count();
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\Migration\Sql;
|
||||||
|
|
||||||
|
class SqliteMigration
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\SearchRequest\Attribute;
|
||||||
|
|
||||||
|
use Ulmus\SearchRequest\SearchMethodEnum;
|
||||||
|
|
||||||
|
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||||
|
class SearchManual extends SearchParameter
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public null|string|array $parameter = null,
|
||||||
|
public null|string|\Stringable|array $field = null,
|
||||||
|
public bool $toggle = false,
|
||||||
|
public SearchMethodEnum $method = SearchMethodEnum::Manual,
|
||||||
|
public string $description = "",
|
||||||
|
public PropertyValueSource|array $source = PropertyValueSource::QueryParams,
|
||||||
|
) {}
|
||||||
|
}
|
|
@ -2,10 +2,12 @@
|
||||||
|
|
||||||
namespace Ulmus\SearchRequest;
|
namespace Ulmus\SearchRequest;
|
||||||
|
|
||||||
|
use Notes\ObjectReflection;
|
||||||
use Psr\Http\Message\RequestInterface;
|
use Psr\Http\Message\RequestInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
|
||||||
use Ulmus\SearchRequest\Attribute\{PropertyValueSource,
|
use Ulmus\SearchRequest\Attribute\{PropertyValueSource,
|
||||||
|
SearchManual,
|
||||||
SearchParameter,
|
SearchParameter,
|
||||||
SearchWhere,
|
SearchWhere,
|
||||||
SearchLike,
|
SearchLike,
|
||||||
|
@ -33,15 +35,14 @@ trait SearchRequestFromRequestTrait
|
||||||
|
|
||||||
$this->page = $queryParams->offsetExists('page') ? $queryParams['page'] : 1;
|
$this->page = $queryParams->offsetExists('page') ? $queryParams['page'] : 1;
|
||||||
|
|
||||||
$classReflection = new \ReflectionClass($this);
|
$classReflection = ObjectReflection::fromClass(static::class)->reflectClass();
|
||||||
|
|
||||||
foreach($classReflection->getProperties() as $property) {
|
foreach($classReflection->getProperties() as $propertyName => $property) {
|
||||||
$attributeList = $property->getAttributes();
|
$attributeList = $property->getAttributes(Attribute\SearchParameter::class);
|
||||||
$attributeReflection = array_filter($attributeList, fn($e) => $e->newInstance() instanceof Attribute\SearchParameter);
|
|
||||||
|
if ($attributeList) {
|
||||||
|
$attribute = $attributeList[0]->object;
|
||||||
|
|
||||||
if ($attributeReflection) {
|
|
||||||
$attribute = $attributeReflection[0]->newInstance();
|
|
||||||
$propertyName = $property->getName();
|
|
||||||
$fieldName = $attribute->field ?? $propertyName;
|
$fieldName = $attribute->field ?? $propertyName;
|
||||||
|
|
||||||
# Field could be defined for another entity class
|
# Field could be defined for another entity class
|
||||||
|
@ -50,7 +51,7 @@ trait SearchRequestFromRequestTrait
|
||||||
}
|
}
|
||||||
# Default class using it, if SearchRequestParameter is defined
|
# Default class using it, if SearchRequestParameter is defined
|
||||||
elseif ($classAttributes = $classReflection->getAttributes(SearchRequestParameter::class)) {
|
elseif ($classAttributes = $classReflection->getAttributes(SearchRequestParameter::class)) {
|
||||||
$searchRequestAttribute = $classAttributes[0]->newInstance();
|
$searchRequestAttribute = $classAttributes[0]->object;
|
||||||
$className = $searchRequestAttribute->class;
|
$className = $searchRequestAttribute->class;
|
||||||
$field = $className::field($fieldName, $searchRequestAttribute->alias);
|
$field = $className::field($fieldName, $searchRequestAttribute->alias);
|
||||||
}
|
}
|
||||||
|
@ -62,15 +63,33 @@ trait SearchRequestFromRequestTrait
|
||||||
$value = $this->getValueFromSource($request, $propertyName, $attribute);
|
$value = $this->getValueFromSource($request, $propertyName, $attribute);
|
||||||
|
|
||||||
if ($value !== null) {
|
if ($value !== null) {
|
||||||
$value = $this->transformValue($attributeList, $value);
|
$value = $this->transformValue($property->getAttributes(Attribute\PropertyValueModifier::class), $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($attribute instanceof SearchWhere || $attribute instanceof SearchLike) {
|
if ($attribute instanceof SearchWhere || $attribute instanceof SearchLike || $attribute instanceof SearchManual) {
|
||||||
if ($attribute->toggle) {
|
if ($attribute->toggle) {
|
||||||
$this->$propertyName = ! empty($value);
|
$this->$propertyName = ! empty($value);
|
||||||
}
|
}
|
||||||
elseif ($value !== null) {
|
elseif ($value !== null) {
|
||||||
$this->$propertyName = $value;
|
foreach($property->getTypes() as $type) {
|
||||||
|
$enum = $type->type;
|
||||||
|
|
||||||
|
if (enum_exists($enum)) {
|
||||||
|
try {
|
||||||
|
$this->$propertyName = $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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->parseAttributeMethod($attribute, $field, $propertyName);
|
$this->parseAttributeMethod($attribute, $field, $propertyName);
|
||||||
|
@ -93,7 +112,7 @@ trait SearchRequestFromRequestTrait
|
||||||
|
|
||||||
protected function getValueFromSource(RequestInterface $request, string $propertyName, SearchParameter $attribute) : mixed
|
protected function getValueFromSource(RequestInterface $request, string $propertyName, SearchParameter $attribute) : mixed
|
||||||
{
|
{
|
||||||
$queryParamName = $attribute->getParameters() ?? [ $propertyName ];
|
$queryParamName = $attribute->getParameters() ?: [ $propertyName ];
|
||||||
|
|
||||||
foreach($attribute->getSources() as $source) {
|
foreach($attribute->getSources() as $source) {
|
||||||
switch($source) {
|
switch($source) {
|
||||||
|
@ -124,15 +143,10 @@ trait SearchRequestFromRequestTrait
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function transformValue(array $attributeList, mixed $value, ) : mixed
|
protected function transformValue(array $valueModifierAttributes, mixed $value, ) : mixed
|
||||||
{
|
{
|
||||||
$transforms = array_map(function($e) {
|
foreach($valueModifierAttributes as $transform) {
|
||||||
$instance = $e->newInstance();
|
$value = $transform->object->run($value);
|
||||||
return $instance instanceof Attribute\PropertyValueModifier ? $e->newInstance() : null;
|
|
||||||
}, $attributeList);
|
|
||||||
|
|
||||||
foreach(array_filter($transforms) as $transform) {
|
|
||||||
$value = $transform->run($value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $value;
|
return $value;
|
||||||
|
|
Loading…
Reference in New Issue