Completed
Push — master ( eaf770...e2d746 )
by Sebastian
04:09
created

StringCompare::getMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Linna Filter
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types = 1);
11
12
namespace Linna\Filter\Rules;
13
14
use UnexpectedValueException;
15
16
/**
17
 * Compare two strings using >, <, >=, <=, =, != operators.
18
 */
19
class StringCompare extends AbstractString implements RuleSanitizeInterface
20
{
21
    /**
22
     * @var array Arguments expected.
23
     */
24
    private $arguments = ['string', 'string'];
0 ignored issues
show
introduced by
The private property $arguments is not used, and could be removed.
Loading history...
25
26
    /**
27
     * @var string Error message
28
     */
29
    private $message = '';
30
31
    /**
32
     * Validate.
33
     *
34
     * @param string $received
35
     * @param string $operator
36
     * @param string $compare
37
     *
38
     * @return bool
39
     */
40 19
    public function validate(string $received, string $operator, string $compare): bool
41
    {
42 19
        if ($this->switchOperator($operator, $received, $compare)) {
43 9
            return false;
44
        }
45
46 9
        return true;
47
    }
48
49
    /**
50
     * Perform correct operation from passed operator.
51
     *
52
     * @param string $operator
53
     * @param string $strReceived
54
     * @param string $strCompare
55
     *
56
     * @return bool
57
     *
58
     * @throws UnexpectedValueException if unknown operator is provided.
59
     */
60 19
    private function switchOperator(string $operator, string &$strReceived, string &$strCompare): bool
61
    {
62
        switch ($operator) {
63 19
            case '>': //greater than
64 3
                return $strReceived > $strCompare;
65 16
            case '<': //less than
66 3
                return $strReceived < $strCompare;
67 13
            case '>=': //greater than or equal
68 3
                return $strReceived >= $strCompare;
69 10
            case '<=': //less than or equal
70 3
                return $strReceived <= $strCompare;
71 7
            case '=': //equal
72 3
                return $strReceived === $strCompare;
73 4
            case '!=': //not equal
74 3
                return $strReceived !== $strCompare;
75
            default:
76 1
                throw new UnexpectedValueException("Unknown comparson operator ({$operator}). Permitted >, <, >=, <=, =, !=");
77
        }
78
    }
79
80
    /**
81
     * Return error message.
82
     *
83
     * @return string Error message
84
     */
85
    public function getMessage(): string
86
    {
87
        return $this->message;
88
    }
89
}
90