From 338b599d39726daa56f185f4ea577eb491da30f4 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Mon, 31 Jul 2023 14:28:19 -0400 Subject: [PATCH] - Added a default searchRequest() method to entityTrait --- src/EntityTrait.php | 54 ++++++++++++++++++++++++++++++- src/Migration/FieldDefinition.php | 23 ++++++++++--- 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/src/EntityTrait.php b/src/EntityTrait.php index bf2af7e..1879dea 100644 --- a/src/EntityTrait.php +++ b/src/EntityTrait.php @@ -3,7 +3,12 @@ namespace Ulmus; use Notes\Attribute\Ignore; -use Ulmus\{ Repository, Query, Common\EntityResolver, Common\EntityField }; +use Psr\Http\Message\ServerRequestInterface; +use Ulmus\{ + Common\EntityResolver, + Common\EntityField, + SearchRequest\SearchRequestInterface, + SearchRequest\SearchRequestPaginationTrait}; use Ulmus\Annotation\Classes\{ Method, Table, Collation, }; use Ulmus\Annotation\Property\{ Field, Filter, FilterJoin, Relation, OrderBy, Where, OrWhere, Join, Virtual, On, WithJoin, }; use Ulmus\Annotation\Property\Field\{ PrimaryKey, Id, ForeignKey, CreatedAt, UpdatedAt, Datetime as DateTime, Date, Time, Bigint, Tinyint, Text, Mediumtext, Longtext, Blob, Mediumblob, Longblob, }; @@ -313,4 +318,51 @@ trait EntityTrait { return static::field($item, $alias); }, $fields)); } + + #[Ignore] + public static function searchRequest(...$arguments) : SearchRequestInterface + { + return new class() implements SearchRequestInterface + { + use SearchRequestPaginationTrait; + + public function fromRequest(ServerRequestInterface $request) : self + { + $get = new \ArrayObject(array_filter($request->getQueryParams(), function($i) { return $i !== ""; })); + + $this->page = $get->offsetExists('page') ? $get['page'] : 1; + $this->limit = 100; + + return $this; + } + + public function filter(Repository $repository) : Repository + { + return $repository; + } + + public function wheres() : iterable + { + return array_filter([ + ], fn($i) => ! is_null($i) ) + [ ]; + } + + public function likes(): iterable + { + return array_filter([ + ], fn($i) => ! is_null($i) ) + []; + } + + public function groups(): iterable + { + return []; + } + + public function orders(): iterable + { + return array_filter([ + ], fn($e) => ! is_null($e) && $e !== "" ); + } + }; + } } diff --git a/src/Migration/FieldDefinition.php b/src/Migration/FieldDefinition.php index 449ae83..7a67969 100644 --- a/src/Migration/FieldDefinition.php +++ b/src/Migration/FieldDefinition.php @@ -19,7 +19,7 @@ class FieldDefinition { public array $tags; - public /* ? string */ $default; + public mixed $default; public ? int $precision; @@ -37,6 +37,10 @@ class FieldDefinition { $this->builtIn = $data['builtin']; $this->tags = $data['tags']; + if (isset($data['value'])) { + $this->default = $data['value']; + } + $field = $this->getFieldTag(); $adapter->whitelistAttributes($field->attributes); @@ -66,7 +70,7 @@ class FieldDefinition { $this->isUnsigned() ? $syntax['unsigned'] : null, $this->nullable ? "NULL" : "NOT NULL", $this->update ? "ON UPDATE {$this->update}" : null, - $default ? "DEFAULT $default" : null, + $default !== null ? "DEFAULT $default" : null, $this->isPrimaryKey() ? $syntax['pk'] : null, $this->isAutoIncrement() ? $syntax['ai'] : null, ])); @@ -84,9 +88,20 @@ class FieldDefinition { return $this->getFieldTag()->name ?? $this->name; } - public function getDefault() : ? string + public function getDefault() : mixed { - return $this->getFieldTag()->attributes['default'] ?? ( $this->nullable ? "NULL" : null ) ; + if ( isset($this->default) ) { + if (is_bool($this->default)) { + return (int) $this->default; + } + + # Specific default cases here .. + + return $this->default; + } + + # Fallback on attribute's default value + return $this->getFieldTag()->default ?? ( $this->nullable ? "NULL" : null ) ; } public function isPrimaryKey() : bool