- Added a default searchRequest() method to entityTrait

This commit is contained in:
Dave M. 2023-07-31 14:28:19 -04:00
parent d01624b8aa
commit 338b599d39
2 changed files with 72 additions and 5 deletions

View File

@ -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 !== "" );
}
};
}
}

View File

@ -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