Passed
Pull Request — master (#2)
by
unknown
02:23
created

Terms::method()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 5
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 10
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
        $separator = stripslashes($this->params['separator'] ?? '","');
35
36
        return sprintf('{!terms %s}%s', urldecode(http_build_query($this->params, '', ' ')), implode(substr($separator, 1, -1), $this->values));
37
    }
38
39
    public static function create(string $field, array $values): self
40
    {
41
        return new self($field, $values);
42
    }
43
44
    public function separator(string $separator): self
45
    {
46
        $terms = clone $this;
47
        $terms->params['separator'] = sprintf('"%s"', addslashes($separator));
48
49
        return $terms;
50
    }
51
52
    public function method(string $method): self
53
    {
54
        if (!\in_array($method, $available = ['termsFilter', 'booleanQuery', 'automaton', 'docValuesTermsFilter'], true)) {
55
            throw new \InvalidArgumentException(sprintf('Available methods: %s!', implode(',', $available)));
56
        }
57
58
        $terms = clone $this;
59
        $terms->params['method'] = $method;
60
61
        return $terms;
62
    }
63
}
64