Completed
Push — master ( 5ad706...418f81 )
by Sebastian
03:53
created

StringLenCompare::concreteValidate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 9
ccs 5
cts 5
cp 1
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 the length of provided string using >, <, >=, <=, = operators.
18
 */
19
class StringLenCompare extends AbstractString implements RuleSanitizeInterface, RuleValidateInterface
20
{
21
    /**
22
     * @var array Rule properties
23
     */
24
    public static $config = [
25
        'class' => 'StringLenCompare',
26
        'full_class' => __CLASS__,
27
        'alias' => ['stringlencompare', 'strlencmp', 'slc'],
28
        'args_count' => 2,
29
        'args_type' => ['string', 'number'],
30
        'has_validate' => true,
31
        //'has_sanitize' => true
32
    ];
33
34
    /**
35
     * @var string Error message
36
     */
37
    private $message = '';
38
39
    /**
40
     * Validate.
41
     *
42
     * @return bool
43
     */
44 37
    public function validate(): bool
45
    {
46 37
        $args = func_get_args();
47
48 37
        return $this->concreteValidate($args[0], $args[1], $args[2]);
49
    }
50
51
    /**
52
     * Concrete validate.
53
     *
54
     * @param string $received
55
     * @param string $operator
56
     * @param int    $compare
57
     *
58
     * @return bool
59
     */
60 37
    private function concreteValidate(string $received, string $operator, int $compare): bool
61
    {
62 37
        if ($this->switchOperator($operator, $received, $compare)) {
63 19
            return false;
64
        }
65
66 17
        $this->message = "Received string length is not {$operator} of {$compare}";
67
68 17
        return true;
69
    }
70
71
    /**
72
     * Perform correct operation from passed operator.
73
     *
74
     * @param string $operator
75
     * @param string $strReceived
76
     * @param int    $strCompare
77
     *
78
     * @return bool
79
     *
80
     * @throws UnexpectedValueException if unknown operator is provided.
81
     */
82 37
    private function switchOperator(string $operator, string &$strReceived, int &$strCompare): bool
83
    {
84
        switch ($operator) {
85 37
            case '>': //greater than
86 6
                return strlen($strReceived) > $strCompare;
87 31
            case '<': //less than
88 6
                return strlen($strReceived) < $strCompare;
89 25
            case '>=': //greater than or equal
90 9
                return strlen($strReceived) >= $strCompare;
91 16
            case '<=': //less than or equal
92 9
                return strlen($strReceived) <= $strCompare;
93 7
            case '=': //equal
94 3
                return strlen($strReceived) === $strCompare;
95 4
            case '!=': //equal
96 3
                return strlen($strReceived) !== $strCompare;
97
            default:
98 1
                throw new UnexpectedValueException("Unknown comparson operator ({$operator}). Permitted >, <, >=, <=, =, !=");
99
        }
100
    }
101
102
    /**
103
     * Return error message.
104
     *
105
     * @return string Error message
106
     */
107 8
    public function getMessage(): string
108
    {
109 8
        return $this->message;
110
    }
111
}
112