Passed
Push — master ( cc3f84...4a930e )
by Timo
23:43
created

Operator::getAnd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1.037

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 3
cp 0.6667
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1.037
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2017 <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
28
/**
29
 * The Operator ParameterProvider is responsible to build the solr query parameters
30
 * that are needed for the operator q.op.
31
 *
32
 * @package ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder
33
 */
34
class Operator extends AbstractDeactivatable
35
{
36
    const OPERATOR_AND = 'AND';
37
    const OPERATOR_OR = 'OR';
38
39
    /**
40
     * @var string
41
     */
42
    protected $operator = 'AND';
43
44
    /**
45
     * Faceting constructor.
46
     *
47
     * @param bool $isEnabled
48
     * @param string $operator
49
     */
50 1
    public function __construct($isEnabled, $operator = Operator::OPERATOR_AND)
51
    {
52 1
        $this->isEnabled = $isEnabled;
53 1
        $this->setOperator($operator);
54 1
    }
55
56
    /**
57
     * @param string $operator
58
     */
59 1
    public function setOperator($operator)
60
    {
61 1
        if (!in_array($operator, [self::OPERATOR_AND, self::OPERATOR_OR])) {
62
            throw new \InvalidArgumentException("Invalid operator");
63
        }
64
65 1
        $this->operator = $operator;
66 1
    }
67
68
    /**
69
     * @return string
70
     */
71 1
    public function getOperator(): string
72
    {
73 1
        return $this->operator;
74
    }
75
76
    /**
77
     * @return Operator
78
     */
79
    public static function getEmpty(): Operator
80
    {
81
        return new Operator(false);
82
    }
83
84
    /**
85
     * @return Operator
86
     */
87 1
    public static function getAnd(): Operator
88
    {
89 1
        return new Operator(true, static::OPERATOR_AND);
90
    }
91
92
    /**
93
     * @return Operator
94
     */
95 1
    public static function getOr(): Operator
96
    {
97 1
        return new Operator(true, static::OPERATOR_OR);
98
    }
99
100
    /**
101
     * @param string $operator
102
     * @return Operator
103
     */
104
    public static function fromString($operator)
105
    {
106
        return new Operator(true, $operator);
107
    }
108
}