Terms::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 12
rs 10
c 0
b 0
f 0
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\Helper;
15
16
use iCom\SolrClient\Query\QueryHelper;
17
18
/**
19
 * Uses the Terms Query Parser.
20
 *
21
 * May be more efficient in some cases than using
22
 * the Standard Query Parser to generate a boolean
23
 * query since the default implementation method avoids scoring.
24
 *
25
 * @see https://lucene.apache.org/solr/guide/8_4/other-parsers.html#term-query-parser
26
 *
27
 * @psalm-immutable
28
 */
29
final class Terms implements QueryHelper
30
{
31
    use Caching;
32
33
    /**
34
     * @var string
35
     */
36
    private $f;
37
38
    /**
39
     * @var ?string
40
     */
41
    private $method;
42
43
    /**
44
     * @var ?string
45
     */
46
    private $separator;
47
48
    /**
49
     * @psalm-var non-empty-array<array-key, mixed>
50
     */
51
    private $values;
52
53
    /**
54
     * @param array<mixed> $values
55
     */
56
    public function __construct(string $field, array $values)
57
    {
58
        if ('' === $field) {
59
            throw new \InvalidArgumentException('The "field" parameter can not be empty.');
60
        }
61
62
        if ([] === $values) {
63
            throw new \InvalidArgumentException('The "values" parameter can not be empty.');
64
        }
65
66
        $this->f = $field;
67
        $this->values = $values;
68
    }
69
70
    /**
71
     * @param array<mixed> $values
72
     */
73
    public static function create(string $field, array $values): self
74
    {
75
        return new self($field, $values);
76
    }
77
78
    public function separator(string $separator): self
79
    {
80
        $terms = clone $this;
81
        $terms->separator = $separator;
82
83
        return $terms;
84
    }
85
86
    public function method(string $method): self
87
    {
88
        if (!\in_array($method, $available = ['termsFilter', 'booleanQuery', 'automaton', 'docValuesTermsFilter'], true)) {
89
            throw new \InvalidArgumentException(sprintf('Available methods are: "%s"', implode('", "', $available)));
90
        }
91
92
        $terms = clone $this;
93
        $terms->method = $method;
94
95
        return $terms;
96
    }
97
98
    public function toString(): string
99
    {
100
        $params = [
101
            'f' => $this->f,
102
            'method' => $this->method,
103
            'separator' => null,
104
        ] + $this->getCacheFields();
105
106
        if (null !== $this->separator) {
107
            $params['separator'] = sprintf('"%s"', addslashes($this->separator));
108
        }
109
110
        return sprintf('{!terms %s}%s', urldecode(http_build_query($params, '', ' ')), implode($this->separator ?? ',', $this->values));
111
    }
112
}
113