Passed
Pull Request — master (#2)
by
unknown
05:40
created

TermsQuery   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 22
c 1
b 0
f 1
dl 0
loc 48
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A method() 0 10 2
A __toString() 0 7 3
A __construct() 0 4 1
A separator() 0 6 1
A create() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Solr Client Symfony package.
7
 *
8
 * (c) ingatlan.com Zrt. <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
namespace iCom\SolrClient\Query;
15
16
final class TermsQuery
17
{
18
    private $params = [
19
        'field' => null,
20
        'separator' => ',',
21
        'method' => null,
22
        'values' => null,
23
    ];
24
25
    public function __construct(string $field, array $values)
26
    {
27
        $this->params['field'] = $field;
28
        $this->params['values'] = $values;
29
    }
30
31
    public function __toString(): string
32
    {
33
        return sprintf('{!terms f=%s%s%s}%s',
34
            $this->params['field'],
35
            null === $this->params['method'] ? '' : ' method='.$this->params['method'],
36
            ',' === $this->params['separator'] ? '' : sprintf(' separator="%s"', $this->params['separator']),
37
            \implode($this->params['separator'], $this->params['values'])
38
        );
39
    }
40
41
    public static function create(string $field, array $values): self
42
    {
43
        return new self($field, $values);
44
    }
45
46
    public function separator(string $separator): self
47
    {
48
        $collapse = clone $this;
49
        $collapse->params['separator'] = $separator;
50
51
        return $collapse;
52
    }
53
54
    public function method(string $method): self
55
    {
56
        if (!\in_array($method, $available = ['termsFilter', 'booleanQuery', 'automaton', 'docValuesTermsFilter'], true)) {
57
            throw new \InvalidArgumentException(sprintf('Available methods: %s!', implode(',', $available)));
58
        }
59
60
        $collapse = clone $this;
61
        $collapse->params['method'] = $method;
62
63
        return $collapse;
64
    }
65
}
66