Passed
Push — master ( ade47d...27088c )
by Gábor
02:35
created

Collapse::min()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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