- WIP on SearchAttributes supporting Request's attributes
This commit is contained in:
1 parent
bd9078230d
commit
bbfd7c02b4
6 files changed
+70
-9
No files matched your search
@@ -2,9 +2,16 @@
|
||||
|
||||
namespace Ulmus\SearchRequest;
|
||||
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
use Ulmus\SearchRequest\Attribute\{ SearchParameter, SearchWhere, SearchLike, SearchOrderBy, SearchGroupBy, SearchRequestParameter, };
|
||||
use Ulmus\SearchRequest\Attribute\{PropertyValueSource,
|
||||
SearchParameter,
|
||||
SearchWhere,
|
||||
SearchLike,
|
||||
SearchOrderBy,
|
||||
SearchGroupBy,
|
||||
SearchRequestParameter};
|
||||
|
||||
trait SearchRequestFromRequestTrait
|
||||
{
|
||||
@@ -30,15 +37,12 @@ trait SearchRequestFromRequestTrait
|
||||
|
||||
foreach($classReflection->getProperties() as $property) {
|
||||
$attributeList = $property->getAttributes();
|
||||
|
||||
$attributeReflection = array_filter($attributeList, fn($e) => $e->newInstance() instanceof Attribute\SearchParameter);
|
||||
|
||||
if ($attributeReflection) {
|
||||
$attribute = $attributeReflection[0]->newInstance();
|
||||
|
||||
$propertyName = $property->getName();
|
||||
$fieldName = $attribute->field ?? $propertyName;
|
||||
$queryParamName = $attribute->parameter ?? $propertyName;
|
||||
|
||||
# Field could be defined for another entity class
|
||||
if (is_array($fieldName)) {
|
||||
@@ -55,11 +59,15 @@ trait SearchRequestFromRequestTrait
|
||||
$field = $fieldName;
|
||||
}
|
||||
|
||||
$value = $queryParams->offsetExists($queryParamName) ? $this->transformValue($attributeList, $queryParams[$queryParamName]) : null;
|
||||
$value = $this->getValueFromSource($request, $propertyName, $attribute);
|
||||
|
||||
if ($value !== null) {
|
||||
$value = $this->transformValue($attributeList, $value);
|
||||
}
|
||||
|
||||
if ($attribute instanceof SearchWhere || $attribute instanceof SearchLike) {
|
||||
if ($attribute->toggle) {
|
||||
$this->$propertyName = $queryParams->offsetExists($queryParamName);
|
||||
$this->$propertyName = ! empty($value);
|
||||
}
|
||||
elseif ($value !== null) {
|
||||
$this->$propertyName = $value;
|
||||
@@ -83,6 +91,35 @@ trait SearchRequestFromRequestTrait
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function getValueFromSource(RequestInterface $request, string $propertyName, SearchParameter $attribute) : mixed
|
||||
{
|
||||
$queryParamName = $attribute->parameter ?? $propertyName;
|
||||
|
||||
foreach($attribute->getSources() as $source) {
|
||||
switch($source) {
|
||||
case PropertyValueSource::QueryParams:
|
||||
$queryParams = new \ArrayObject(array_filter($request->getQueryParams(), function($i) { return ! is_null($i) && $i !== ""; }));
|
||||
|
||||
if ($queryParams->offsetExists($queryParamName)) {
|
||||
return $queryParams[$queryParamName];
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case PropertyValueSource::RequestAttribute:
|
||||
$fromAttribute = $request->getAttribute($queryParamName, null);
|
||||
|
||||
if ($fromAttribute !== null) {
|
||||
return $fromAttribute;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function transformValue(array $attributeList, mixed $value, ) : mixed
|
||||
{
|
||||
$transforms = array_map(function($e) {
|
||||
|
||||
Reference in new issue
Block a user