| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- declare(strict_types=1);
- namespace App\Doctrine;
- use ApiPlatform\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
- use ApiPlatform\Doctrine\Orm\Extension\QueryItemExtensionInterface;
- use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
- use ApiPlatform\Metadata\Operation;
- use Doctrine\ORM\QueryBuilder;
- /**
- * Base class for custom doctrine extensions.
- */
- abstract class AbstractExtension implements QueryCollectionExtensionInterface, QueryItemExtensionInterface
- {
- /**
- * Called by doctrine when getting a collection.
- *
- * @param mixed[] $context
- *
- * @throws \Exception
- */
- public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
- {
- $this->apply($queryBuilder, $resourceClass, $operation);
- }
- /**
- * Called by doctrine when getting an item.
- *
- * @param array<string, mixed> $identifiers
- * @param mixed[] $context
- *
- * @throws \Exception
- */
- public function applyToItem(
- QueryBuilder $queryBuilder,
- QueryNameGeneratorInterface $queryNameGenerator,
- string $resourceClass,
- array $identifiers,
- ?Operation $operation = null,
- array $context = []
- ): void
- {
- $this->apply($queryBuilder, $resourceClass, $operation);
- }
- /**
- * Generic application of the extension.
- */
- protected function apply(QueryBuilder $queryBuilder, string $resourceClass, ?Operation $operation): void
- {
- if (!$this->supports($resourceClass, $operation)) {
- return;
- }
- $this->addWhere($queryBuilder, $resourceClass, $operation);
- }
- /**
- * Returns true if the extension supports the given resource.
- */
- abstract protected function supports(string $resourceClass, ?Operation $operation): bool;
- /**
- * Add one or more filters to the query.
- */
- abstract protected function addWhere(QueryBuilder $queryBuilder, string $resourceClass, ?Operation $operation): void;
- }
|