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

StringLenCompare   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 70
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B switchOperator() 0 17 7
A validate() 0 9 2
A getMessage() 0 3 1
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 the length of provided string using >, <, >=, <=, = operators.
18
 */
19
class StringLenCompare extends AbstractString implements RuleSanitizeInterface
20
{
21
    /**
22
     * @var array Arguments expected.
23
     */
24
    private $arguments = ['string', 'number'];
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 int    $compare
37
     * @return bool
38
     */
39 37
    public function validate(string $received, string $operator, int $compare): bool
40
    {
41 37
        if ($this->switchOperator($operator, $received, $compare)) {
42 19
            return false;
43
        }
44
45 17
        $this->message = "Received string length is not {$operator} of {$compare}";
46
47 17
        return true;
48
    }
49
50
    /**
51
     * Perform correct operation from passed operator.
52
     *
53
     * @param string $operator
54
     * @param string $strReceived
55
     * @param int    $strCompare
56
     *
57
     * @return bool
58
     *
59
     * @throws UnexpectedValueException if unknown operator is provided.
60
     */
61 37
    private function switchOperator(string $operator, string &$strReceived, int &$strCompare): bool
62
    {
63
        switch ($operator) {
64 37
            case '>': //greater than
65 6
                return strlen($strReceived) > $strCompare;
66 31
            case '<': //less than
67 6
                return strlen($strReceived) < $strCompare;
68 25
            case '>=': //greater than or equal
69 9
                return strlen($strReceived) >= $strCompare;
70 16
            case '<=': //less than or equal
71 9
                return strlen($strReceived) <= $strCompare;
72 7
            case '=': //equal
73 3
                return strlen($strReceived) === $strCompare;
74 4
            case '!=': //equal
75 3
                return strlen($strReceived) !== $strCompare;
76
            default:
77 1
                throw new UnexpectedValueException("Unknown comparson operator ({$operator}). Permitted >, <, >=, <=, =, !=");
78
        }
79
    }
80
81
    /**
82
     * Return error message.
83
     *
84
     * @return string Error message
85
     */
86 8
    public function getMessage(): string
87
    {
88 8
        return $this->message;
89
    }
90
}
91