1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MartinGeorgiev\Doctrine\ORM\Query\AST\Functions; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Query\AST\Node; |
8
|
|
|
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Exception\InvalidArgumentForVariadicFunctionException; |
9
|
|
|
use MartinGeorgiev\Doctrine\ORM\Query\AST\Functions\Traits\BooleanValidationTrait; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Implementation of PostgreSQL JSONB_PATH_MATCH(). |
13
|
|
|
* |
14
|
|
|
* Returns the SQL boolean result of a JSON path predicate check for the specified JSON value. |
15
|
|
|
* This is useful only with predicate check expressions, not SQL-standard JSON path expressions, |
16
|
|
|
* since it will either fail or return NULL if the path result is not a single boolean value. |
17
|
|
|
* |
18
|
|
|
* @see https://www.postgresql.org/docs/14/functions-json.html |
19
|
|
|
* @since 3.1 |
20
|
|
|
* |
21
|
|
|
* @author Martin Georgiev <[email protected]> |
22
|
|
|
* |
23
|
|
|
* @example Using it in DQL: "SELECT JSONB_PATH_MATCH(e.jsonbData, 'exists($.a[*] ? (@ >= 2 && @ <= 4))')" |
24
|
|
|
*/ |
25
|
|
|
class JsonbPathMatch extends BaseVariadicFunction |
26
|
|
|
{ |
27
|
|
|
use BooleanValidationTrait; |
|
|
|
|
28
|
|
|
|
29
|
4 |
|
protected function getNodeMappingPattern(): array |
30
|
|
|
{ |
31
|
4 |
|
return ['StringPrimary']; |
32
|
|
|
} |
33
|
|
|
|
34
|
4 |
|
protected function customizeFunction(): void |
35
|
|
|
{ |
36
|
4 |
|
$this->setFunctionPrototype('jsonb_path_match(%s)'); |
37
|
|
|
} |
38
|
|
|
|
39
|
4 |
|
protected function validateArguments(Node ...$arguments): void |
40
|
|
|
{ |
41
|
4 |
|
$argumentCount = \count($arguments); |
42
|
4 |
|
if ($argumentCount < 2 || $argumentCount > 4) { |
43
|
2 |
|
throw InvalidArgumentForVariadicFunctionException::between('jsonb_path_match', 2, 4); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// Validate that the fourth parameter is a valid boolean if provided |
47
|
2 |
|
if ($argumentCount === 4) { |
48
|
2 |
|
$this->validateBoolean($arguments[3], 'JSONB_PATH_MATCH'); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|