From 62998faf4013212327c227db511fbc03fb061e0a Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Fri, 28 Jun 2024 11:46:09 +0000 Subject: [PATCH] - WIP on SearchRequest enums and manual searchs --- src/Adapter/SQLite.php | 2 +- src/Entity/Sqlite/Column.php | 26 +++++++++ src/EntityCollection.php | 2 +- src/Migration/Sql/SqliteMigration.php | 8 +++ src/SearchRequest/Attribute/SearchManual.php | 18 +++++++ .../SearchRequestFromRequestTrait.php | 54 ++++++++++++------- 6 files changed, 88 insertions(+), 22 deletions(-) create mode 100644 src/Migration/Sql/SqliteMigration.php create mode 100644 src/SearchRequest/Attribute/SearchManual.php diff --git a/src/Adapter/SQLite.php b/src/Adapter/SQLite.php index 6567558..73d3949 100644 --- a/src/Adapter/SQLite.php +++ b/src/Adapter/SQLite.php @@ -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 diff --git a/src/Entity/Sqlite/Column.php b/src/Entity/Sqlite/Column.php index 4537b3c..236360a 100644 --- a/src/Entity/Sqlite/Column.php +++ b/src/Entity/Sqlite/Column.php @@ -2,6 +2,7 @@ namespace Ulmus\Entity\Sqlite; +use Notes\Common\ReflectedProperty; use Ulmus\EntityCollection; use Ulmus\{Attribute\Obj\Table}; @@ -32,4 +33,29 @@ class Column #[Field(name: "pk")] 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"; + } } \ No newline at end of file diff --git a/src/EntityCollection.php b/src/EntityCollection.php index b4074b3..a159184 100644 --- a/src/EntityCollection.php +++ b/src/EntityCollection.php @@ -57,7 +57,7 @@ class EntityCollection extends \ArrayObject implements \JsonSerializable { 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(); } diff --git a/src/Migration/Sql/SqliteMigration.php b/src/Migration/Sql/SqliteMigration.php new file mode 100644 index 0000000..f8c0abc --- /dev/null +++ b/src/Migration/Sql/SqliteMigration.php @@ -0,0 +1,8 @@ +page = $queryParams->offsetExists('page') ? $queryParams['page'] : 1; - $classReflection = new \ReflectionClass($this); + $classReflection = ObjectReflection::fromClass(static::class)->reflectClass(); - foreach($classReflection->getProperties() as $property) { - $attributeList = $property->getAttributes(); - $attributeReflection = array_filter($attributeList, fn($e) => $e->newInstance() instanceof Attribute\SearchParameter); + foreach($classReflection->getProperties() as $propertyName => $property) { + $attributeList = $property->getAttributes(Attribute\SearchParameter::class); + + if ($attributeList) { + $attribute = $attributeList[0]->object; - if ($attributeReflection) { - $attribute = $attributeReflection[0]->newInstance(); - $propertyName = $property->getName(); $fieldName = $attribute->field ?? $propertyName; # Field could be defined for another entity class @@ -50,7 +51,7 @@ trait SearchRequestFromRequestTrait } # Default class using it, if SearchRequestParameter is defined elseif ($classAttributes = $classReflection->getAttributes(SearchRequestParameter::class)) { - $searchRequestAttribute = $classAttributes[0]->newInstance(); + $searchRequestAttribute = $classAttributes[0]->object; $className = $searchRequestAttribute->class; $field = $className::field($fieldName, $searchRequestAttribute->alias); } @@ -62,15 +63,33 @@ trait SearchRequestFromRequestTrait $value = $this->getValueFromSource($request, $propertyName, $attribute); 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) { $this->$propertyName = ! empty($value); } 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); @@ -93,7 +112,7 @@ trait SearchRequestFromRequestTrait protected function getValueFromSource(RequestInterface $request, string $propertyName, SearchParameter $attribute) : mixed { - $queryParamName = $attribute->getParameters() ?? [ $propertyName ]; + $queryParamName = $attribute->getParameters() ?: [ $propertyName ]; foreach($attribute->getSources() as $source) { switch($source) { @@ -124,15 +143,10 @@ trait SearchRequestFromRequestTrait return null; } - protected function transformValue(array $attributeList, mixed $value, ) : mixed + protected function transformValue(array $valueModifierAttributes, mixed $value, ) : mixed { - $transforms = array_map(function($e) { - $instance = $e->newInstance(); - return $instance instanceof Attribute\PropertyValueModifier ? $e->newInstance() : null; - }, $attributeList); - - foreach(array_filter($transforms) as $transform) { - $value = $transform->run($value); + foreach($valueModifierAttributes as $transform) { + $value = $transform->object->run($value); } return $value;