Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC: add filter description #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 54 additions & 3 deletions src/Filter/FilterLogic.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Metaclass\FilterBundle\Filter;

use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Api\FilterCollection;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\AbstractContextAwareFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ContextAwareFilterInterface;
Expand All @@ -14,6 +15,8 @@
use Doctrine\Persistence\ManagerRegistry;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use ReflectionAttribute;
use ReflectionClass;
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
use Doctrine\ORM\Query\Expr\Join;

Expand Down Expand Up @@ -60,8 +63,30 @@ public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFa
/** {@inheritdoc } */
public function getDescription(string $resourceClass): array
{
// No description
return [];
$description = [];
$reflectionClass = new \ReflectionClass($resourceClass);

// Does the resource have the filter? #[ApiFilter(FilterLogic::class)] ?
if (false === $this->isFilterLogicFilter($reflectionClass)) {
return $description;
}

// Does the resource have the filter with properties? #[ApiFilter(SearchFilter::class)]
$properties = $this->getPropertiesFromSearchFilter($reflectionClass);
if (null === $properties) {
return $description;
}

foreach ($properties as $key => $value) {
$description['and[or]['.$key.']'] = [
'description' => 'andOr filter',
'type' => 'string',
'required' => false,
];
// todo add all filters here and or not ...
}

return $description;
}

/**
Expand Down Expand Up @@ -301,4 +326,30 @@ protected function replaceInnerJoinsByLeftJoins(QueryBuilder $queryBuilder) {
}
$queryBuilder->add('join', $result);
}
}

private function getPropertiesFromSearchFilter(\ReflectionClass $class): ?array
{
$apiFilterAttributes = $class->getAttributes('ApiPlatform\Core\Annotation\ApiFilter', ReflectionAttribute::IS_INSTANCEOF);

foreach ($apiFilterAttributes as $filterAttribute) {
if ('ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter' === $filterAttribute->newInstance()->filterClass) {
return $filterAttribute->newInstance()->properties;
}
}

return null;
}

private function isFilterLogicFilter(\ReflectionClass $class): bool
{
$apiFilterAttributes = $class->getAttributes('ApiPlatform\Core\Annotation\ApiFilter', ReflectionAttribute::IS_INSTANCEOF);

foreach ($apiFilterAttributes as $filterAttribute) {
if ('Metaclass\FilterBundle\Filter\FilterLogic' === $filterAttribute->newInstance()->filterClass) {
return true;
}
}

return false;
}
}