Completed
Push — master ( 6c9334...f3ba17 )
by Simonas
02:26
created

TemplateQuery::toArray()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 3
eloc 10
nc 2
nop 0
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
    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