Passed
Push — master ( af0e2f...ade47d )
by Gábor
03:00 queued 10s
created

Collapse::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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
final class Collapse
17
{
18
    private $params = [
19
        'field' => null,
20
        'min' => null,
21
        'max' => null,
22
        'sort' => null,
23
        'nullPolicy' => null,
24
        'hint' => null,
25
        'size' => null,
26
        'cache' => null,
27
    ];
28
29
    public function __construct(string $field)
30
    {
31
        $this->params['field'] = $field;
32
    }
33
34
    public function __toString(): string
35
    {
36
        return sprintf('{!collapse %s}', urldecode(http_build_query($this->params, '', ' ')));
37
    }
38
39
    public static function create(string $field): self
40
    {
41
        return new self($field);
42
    }
43
44
    public function min(string $expression): self
45
    {
46
        $this->assertSingleSort();
47
48
        $collapse = clone $this;
49
        $collapse->params['min'] = $expression;
50
51
        return $collapse;
52
    }
53
54
    public function max(string $expression): self
55
    {
56
        $this->assertSingleSort();
57
58
        $collapse = clone $this;
59
        $collapse->params['max'] = $expression;
60
61
        return $collapse;
62
    }
63
64
    public function sort(array $sort): self
65
    {
66
        $this->assertSingleSort();
67
68
        $collapse = clone $this;
69
        $collapse->params['sort'] = sprintf("'%s'", implode(',', $sort));
70
71
        return $collapse;
72
    }
73
74
    public function nullPolicy(string $nullPolicy): self
75
    {
76
        if (!\in_array($nullPolicy, $available = ['ignore', 'expand', 'collapse'], true)) {
77
            throw new \InvalidArgumentException(sprintf('Available null policies: %s!', implode(',', $available)));
78
        }
79
80
        $collapse = clone $this;
81
        $collapse->params['nullPolicy'] = $nullPolicy;
82
83
        return $collapse;
84
    }
85
86
    public function hint(): self
87
    {
88
        $collapse = clone $this;
89
        $collapse->params['hint'] = 'top_fc';
90
91
        return $collapse;
92
    }
93
94
    public function size(int $size): self
95
    {
96
        $collapse = clone $this;
97
        $collapse->params['size'] = $size;
98
99
        return $collapse;
100
    }
101
102
    public function cache(bool $cache): self
103
    {
104
        $collapse = clone $this;
105
        $collapse->params['cache'] = $cache ? 'true' : 'false';
106
107
        return $collapse;
108
    }
109
110
    private function assertSingleSort(): void
111
    {
112
        if (null !== $this->params['min'] || null !== $this->params['max'] || null !== $this->params['sort']) {
113
            throw new \RuntimeException('Multiple sort is not allowed.');
114
        }
115
    }
116
}
117