1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* This file is part of the Happyr Doctrine Specification package. |
6
|
|
|
* |
7
|
|
|
* (c) Tobias Nyholm <[email protected]> |
8
|
|
|
* Kacper Gunia <[email protected]> |
9
|
|
|
* Peter Gribanov <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Happyr\DoctrineSpecification\Filter; |
16
|
|
|
|
17
|
|
|
use Doctrine\ORM\QueryBuilder; |
18
|
|
|
use Happyr\DoctrineSpecification\Operand\ArgumentToOperandConverter; |
19
|
|
|
use Happyr\DoctrineSpecification\Operand\Operand; |
20
|
|
|
|
21
|
|
|
final class IsNull implements Filter, Satisfiable |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Operand|string |
25
|
|
|
*/ |
26
|
|
|
private $field; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string|null |
30
|
|
|
*/ |
31
|
|
|
private $context; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Operand|string $field |
35
|
|
|
* @param string|null $context |
36
|
|
|
*/ |
37
|
|
|
public function __construct($field, ?string $context = null) |
38
|
|
|
{ |
39
|
|
|
$this->field = $field; |
40
|
|
|
$this->context = $context; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param QueryBuilder $qb |
45
|
|
|
* @param string $context |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getFilter(QueryBuilder $qb, string $context): string |
50
|
|
|
{ |
51
|
|
|
if (null !== $this->context) { |
52
|
|
|
$context = sprintf('%s.%s', $context, $this->context); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$field = ArgumentToOperandConverter::toField($this->field); |
56
|
|
|
|
57
|
|
|
return (string) $qb->expr()->isNull($field->transform($qb, $context)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function filterCollection(iterable $collection, ?string $context = null): iterable |
64
|
|
|
{ |
65
|
|
|
$context = $this->resolveContext($context); |
66
|
|
|
$field = ArgumentToOperandConverter::toField($this->field); |
67
|
|
|
|
68
|
|
|
foreach ($collection as $candidate) { |
69
|
|
|
if (null === $field->execute($candidate, $context)) { |
70
|
|
|
yield $candidate; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public function isSatisfiedBy($candidate, ?string $context = null): bool |
79
|
|
|
{ |
80
|
|
|
$context = $this->resolveContext($context); |
81
|
|
|
$field = ArgumentToOperandConverter::toField($this->field); |
82
|
|
|
|
83
|
|
|
return null === $field->execute($candidate, $context); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string|null $context |
88
|
|
|
* |
89
|
|
|
* @return string|null |
90
|
|
|
*/ |
91
|
|
|
private function resolveContext(?string $context): ?string |
92
|
|
|
{ |
93
|
|
|
if (null !== $this->context && null !== $context) { |
94
|
|
|
return sprintf('%s.%s', $context, $this->context); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (null !== $this->context) { |
98
|
|
|
return $this->context; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $context; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.