1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arp\DoctrineQueryFilter\Filter; |
6
|
|
|
|
7
|
|
|
use Arp\DoctrineQueryFilter\Filter\Exception\InvalidArgumentException; |
8
|
|
|
use Arp\DoctrineQueryFilter\Metadata\Exception\MetadataException; |
9
|
|
|
use Arp\DoctrineQueryFilter\Metadata\MetadataInterface; |
10
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
11
|
|
|
use Doctrine\ORM\Query\Expr; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author Alex Patterson <[email protected]> |
15
|
|
|
* @package Arp\DoctrineQueryFilter\Filter |
16
|
|
|
*/ |
17
|
|
|
final class IsMemberOf extends AbstractExpression |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param Expr $expr |
21
|
|
|
* @param string $fieldName |
22
|
|
|
* @param string $parameterName |
23
|
|
|
* @param string $alias |
24
|
|
|
* |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
protected function createExpression(Expr $expr, string $fieldName, string $parameterName, string $alias): string |
28
|
|
|
{ |
29
|
|
|
return (string)$expr->isMemberOf(':' . $parameterName, $alias . '.' . $fieldName); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param MetadataInterface $metadata |
34
|
|
|
* @param array<mixed> $criteria |
35
|
|
|
* @param string $key |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
* |
39
|
|
|
* @throws InvalidArgumentException |
40
|
|
|
* @throws MetadataException |
41
|
|
|
*/ |
42
|
|
|
protected function resolveFieldName(MetadataInterface $metadata, array $criteria, string $key = 'field'): string |
43
|
|
|
{ |
44
|
|
|
$fieldName = parent::resolveFieldName($metadata, $criteria, $key); |
45
|
|
|
|
46
|
|
|
if ($metadata->hasAssociation($fieldName)) { |
47
|
|
|
$associationType = $metadata->getAssociationMapping($fieldName)['type'] ?? ''; |
48
|
|
|
|
49
|
|
|
if (!empty($associationType) && !($associationType & ClassMetadata::TO_ONE)) { |
50
|
|
|
return $fieldName; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
throw new InvalidArgumentException( |
54
|
|
|
sprintf( |
55
|
|
|
'Unable to apply query filter \'%s\': ' |
56
|
|
|
. 'The field \'%s\' is not a collection valued association', |
57
|
|
|
self::class, |
58
|
|
|
$fieldName |
59
|
|
|
) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
throw new InvalidArgumentException( |
64
|
|
|
sprintf( |
65
|
|
|
'Unable to apply query filter \'%s\': ' |
66
|
|
|
. 'The entity class \'%s\' has no association named \'%s\'', |
67
|
|
|
self::class, |
68
|
|
|
$metadata->getName(), |
69
|
|
|
$fieldName |
70
|
|
|
) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|