- WIP on SearchAttributes supporting Request's attributes

This commit is contained in:
Dave Mc Nicoll 2024-06-04 13:33:48 +00:00
parent bd9078230d
commit bbfd7c02b4
6 changed files with 70 additions and 9 deletions

View File

@ -237,7 +237,7 @@ class EntityResolver {
public function getAttributeImplementing(string $interface) : null|object
{
foreach (array_reverse($this->reflectedClass->getAttributes(true)) as $item) {
foreach ($this->reflectedClass->getAttributes(true) as $item) {
if ($item->object instanceof $interface) {
return $item->object;
}

View File

@ -0,0 +1,14 @@
<?php
namespace Ulmus\SearchRequest\Attribute;
enum PropertyValueSource
{
case QueryParams;
case RequestAttribute;
public const ATTRIBUTE_FIRST = [ self::RequestAttribute, self::QueryParams ];
public const QUERY_PARAMS_FIRST = [ self::QueryParams, self::RequestAttribute ];
}

View File

@ -13,5 +13,6 @@ class SearchLike extends SearchParameter
public bool $toggle = false,
public SearchMethodEnum $method = SearchMethodEnum::Like,
public string $description = "",
public PropertyValueSource|array $source = PropertyValueSource::QueryParams,
) {}
}

View File

@ -5,8 +5,16 @@ namespace Ulmus\SearchRequest\Attribute;
use Ulmus\SearchRequest\SearchMethodEnum;
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class SearchParameter
{
abstract class SearchParameter {
public PropertyValueSource|array $source = PropertyValueSource::QueryParams;
public null|string|\Stringable|array $field = null;
public string $description = "";
public function getSources() : array
{
return is_array($this->source) ? $this->source : [ $this->source ];
}
}

View File

@ -13,5 +13,6 @@ class SearchWhere extends SearchParameter
public bool $toggle = false,
public SearchMethodEnum $method = SearchMethodEnum::Where,
public string $description = "",
public PropertyValueSource|array $source = PropertyValueSource::QueryParams,
) {}
}

View File

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