Completed
Pull Request — master (#348)
by
unknown
09:42 queued 08:20
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
namespace ONGR\ElasticsearchDSL\Query\Specialized;
4
5
use ONGR\ElasticsearchDSL\BuilderInterface;
6
use ONGR\ElasticsearchDSL\ParametersTrait;
7
8
/**
9
 * Represents Elasticsearch "template" query.
10
 *
11
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-template-query.html
12
 */
13
class TemplateQuery implements BuilderInterface
14
{
15
    use ParametersTrait;
16
17
    public function __construct(
18
        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...
19
        private ?string $inline = null,
20
        private array $params = []
21
    ) {
22
        $this->setFile($file);
23
        $this->setInline($inline);
24
        $this->setParams($params);
25
    }
26
27
    public function getFile(): ?string
28
    {
29
        return $this->file;
30
    }
31
32
    public function setFile(?string $file): static
33
    {
34
        $this->file = $file;
35
36
        return $this;
37
    }
38
39
    public function getInline(): ?string
40
    {
41
        return $this->inline;
42
    }
43
44
    public function setInline(?string $inline): static
45
    {
46
        $this->inline = $inline;
47
48
        return $this;
49
    }
50
51
    public function getParams(): array
52
    {
53
        return $this->params;
54
    }
55
56
    public function setParams(array $params): static
57
    {
58
        $this->params = $params;
59
60
        return $this;
61
    }
62
63
    public function getType(): string
64
    {
65
        return 'template';
66
    }
67
68
    public function toArray(): array
69
    {
70
        $output = array_filter(
71
            [
72
                'file' => $this->getFile(),
73
                'inline' => $this->getInline(),
74
                'params' => $this->getParams(),
75
            ]
76
        );
77
78
        if (!isset($output['file']) && !isset($output['inline'])) {
79
            throw new \InvalidArgumentException(
80
                'Template query requires that either `inline` or `file` parameters are set'
81
            );
82
        }
83
84
        $output = $this->processArray($output);
85
86
        return [$this->getType() => $output];
87
    }
88
}
89