Passed
Pull Request — master (#2)
by
unknown
02:29
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
        'cache' => null,
23
    ];
24
25
    private $values;
26
27
    public function __construct(string $field, array $values)
28
    {
29
        $this->params['f'] = $field;
30
        $this->values = $values;
31
    }
32
33
    public function __toString(): string
34
    {
35
        $separator = stripslashes($this->params['separator'] ?? '","');
36
37
        return sprintf('{!terms %s}%s', urldecode(http_build_query($this->params, '', ' ')), implode(substr($separator, 1, -1), $this->values));
38
    }
39
40
    public static function create(string $field, array $values): self
41
    {
42
        return new self($field, $values);
43
    }
44
45
    public function separator(string $separator): self
46
    {
47
        $terms = clone $this;
48
        $terms->params['separator'] = sprintf('"%s"', addslashes($separator));
49
50
        return $terms;
51
    }
52
53
    public function method(string $method): self
54
    {
55
        if (!\in_array($method, $available = ['termsFilter', 'booleanQuery', 'automaton', 'docValuesTermsFilter'], true)) {
56
            throw new \InvalidArgumentException(sprintf('Available methods: %s!', implode(',', $available)));
57
        }
58
59
        $terms = clone $this;
60
        $terms->params['method'] = $method;
61
62
        return $terms;
63
    }
64
65
    public function cache(bool $cache): self
66
    {
67
        $terms = clone $this;
68
        $terms->params['cache'] = $cache ? 'true' : 'false';
69
70
        return $terms;
71
    }
72
}
73