|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sanderdekroon\Parlant\Compiler; |
|
4
|
|
|
|
|
5
|
|
|
use Sanderdekroon\Parlant\Builder\NestedMeta; |
|
6
|
|
|
|
|
7
|
|
|
trait CompilesMeta |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Compile the meta query to valid query arguments |
|
12
|
|
|
* @param array $wheres |
|
13
|
|
|
*/ |
|
14
|
7 |
|
protected function compileWhereMetas($wheres) |
|
15
|
|
|
{ |
|
16
|
7 |
|
if (empty($wheres) || !is_array($wheres)) { |
|
17
|
|
|
return; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
7 |
|
$compiled = []; |
|
21
|
|
|
|
|
22
|
7 |
|
foreach ($wheres as $where) { |
|
23
|
7 |
|
if ($where instanceof NestedMeta) { |
|
24
|
1 |
|
$compiled[] = $this->compileNestedMeta($where); |
|
25
|
1 |
|
continue; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
7 |
|
$compiled[] = $this->prepareMetaArguments($where); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
7 |
|
$this->addArgument('meta_query', $compiled, true); |
|
|
|
|
|
|
32
|
7 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Complie nested meta query arguments to valid query arguments. If a meta query |
|
36
|
|
|
* contains another nested meta, we'll resolve that recursively. |
|
37
|
|
|
* @param NestedMeta $nestedMeta |
|
38
|
|
|
* @param integer $level |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
1 |
|
protected function compileNestedMeta($nestedMeta, $level = 2) |
|
42
|
|
|
{ |
|
43
|
1 |
|
$query = $nestedMeta->getQuery(); |
|
44
|
1 |
|
foreach ($query as $key => $meta) { |
|
45
|
1 |
|
if ($meta instanceof NestedMeta) { |
|
46
|
|
|
$query[$key] = $this->compileNestedMeta($meta, $level++); |
|
47
|
|
|
continue; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
$query[$key] = $this->prepareMetaArguments($meta); |
|
51
|
|
|
} |
|
52
|
1 |
|
$query['relation'] = $nestedMeta->getRelation(); |
|
53
|
|
|
|
|
54
|
1 |
|
return $query; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Format the supplied arguments to WordPress arguments |
|
59
|
|
|
* @param array $meta |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
7 |
|
private function prepareMetaArguments($meta) |
|
63
|
|
|
{ |
|
64
|
|
|
return [ |
|
65
|
7 |
|
'key' => $meta['column'], |
|
66
|
7 |
|
'value' => $meta['value'], |
|
67
|
7 |
|
'compare' => $meta['operator'], |
|
68
|
7 |
|
'type' => $meta['type'], |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Compile the relation(s) between meta queries and add it to the arguments. |
|
74
|
|
|
* @param array $relations The index of the array is used as the level. |
|
75
|
|
|
*/ |
|
76
|
7 |
|
protected function compileWhereMetaRelation($relations) |
|
77
|
|
|
{ |
|
78
|
7 |
|
return $this->addArgument('meta_query', ['relation' => reset($relations)], true); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
protected abstract function addArgument(); |
|
82
|
|
|
} |
|
83
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.