-
-
Save teohhanhui/6d77c2ef4cbdde7ecbee9b314ad27281 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
much simpler !!!
?page=1&firstname[exact]=mick