-
-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b42dfa5
commit 87f62bb
Showing
53 changed files
with
1,273 additions
and
698 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Doctrine\Orm\Filter; | ||
|
||
use ApiPlatform\Doctrine\Orm\Filter\AbstractFilter; | ||
use ApiPlatform\Doctrine\Orm\PropertyHelperTrait; | ||
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface; | ||
use ApiPlatform\Exception\InvalidArgumentException; | ||
use ApiPlatform\Metadata\Operation; | ||
use Doctrine\ORM\QueryBuilder; | ||
|
||
final class NameFilter extends AbstractFilter | ||
{ | ||
use PropertyHelperTrait; | ||
|
||
public function getDescription(string $resourceClass): array | ||
{ | ||
return [ | ||
'name' => [ | ||
'property' => 'name', | ||
'type' => 'string', | ||
'required' => false, | ||
'strategy' => 'ipartial', | ||
'is_collection' => false, | ||
], | ||
]; | ||
} | ||
|
||
protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, Operation $operation = null, array $context = []): void | ||
{ | ||
if ('name' !== $property) { | ||
return; | ||
} | ||
|
||
$values = $this->normalizeValues($value, $property); | ||
if (null === $values) { | ||
return; | ||
} | ||
|
||
$alias = $queryBuilder->getRootAliases()[0]; | ||
$expressions = []; | ||
foreach ($values as $key => $value) { | ||
$parameterName = $queryNameGenerator->generateParameterName("name$key"); | ||
$queryBuilder->setParameter($parameterName, "%$value%"); | ||
$expressions[] = $queryBuilder->expr()->orX( | ||
$queryBuilder->expr()->like(sprintf('%s.firstName', $alias), ":$parameterName"), | ||
$queryBuilder->expr()->like(sprintf('%s.lastName', $alias), ":$parameterName") | ||
); | ||
} | ||
$queryBuilder->andWhere($queryBuilder->expr()->andX(...$expressions)); | ||
} | ||
|
||
protected function normalizeValues($value, string $property): ?array | ||
{ | ||
if (!\is_string($value) || empty(trim($value))) { | ||
return null; | ||
} | ||
|
||
$values = explode(' ', $value); | ||
foreach ($values as $key => $value) { | ||
if (empty(trim($value))) { | ||
unset($values[$key]); | ||
} | ||
} | ||
|
||
if (empty($values)) { | ||
$this->getLogger()->notice('Invalid filter ignored', [ | ||
'exception' => new InvalidArgumentException(sprintf('At least one value is required, multiple values should be in "%1$s[]=firstvalue&%1$s[]=secondvalue" format', $property)), | ||
]); | ||
|
||
return null; | ||
} | ||
|
||
return array_values($values); | ||
} | ||
} |
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
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
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
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
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
Oops, something went wrong.