1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arp\LaminasDoctrine\Query\Filter; |
6
|
|
|
|
7
|
|
|
use Arp\LaminasDoctrine\Query\Exception\InvalidArgumentException; |
8
|
|
|
use Arp\LaminasDoctrine\Query\Metadata\MetadataInterface; |
9
|
|
|
use Arp\LaminasDoctrine\Query\QueryFilterManager; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Alex Patterson <[email protected]> |
13
|
|
|
* @package Arp\LaminasDoctrine\Query\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
|
|
|
* @param MetadataInterface $metadata |
32
|
|
|
* @param array $criteria |
33
|
|
|
* @param string $key |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
protected function resolveFieldName(MetadataInterface $metadata, array $criteria, string $key): string |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
if (empty($criteria[$key])) { |
40
|
|
|
throw new InvalidArgumentException( |
41
|
|
|
sprintf('The required \'%s\' is missing', $key) |
42
|
|
|
); |
43
|
|
|
} |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param MetadataInterface $metadata |
48
|
|
|
* @param string $fieldName |
49
|
|
|
* @param string $entityName |
50
|
|
|
* |
51
|
|
|
* @throws InvalidArgumentException |
52
|
|
|
*/ |
53
|
|
|
protected function validateFieldName(MetadataInterface $metadata, string $fieldName, string $entityName): void |
54
|
|
|
{ |
55
|
|
|
if (!$metadata->hasField($fieldName) && !$metadata->hasAssociation($fieldName)) { |
56
|
|
|
throw new InvalidArgumentException( |
57
|
|
|
sprintf( |
58
|
|
|
'Unable to apply query filter \'%s\': ' |
59
|
|
|
. 'The entity class \'%s\' has no field or association named \'%s\'', |
60
|
|
|
static::class, |
61
|
|
|
$entityName, |
62
|
|
|
$fieldName |
63
|
|
|
) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $fieldName |
70
|
|
|
* @param array $criteria |
71
|
|
|
* |
72
|
|
|
* @return mixed |
73
|
|
|
* |
74
|
|
|
* @throws InvalidArgumentException |
75
|
|
|
*/ |
76
|
|
|
protected function resolveValue(string $fieldName, array $criteria) |
77
|
|
|
{ |
78
|
|
|
if (!array_key_exists('value', $criteria)) { |
79
|
|
|
throw new InvalidArgumentException( |
80
|
|
|
sprintf( |
81
|
|
|
'The required \'value\' criteria value is missing for filter \'%s::%s\'', |
82
|
|
|
static::class, |
83
|
|
|
$fieldName |
84
|
|
|
) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
return $criteria['value']; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param MetadataInterface $metadata |
92
|
|
|
* @param string $fieldName |
93
|
|
|
* @param mixed $value |
94
|
|
|
* @param string|null $format |
95
|
|
|
* |
96
|
|
|
* @return mixed |
97
|
|
|
* |
98
|
|
|
* @noinspection PhpUnusedParameterInspection |
99
|
|
|
*/ |
100
|
|
|
protected function formatValue(MetadataInterface $metadata, string $fieldName, $value, ?string $format = null) |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
return $value; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.