|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace EasyCorp\Bundle\EasyAdminBundle\Filter; |
| 4 | + |
| 5 | +use Doctrine\ORM\QueryBuilder; |
| 6 | +use Doctrine\Persistence\ObjectManager; |
| 7 | +use EasyCorp\Bundle\EasyAdminBundle\Contracts\Filter\FilterInterface; |
| 8 | +use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto; |
| 9 | +use EasyCorp\Bundle\EasyAdminBundle\Dto\FieldDto; |
| 10 | +use EasyCorp\Bundle\EasyAdminBundle\Dto\FilterDataDto; |
| 11 | + |
| 12 | +/** |
| 13 | + * @author Brandon Marcachi <brandon.marcachi@gmail.com> |
| 14 | + */ |
| 15 | +final class NestedFilter implements FilterInterface |
| 16 | +{ |
| 17 | + use FilterTrait; |
| 18 | + |
| 19 | + public const FORM_OPTION_WRAPPED_FILTER = 'attr.wrapped_filter'; |
| 20 | + |
| 21 | + public const PATH_SEPARATOR_EXPECTED = '.'; |
| 22 | + public const PATH_SEPARATOR = '_'; |
| 23 | + |
| 24 | + /** @var FilterInterface */ |
| 25 | + private $wrappedFilter; |
| 26 | + |
| 27 | + public static function new(string $propertyName, string $label = null): self |
| 28 | + { |
| 29 | + throw new \RuntimeException('Instead of this method, use the "wrap()" method.'); |
| 30 | + } |
| 31 | + |
| 32 | + public static function wrap(FilterInterface $filter): FilterInterface |
| 33 | + { |
| 34 | + $filterDto = $filter->getAsDto(); |
| 35 | + $property = $filterDto->getProperty(); |
| 36 | + |
| 37 | + if (false === strpos($property, self::PATH_SEPARATOR_EXPECTED)) { |
| 38 | + return $filter; |
| 39 | + } |
| 40 | + |
| 41 | + return (new self()) |
| 42 | + ->setFilterFqcn(__CLASS__) |
| 43 | + ->setProperty($property) |
| 44 | + ->setFormType($filterDto->getFormType()) |
| 45 | + ->setFormTypeOptions($filterDto->getFormTypeOptions()) |
| 46 | + ->setWrappedFilter($filter) |
| 47 | + ; |
| 48 | + } |
| 49 | + |
| 50 | + public function apply(QueryBuilder $queryBuilder, FilterDataDto $filterDataDto, ?FieldDto $fieldDto, EntityDto $entityDto): void |
| 51 | + { |
| 52 | + $propertyPath = $filterDataDto->getProperty(); |
| 53 | + |
| 54 | + [$targetClassMetadata, $targetProperty] = self::extractTargets( |
| 55 | + $queryBuilder->getEntityManager(), |
| 56 | + $entityDto->getFqcn(), |
| 57 | + $propertyPath |
| 58 | + ); |
| 59 | + |
| 60 | + $wrappedEntityDto = new EntityDto($targetClassMetadata->getName(), $targetClassMetadata); |
| 61 | + $wrappedFilter = $this->getWrappedFilter(); |
| 62 | + $wrappedFilterDto = $wrappedFilter->getAsDto(); |
| 63 | + $wrappedFilterDto->setProperty($targetProperty); |
| 64 | + |
| 65 | + // Apply required left joins and get the alias we have to work with |
| 66 | + $alias = $this->applyLeftJoins($queryBuilder, $filterDataDto->getEntityAlias(), $propertyPath); |
| 67 | + |
| 68 | + // Recreate FilterDataDto adapted for the wrapped filter |
| 69 | + $wrappedFilterDataDto = FilterDataDto::new($filterDataDto->getIndex(), $wrappedFilterDto, $alias, [ |
| 70 | + 'value' => $filterDataDto->getValue(), |
| 71 | + 'value2' => $filterDataDto->getValue2(), |
| 72 | + 'comparison' => $filterDataDto->getComparison(), |
| 73 | + ]); |
| 74 | + |
| 75 | + $wrappedFilterDto->apply($queryBuilder, $wrappedFilterDataDto, null, $wrappedEntityDto); |
| 76 | + } |
| 77 | + |
| 78 | + public static function extractTargets(ObjectManager $objectManager, string $class, string $propertyPath): array |
| 79 | + { |
| 80 | + $segments = explode(self::PATH_SEPARATOR, $propertyPath); |
| 81 | + $metadata = $objectManager->getClassMetadata($class); |
| 82 | + $lastIndex = \count($segments) - 1; |
| 83 | + $property = null; |
| 84 | + |
| 85 | + foreach ($segments as $i => $prop) { |
| 86 | + if (!$metadata->hasField($prop) && !$metadata->hasAssociation($prop)) { |
| 87 | + self::throwInvalidPropertyPathException($propertyPath, $class); |
| 88 | + } |
| 89 | + |
| 90 | + // The target property must be at the end of path |
| 91 | + if ($i === $lastIndex) { |
| 92 | + $property = $prop; |
| 93 | + break; |
| 94 | + } |
| 95 | + |
| 96 | + if (!$metadata->hasAssociation($prop)) { |
| 97 | + self::throwInvalidPropertyPathException($propertyPath, $class); |
| 98 | + } |
| 99 | + |
| 100 | + // Move to next nested class |
| 101 | + $metadata = $objectManager->getClassMetadata($metadata->getAssociationTargetClass($prop)); |
| 102 | + } |
| 103 | + |
| 104 | + return [$metadata, $property]; |
| 105 | + } |
| 106 | + |
| 107 | + public function setWrappedFilter(FilterInterface $filter): self |
| 108 | + { |
| 109 | + $this->wrappedFilter = $filter; |
| 110 | + |
| 111 | + return $this->setFormTypeOption(self::FORM_OPTION_WRAPPED_FILTER, $filter); |
| 112 | + } |
| 113 | + |
| 114 | + public function getWrappedFilter(): FilterInterface |
| 115 | + { |
| 116 | + return $this->wrappedFilter; |
| 117 | + } |
| 118 | + |
| 119 | + public function setProperty(string $propertyName): self |
| 120 | + { |
| 121 | + // Replace dots with underscore to avoid errors |
| 122 | + $this->dto->setProperty( |
| 123 | + str_replace(self::PATH_SEPARATOR_EXPECTED, self::PATH_SEPARATOR, $propertyName) |
| 124 | + ); |
| 125 | + |
| 126 | + return $this; |
| 127 | + } |
| 128 | + |
| 129 | + private function applyLeftJoins(QueryBuilder $qb, string $alias, string $propertyPath): string |
| 130 | + { |
| 131 | + $path = explode(self::PATH_SEPARATOR, $propertyPath); |
| 132 | + $lastIndex = \count($path) - 1; |
| 133 | + $currentAlias = $alias; |
| 134 | + |
| 135 | + foreach ($path as $i => $prop) { |
| 136 | + if ($i === $lastIndex) { |
| 137 | + break; |
| 138 | + } |
| 139 | + |
| 140 | + $nextAlias = sprintf('%s_%s', $currentAlias, $prop); |
| 141 | + if (!\in_array($nextAlias, $qb->getAllAliases(), true)) { |
| 142 | + $qb->leftJoin(sprintf('%s.%s', $currentAlias, $prop), $nextAlias); |
| 143 | + } |
| 144 | + |
| 145 | + $currentAlias = $nextAlias; |
| 146 | + } |
| 147 | + |
| 148 | + return $currentAlias; |
| 149 | + } |
| 150 | + |
| 151 | + private static function throwInvalidPropertyPathException(string $propertyPath, string $class): void |
| 152 | + { |
| 153 | + throw new \InvalidArgumentException(sprintf( |
| 154 | + 'The property path "%s" for class "%s" is invalid.', |
| 155 | + str_replace(self::PATH_SEPARATOR, self::PATH_SEPARATOR_EXPECTED, $propertyPath), |
| 156 | + $class |
| 157 | + )); |
| 158 | + } |
| 159 | +} |
0 commit comments