Skip to content

Instantly share code, notes, and snippets.

@teohhanhui
Created July 8, 2019 10:36
Show Gist options
  • Save teohhanhui/6d77c2ef4cbdde7ecbee9b314ad27281 to your computer and use it in GitHub Desktop.
Save teohhanhui/6d77c2ef4cbdde7ecbee9b314ad27281 to your computer and use it in GitHub Desktop.
<?php
namespace App\Filter;
use ApiPlatform\Core\Api\IriConverterInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\QueryBuilder;
use Psr\Log\LoggerInterface;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
/**
* Filters the collection by match, using a property alias.
*/
class PropertyAliasSearchFilter extends SearchFilter
{
private $aliasedProperties;
public function __construct(ManagerRegistry $managerRegistry, array $properties, IriConverterInterface $iriConverter, PropertyAccessorInterface $propertyAccessor = null, LoggerInterface $logger = null)
{
$this->aliasedProperties = $properties;
$properties = [];
foreach ($this->aliasedProperties as $property => $propertyOptions) {
$properties[$propertyOptions['propertyName']] = $propertyOptions['strategy'];
}
parent::__construct($managerRegistry, null, $iriConverter, $propertyAccessor, $logger, $properties);
}
/**
* {@inheritdoc}
*/
public function getDescription(string $resourceClass): array
{
$description = [];
$properties = $this->aliasedProperties;
foreach ($properties as $property => $propertyOptions) {
$description[$property] = [
'property' => $propertyOptions['propertyName'],
'type' => 'string',
'required' => false,
'strategy' => $propertyOptions['strategy'],
];
}
return $description;
}
/**
* {@inheritdoc}
*/
protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
{
if (!isset($this->aliasedProperties[$property])) {
return;
}
$property = $this->aliasedProperties[$property]['propertyName'];
parent::filterProperty($property, $value, $queryBuilder, $queryNameGenerator, $resourceClass, $operationName);
}
}
@nicotec
Copy link

nicotec commented Aug 31, 2022

Hello,

much simpler !!!

?page=1&firstname[exact]=mick

use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use Doctrine\ORM\QueryBuilder;

class SearchStrategyFilter extends SearchFilter
{
    protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
    {
        if (!is_array($value)) {
            return;
        }

        $first = array_key_first($value);

        if (!in_array($first, [
            self::STRATEGY_EXACT,
            self::STRATEGY_PARTIAL,
            self::STRATEGY_START,
            self::STRATEGY_END,
            self::STRATEGY_WORD_START,
        ])) {
            return;
        }

        $this->properties = [$property => $first];
        $value = $value[$first];

        parent::filterProperty($property, $value, $queryBuilder, $queryNameGenerator, $resourceClass, $operationName);
   }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment