Test Failed
Push — master ( b17eba...69b436 )
by Kirill
06:37
created

Operator::normalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of Hydrogen package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace RDS\Hydrogen\Criteria\Where;
11
12
use Illuminate\Support\Str;
13
14
/**
15
 * Class Operator
16
 */
17
final class Operator
18
{
19
    public const EQ = '=';
20
    public const NEQ = '<>';
21
    public const GT = '>';
22
    public const GTE = '>=';
23
    public const LT = '<';
24
    public const LTE = '<=';
25
26
    // X IN (...)
27
    public const IN = 'IN';
28
    public const NOT_IN = 'NOT IN';
29
30
    // LIKE
31
    public const LIKE = 'LIKE';
32
    public const NOT_LIKE = 'NOT LIKE';
33
34
    // BETWEEN
35
    public const BTW = 'BETWEEN';
36
    public const NOT_BTW = 'NOT BETWEEN';
37
38
    /**
39
     * Mappings
40
     *
41
     * Transform the given format into normal operator format.
42
     */
43
    private const OPERATOR_MAPPINGS = [
44
        '=='       => self::EQ,
45
        'IS'       => self::EQ,
46
        '!='       => self::NEQ,
47
        'NOTIS'    => self::NEQ,
48
        '!IN'      => self::NOT_IN,
49
        '~'        => self::LIKE,
50
        '!LIKE'    => self::NOT_LIKE,
51
        '!~'       => self::NOT_LIKE,
52
        '..'       => self::BTW,
53
        '...'      => self::BTW,
54
        '!BETWEEN' => self::NOT_BTW,
55
        '!..'      => self::NOT_BTW,
56
        '!...'     => self::NOT_BTW,
57
    ];
58
59
    /**
60
     * @var string
61
     */
62
    private $operator;
63
64
    /**
65
     * Operator constructor.
66
     * @param string $operator
67
     */
68 43
    public function __construct(string $operator)
69
    {
70 43
        $this->operator = $this->normalize($operator);
71 43
    }
72
73
    /**
74
     * @param string $operator
75
     * @return string
76
     */
77 43
    private function normalize(string $operator): string
78
    {
79 43
        $upper = Str::upper($operator);
80
81 43
        $operator = \str_replace(' ', '', $upper);
82
83 43
        return self::OPERATOR_MAPPINGS[$operator] ?? $upper;
84
    }
85
86
    /**
87
     * @param string $operator
88
     * @return Operator
89
     */
90
    public static function new(string $operator): self
91
    {
92
        return new static($operator);
93
    }
94
95
    /**
96
     * @param string $operator
97
     * @return Operator
98
     */
99 4
    public function changeTo(string $operator): self
100
    {
101 4
        $this->operator = $operator;
102
103 4
        return $this;
104
    }
105
106
    /**
107
     * @return string
108
     */
109 35
    public function toString(): string
110
    {
111 35
        return $this->operator;
112
    }
113
114
    /**
115
     * @return string
116
     */
117
    public function __toString(): string
118
    {
119
        return $this->operator;
120
    }
121
122
    /**
123
     * @param string $operator
124
     * @return bool
125
     */
126 20
    public function is(string $operator): bool
127
    {
128 20
        return $this->operator === Str::upper($operator, 'UTF-8');
0 ignored issues
show
Unused Code introduced by
The call to Str::upper() has too many arguments starting with 'UTF-8'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
129
    }
130
}
131