Completed
Pull Request — master (#348)
by
unknown
01:15
created

TemplateQuery   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 123
rs 10
c 0
b 0
f 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
    public function __construct(
27
        private ?string $file = null,
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_PRIVATE, expecting T_VARIABLE
Loading history...
28
        private ?string $inline = null,
29
        private array $params = []
30
    ) {
31
        $this->setFile($file);
32
        $this->setInline($inline);
33
        $this->setParams($params);
34
    }
35
36
    public function getFile(): ?string
37
    {
38
        return $this->file;
39
    }
40
41
    public function setFile(?string $file): static
42
    {
43
        $this->file = $file;
44
45
        return $this;
46
    }
47
48
    public function getInline(): ?string
49
    {
50
        return $this->inline;
51
    }
52
53
    public function setInline(?string $inline): static
54
    {
55
        $this->inline = $inline;
56
57
        return $this;
58
    }
59
60
    public function getParams(): array
61
    {
62
        return $this->params;
63
    }
64
65
    public function setParams(array $params): static
66
    {
67
        $this->params = $params;
68
69
        return $this;
70
    }
71
72
    public function getType(): string
73
    {
74
        return 'template';
75
    }
76
77
    public function toArray(): array
78
    {
79
        $output = array_filter(
80
            [
81
                'file' => $this->getFile(),
82
                'inline' => $this->getInline(),
83
                'params' => $this->getParams(),
84
            ]
85
        );
86
87
        if (!isset($output['file']) && !isset($output['inline'])) {
88
            throw new \InvalidArgumentException(
89
                'Template query requires that either `inline` or `file` parameters are set'
90
            );
91
        }
92
93
        $output = $this->processArray($output);
94
95
        return [$this->getType() => $output];
96
    }
97
}
98