|
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; |
|
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
|
|
|
public function setFile($file) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->file = $file; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getInline() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->inline; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $inline |
|
79
|
|
|
*/ |
|
80
|
|
|
public function setInline($inline) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->inline = $inline; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getParams() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->params; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param array $params |
|
95
|
|
|
*/ |
|
96
|
|
|
public function setParams($params) |
|
97
|
|
|
{ |
|
98
|
|
|
$this->params = $params; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* {@inheritdoc} |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getType() |
|
105
|
|
|
{ |
|
106
|
|
|
return 'template'; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inheritdoc} |
|
111
|
|
|
*/ |
|
112
|
|
|
public function toArray() |
|
113
|
|
|
{ |
|
114
|
|
|
$output = array_filter( |
|
115
|
|
|
[ |
|
116
|
|
|
'file' => $this->getFile(), |
|
117
|
|
|
'inline' => $this->getInline(), |
|
118
|
|
|
'params' => $this->getParams(), |
|
119
|
|
|
] |
|
120
|
|
|
); |
|
121
|
|
|
|
|
122
|
|
|
if (!isset($output['file']) && !isset($output['inline'])) { |
|
123
|
|
|
throw new \InvalidArgumentException( |
|
124
|
|
|
'Template query requires that either `inline` or `file` parameters are set' |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$output = $this->processArray($output); |
|
129
|
|
|
|
|
130
|
|
|
return [$this->getType() => $output]; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|