1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\ElasticsearchDSL\Query\Specialized; |
13
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchDSL\BuilderInterface; |
15
|
|
|
use ONGR\ElasticsearchDSL\ParametersTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Represents Elasticsearch "template" query. |
19
|
|
|
* |
20
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-template-query.html |
21
|
|
|
*/ |
22
|
|
|
class TemplateQuery implements BuilderInterface |
23
|
|
|
{ |
24
|
|
|
use ParametersTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $file; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $inline; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $params; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $file A template of the query |
43
|
|
|
* @param string $inline A template of the query |
44
|
|
|
* @param array $params Parameters to insert into template |
45
|
|
|
*/ |
46
|
|
|
public function __construct($file = null, $inline = null, array $params = []) |
47
|
|
|
{ |
48
|
|
|
$this->setFile($file); |
49
|
|
|
$this->setInline($inline); |
50
|
|
|
$this->setParams($params); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function getFile() |
57
|
|
|
{ |
58
|
|
|
return $this->file; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $file |
63
|
|
|
* |
64
|
|
|
* @return $this; |
|
|
|
|
65
|
|
|
*/ |
66
|
|
|
public function setFile($file) |
67
|
|
|
{ |
68
|
|
|
$this->file = $file; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function getInline() |
77
|
|
|
{ |
78
|
|
|
return $this->inline; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $inline |
83
|
|
|
* |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
|
|
public function setInline($inline) |
87
|
|
|
{ |
88
|
|
|
$this->inline = $inline; |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
public function getParams() |
97
|
|
|
{ |
98
|
|
|
return $this->params; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param array $params |
103
|
|
|
* |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
|
|
public function setParams($params) |
107
|
|
|
{ |
108
|
|
|
$this->params = $params; |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
|
public function getType() |
117
|
|
|
{ |
118
|
|
|
return 'template'; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
|
|
*/ |
124
|
|
|
public function toArray() |
125
|
|
|
{ |
126
|
|
|
$output = array_filter( |
127
|
|
|
[ |
128
|
|
|
'file' => $this->getFile(), |
129
|
|
|
'inline' => $this->getInline(), |
130
|
|
|
'params' => $this->getParams(), |
131
|
|
|
] |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
if (!isset($output['file']) && !isset($output['inline'])) { |
135
|
|
|
throw new \InvalidArgumentException( |
136
|
|
|
'Template query requires that either `inline` or `file` parameters are set' |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$output = $this->processArray($output); |
141
|
|
|
|
142
|
|
|
return [$this->getType() => $output]; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.