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

Terms::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 3
rs 10
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 Terms
17
{
18
    private $params = [
19
        'f' => null,
20
        'method' => null,
21
        'separator' => null,
22
    ];
23
24
    private $values;
25
26
    public function __construct(string $field, array $values)
27
    {
28
        $this->params['f'] = $field;
29
        $this->values = $values;
30
    }
31
32
    public function __toString(): string
33
    {
34
        return sprintf('{!terms %s}%s', urldecode(http_build_query($this->params, '', ' ')), implode($this->params['separator'] ?? ',', $this->values));
35
    }
36
37
    public static function create(string $field, array $values): self
38
    {
39
        return new self($field, $values);
40
    }
41
42
    public function separator(string $separator): self
43
    {
44
        $terms = clone $this;
45
        $terms->params['separator'] = $separator;
46
47
        return $terms;
48
    }
49
50
    public function method(string $method): self
51
    {
52
        if (!\in_array($method, $available = ['termsFilter', 'booleanQuery', 'automaton', 'docValuesTermsFilter'], true)) {
53
            throw new \InvalidArgumentException(sprintf('Available methods: %s!', implode(',', $available)));
54
        }
55
56
        $terms = clone $this;
57
        $terms->params['method'] = $method;
58
59
        return $terms;
60
    }
61
}
62