- Still WIP on SearchRequest attributes migration
This commit is contained in:
parent
950df5eb99
commit
b38b81d03c
|
@ -324,7 +324,6 @@ trait EntityTrait {
|
||||||
#[Ignore]
|
#[Ignore]
|
||||||
public static function field($name, null|string|false $alias = Repository::DEFAULT_ALIAS) : EntityField
|
public static function field($name, null|string|false $alias = Repository::DEFAULT_ALIAS) : EntityField
|
||||||
{
|
{
|
||||||
|
|
||||||
$default = ( $alias === false ? '' : Repository::DEFAULT_ALIAS ); # bw compatibility, to be deprecated
|
$default = ( $alias === false ? '' : Repository::DEFAULT_ALIAS ); # bw compatibility, to be deprecated
|
||||||
|
|
||||||
return new EntityField(static::class, $name, $alias ? Ulmus::repository(static::class)->adapter->adapter()->escapeIdentifier($alias, Adapter\AdapterInterface::IDENTIFIER_FIELD) : $default, Ulmus::resolveEntity(static::class));
|
return new EntityField(static::class, $name, $alias ? Ulmus::repository(static::class)->adapter->adapter()->escapeIdentifier($alias, Adapter\AdapterInterface::IDENTIFIER_FIELD) : $default, Ulmus::resolveEntity(static::class));
|
||||||
|
@ -341,7 +340,7 @@ trait EntityTrait {
|
||||||
#[Ignore]
|
#[Ignore]
|
||||||
public static function searchRequest(...$arguments) : SearchRequestInterface
|
public static function searchRequest(...$arguments) : SearchRequestInterface
|
||||||
{
|
{
|
||||||
return new #[SearchRequest\Attribute\SearchRequestParameter(self::class)] class(... $arguments) extends SearchRequest\SearchRequest {
|
return new /* #[SearchRequest\Attribute\SearchRequestParameter(YourEntityClass::class)] */ class(... $arguments) extends SearchRequest\SearchRequest {
|
||||||
# Define searchable properties here, some ex:
|
# Define searchable properties here, some ex:
|
||||||
|
|
||||||
# #[SearchParameter(method: SearchMethodEnum::Where)]
|
# #[SearchParameter(method: SearchMethodEnum::Where)]
|
||||||
|
|
|
@ -8,8 +8,6 @@ use Ulmus\SearchRequest\SearchMethodEnum;
|
||||||
class SearchGroupBy extends SearchParameter
|
class SearchGroupBy extends SearchParameter
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public ? string $parameter = null,
|
|
||||||
public null|string|\Stringable|array $field = null,
|
public null|string|\Stringable|array $field = null,
|
||||||
public SearchMethodEnum $method = SearchMethodEnum::GroupBy,
|
|
||||||
) {}
|
) {}
|
||||||
}
|
}
|
|
@ -9,5 +9,6 @@ class SearchRequestParameter
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public string $class,
|
public string $class,
|
||||||
|
public ? string $alias = null,
|
||||||
) {}
|
) {}
|
||||||
}
|
}
|
|
@ -3,10 +3,8 @@
|
||||||
namespace Ulmus\SearchRequest;
|
namespace Ulmus\SearchRequest;
|
||||||
|
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Ulmus\SearchRequest\Attribute\SearchGroupBy;
|
|
||||||
use Ulmus\SearchRequest\Attribute\SearchOrderBy;
|
use Ulmus\SearchRequest\Attribute\{ SearchParameter, SearchWhere, SearchOrderBy, SearchGroupBy, SearchRequestParameter, };
|
||||||
use Ulmus\SearchRequest\Attribute\SearchParameter;
|
|
||||||
use Ulmus\SearchRequest\Attribute\SearchWhere;
|
|
||||||
|
|
||||||
trait SearchRequestFromRequestTrait
|
trait SearchRequestFromRequestTrait
|
||||||
{
|
{
|
||||||
|
@ -38,7 +36,20 @@ trait SearchRequestFromRequestTrait
|
||||||
$fieldName = $attribute->field ?? $propertyName;
|
$fieldName = $attribute->field ?? $propertyName;
|
||||||
$queryParamName = $attribute->parameter ?? $propertyName;
|
$queryParamName = $attribute->parameter ?? $propertyName;
|
||||||
|
|
||||||
$field = \Ulmus\Attribute\Attribute::handleArrayField($fieldName, false);;
|
# Field could be defined for another entity class
|
||||||
|
if (is_array($fieldName)) {
|
||||||
|
$field = \Ulmus\Attribute\Attribute::handleArrayField($fieldName, false);
|
||||||
|
}
|
||||||
|
# Default class using it, if SearchRequestParameter is defined
|
||||||
|
elseif ($classAttributes = $classReflection->getAttributes(SearchRequestParameter::class)) {
|
||||||
|
$searchRequestAttribute = $classAttributes[0]->newInstance();
|
||||||
|
$className = $searchRequestAttribute->class;
|
||||||
|
$field = $className::field($fieldName, $searchRequestAttribute->alias);
|
||||||
|
}
|
||||||
|
# Untouched string from the attribute
|
||||||
|
else {
|
||||||
|
$field = $fieldName;
|
||||||
|
}
|
||||||
|
|
||||||
$value = $queryParams->offsetExists($queryParamName) ? $this->transformValue($attributeList, $queryParams[$queryParamName]) : null;
|
$value = $queryParams->offsetExists($queryParamName) ? $this->transformValue($attributeList, $queryParams[$queryParamName]) : null;
|
||||||
|
|
||||||
|
@ -57,7 +68,10 @@ trait SearchRequestFromRequestTrait
|
||||||
$this->$propertyName = $value;
|
$this->$propertyName = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->parseAttributeDefaultOrder($attribute, $field, $propertyName);
|
$this->parseAttributeOrderBy($attribute, $field, $propertyName);
|
||||||
|
}
|
||||||
|
elseif ($attribute instanceof SearchGroupBy) {
|
||||||
|
$this->parseAttributeGroupBy($attribute, $field, $propertyName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -104,11 +118,18 @@ trait SearchRequestFromRequestTrait
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function parseAttributeDefaultOrder(object $attribute, string $field, mixed $propertyName,) : void
|
protected function parseAttributeOrderBy(object $attribute, string $field, mixed $propertyName,) : void
|
||||||
{
|
{
|
||||||
$this->orders[$field] = $this->$propertyName;
|
$this->orders[$field] = $this->$propertyName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function parseAttributeGroupBy(object $attribute, string $field, mixed $propertyName,) : void
|
||||||
|
{
|
||||||
|
if (! empty($this->$propertyName)) {
|
||||||
|
$this->groups[] = $field;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
# @TODO !
|
# @TODO !
|
||||||
/* protected function parseAttributeGroupBy(object $attribute, string $field, string $propertyName,) : void
|
/* protected function parseAttributeGroupBy(object $attribute, string $field, string $propertyName,) : void
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue