|
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\MetadataInterface; |
|
9
|
|
|
use Arp\DoctrineQueryFilter\QueryFilterManager; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @author Alex Patterson <[email protected]> |
|
13
|
|
|
* @package Arp\DoctrineQueryFilter\Filter |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class AbstractFilter implements FilterInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var QueryFilterManager |
|
19
|
|
|
*/ |
|
20
|
|
|
protected QueryFilterManager $queryFilterManager; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param QueryFilterManager $queryFilterManager |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(QueryFilterManager $queryFilterManager) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->queryFilterManager = $queryFilterManager; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Create a new unique parameter name |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $prefix |
|
34
|
|
|
* |
|
35
|
|
|
* @return string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function createParamName(string $prefix = ''): string |
|
38
|
|
|
{ |
|
39
|
|
|
return uniqid($prefix, true); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param MetadataInterface $metadata |
|
44
|
|
|
* @param array $criteria |
|
45
|
|
|
* @param string $key |
|
46
|
|
|
* |
|
47
|
|
|
* @return string |
|
48
|
|
|
* |
|
49
|
|
|
* @throws InvalidArgumentException |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function resolveFieldName(MetadataInterface $metadata, array $criteria, string $key = 'field'): string |
|
52
|
|
|
{ |
|
53
|
|
|
if (empty($criteria[$key])) { |
|
54
|
|
|
throw new InvalidArgumentException( |
|
55
|
|
|
sprintf( |
|
56
|
|
|
'The required \'%s\' criteria value is missing for filter \'%s\'', |
|
57
|
|
|
$key, |
|
58
|
|
|
static::class |
|
59
|
|
|
) |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (!$metadata->hasField($criteria[$key]) && !$metadata->hasAssociation($criteria[$key])) { |
|
64
|
|
|
throw new InvalidArgumentException( |
|
65
|
|
|
sprintf( |
|
66
|
|
|
'Unable to apply query filter \'%s\': ' |
|
67
|
|
|
. 'The entity class \'%s\' has no field or association named \'%s\'', |
|
68
|
|
|
static::class, |
|
69
|
|
|
$metadata->getName(), |
|
70
|
|
|
$criteria[$key] |
|
71
|
|
|
) |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $criteria[$key]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param MetadataInterface $metadata |
|
80
|
|
|
* @param string $fieldName |
|
81
|
|
|
* @param mixed $value |
|
82
|
|
|
* @param string|null $format |
|
83
|
|
|
* |
|
84
|
|
|
* @return mixed |
|
85
|
|
|
* |
|
86
|
|
|
* @noinspection PhpUnusedParameterInspection |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function formatValue(MetadataInterface $metadata, string $fieldName, $value, ?string $format = null) |
|
|
|
|
|
|
89
|
|
|
{ |
|
90
|
|
|
return $value; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.