- Continued work on SearchRequest migration and fixed some more QueryBuilder migration too
This commit is contained in:
1 parent
9fdebf454d
commit
950df5eb99
15 files changed
+122
-48
No files matched your search
@@ -3,6 +3,8 @@
|
||||
namespace Ulmus\SearchRequest;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Ulmus\SearchRequest\Attribute\SearchGroupBy;
|
||||
use Ulmus\SearchRequest\Attribute\SearchOrderBy;
|
||||
use Ulmus\SearchRequest\Attribute\SearchParameter;
|
||||
use Ulmus\SearchRequest\Attribute\SearchWhere;
|
||||
|
||||
@@ -18,43 +20,71 @@ trait SearchRequestFromRequestTrait
|
||||
|
||||
public function fromRequest(ServerRequestInterface $request)
|
||||
{
|
||||
$get = new \ArrayObject(array_filter($request->getQueryParams(), function($i) { return $i !== ""; }));
|
||||
$queryParams = new \ArrayObject(array_filter($request->getQueryParams(), function($i) { return ! is_null($i) && $i !== ""; }));
|
||||
|
||||
$this->page = $get->offsetExists('page') ? $get['page'] : 1;
|
||||
$this->page = $queryParams->offsetExists('page') ? $queryParams['page'] : 1;
|
||||
|
||||
$classReflection = new \ReflectionClass($this);
|
||||
|
||||
foreach($classReflection->getProperties() as $property) {
|
||||
$attributeList = $property->getAttributes();
|
||||
|
||||
foreach($property->getAttributes() as $attributeReflection) {
|
||||
$attribute = $attributeReflection->newInstance();
|
||||
$attributeReflection = array_filter($attributeList, fn($e) => $e->newInstance() instanceof Attribute\SearchParameter);
|
||||
|
||||
# We can't simply pass this class as first arguments of getAttributes() since it do not check for inheritance
|
||||
if (! $attribute instanceof Attribute\SearchParameter) {
|
||||
continue;
|
||||
if ($attributeReflection) {
|
||||
$attribute = $attributeReflection[0]->newInstance();
|
||||
|
||||
$propertyName = $property->getName();
|
||||
$fieldName = $attribute->field ?? $propertyName;
|
||||
$queryParamName = $attribute->parameter ?? $propertyName;
|
||||
|
||||
$field = \Ulmus\Attribute\Attribute::handleArrayField($fieldName, false);;
|
||||
|
||||
$value = $queryParams->offsetExists($queryParamName) ? $this->transformValue($attributeList, $queryParams[$queryParamName]) : null;
|
||||
|
||||
if ($attribute instanceof SearchWhere) {
|
||||
if ($attribute->toggle) {
|
||||
$this->$propertyName = $queryParams->offsetExists($queryParamName);
|
||||
}
|
||||
elseif ($value !== null) {
|
||||
$this->$propertyName = $value;
|
||||
}
|
||||
|
||||
$this->parseAttributeMethod($attribute, $field, $propertyName);
|
||||
}
|
||||
elseif ($attribute instanceof SearchOrderBy) {
|
||||
if ($value !== null) {
|
||||
$this->$propertyName = $value;
|
||||
}
|
||||
|
||||
$propertyName = $property->getName();
|
||||
$fieldName = $attribute->field ?? $property->getName();
|
||||
$queryParamName = $attribute->parameter ?? $property->getName();
|
||||
|
||||
if ($attribute->toggle) {
|
||||
$this->$propertyName = $get->offsetExists('rental');
|
||||
} else {
|
||||
$this->$propertyName = $get->offsetExists($queryParamName) ? $get[$queryParamName] : null;
|
||||
$this->parseAttributeDefaultOrder($attribute, $field, $propertyName);
|
||||
}
|
||||
|
||||
$field = (string) $fieldName;
|
||||
|
||||
$this->parseAttributeMethod($attribute, $field, $propertyName);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function transformValue(array $attributeList, 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);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
protected function parseAttributeMethod(object $attribute, string $field, string $propertyName,) : void
|
||||
{
|
||||
if (! isset($this->$propertyName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch ($attribute->method) {
|
||||
case SearchMethodEnum::Where:
|
||||
$this->wheres[$field] = $this->$propertyName;
|
||||
@@ -71,18 +101,21 @@ trait SearchRequestFromRequestTrait
|
||||
case SearchMethodEnum::LikeRight:
|
||||
$this->likes[$field] = "{$this->$propertyName}%";
|
||||
break;
|
||||
|
||||
case SearchMethodEnum::OrderByAsc:
|
||||
$this->orders[$field] = "ASC";
|
||||
break;
|
||||
|
||||
case SearchMethodEnum::OrderByDesc:
|
||||
$this->orders[$field] = "DESC";
|
||||
break;
|
||||
|
||||
case SearchMethodEnum::GroupBy:
|
||||
$this->groups[$field] = "DESC";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected function parseAttributeDefaultOrder(object $attribute, string $field, mixed $propertyName,) : void
|
||||
{
|
||||
$this->orders[$field] = $this->$propertyName;
|
||||
}
|
||||
|
||||
# @TODO !
|
||||
/* protected function parseAttributeGroupBy(object $attribute, string $field, string $propertyName,) : void
|
||||
{
|
||||
switch ($attribute->order) {
|
||||
case SearchMethodEnum::GroupBy:
|
||||
$this->groups[$field];
|
||||
break;
|
||||
}
|
||||
}*/
|
||||
}
|
||||
Reference in new issue
Block a user