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

Terms   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 18
c 1
b 0
f 1
dl 0
loc 44
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A separator() 0 6 1
A create() 0 3 1
A __construct() 0 4 1
A method() 0 10 2
A __toString() 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 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