Length   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 97.3%

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 0
dl 0
loc 111
ccs 36
cts 37
cp 0.973
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C apply() 0 27 12
A max() 0 4 1
A min() 0 4 1
A equal() 0 4 1
A between() 0 6 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Caridea
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2018 LibreWorks contributors
19
 * @license   Apache-2.0
20
 */
21
namespace Caridea\Validate\Rule;
22
23
/**
24
 * Compares string length to accepted boundaries.
25
 *
26
 * @copyright 2015-2018 LibreWorks contributors
27
 * @license   Apache-2.0
28
 */
29
class Length implements \Caridea\Validate\Rule
30
{
31
    /**
32
     * @var string The operator type
33
     */
34
    private $operator;
35
    /**
36
     * @var int|int[] The length comparison
37
     */
38
    private $length;
39
    /**
40
     * @var string The encoding to pass to `mb_strlen`
41
     */
42
    private $encoding;
43
44
    /**
45
     * Creates a new LengthRule.
46
     *
47
     * @param string $operator The operator type
48
     * @param int|int[] $length The length comparison
49
     * @param string $encoding The encoding to pass to `mb_strlen`
50
     */
51 4
    protected function __construct(string $operator, $length, string $encoding = 'UTF-8')
52
    {
53 4
        $this->operator = $operator;
54 4
        $this->length = $length;
55 4
        $this->encoding = $encoding;
56 4
    }
57
58
    /**
59
     * {@inheritDoc}
60
     */
61 4
    public function apply($value, $data = []): ?array
62
    {
63 4
        if (!is_string($value)) {
64 4
            return ['FORMAT_ERROR'];
65
        }
66 4
        $length = mb_strlen($value, $this->encoding);
67 4
        switch ($this->operator) {
68 4
            case "lt":
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
69 1
                return $length > $this->length ? ['TOO_LONG'] : null;
70 3
            case "gt":
71 1
                return $length < $this->length ? ['TOO_SHORT'] : null;
72 2
            case "eq":
73 1
                if ($length > $this->length) {
74 1
                    return ['TOO_LONG'];
75 1
                } elseif ($length < $this->length) {
76 1
                    return ['TOO_SHORT'];
77
                }
78 1
                return null;
79 1
            case "bt":
80 1
                if ($length > $this->length[1]) {
81 1
                    return ['TOO_LONG'];
82 1
                } elseif ($length < $this->length[0]) {
83 1
                    return ['TOO_SHORT'];
84
                }
85 1
                return null;
86
        }
87
    }
88
89
    /**
90
     * Gets a rule that requires strings to be no longer than the limit.
91
     *
92
     * @param int $length The maximum length
93
     * @param string $encoding The string encoding
94
     * @return \Caridea\Validate\Rule\Length the created rule
95
     */
96 1
    public static function max(int $length, string $encoding = 'UTF-8'): Length
97
    {
98 1
        return new Length('lt', $length, $encoding);
99
    }
100
101
    /**
102
     * Gets a rule that requires strings to be no shorter than the limit.
103
     *
104
     * @param int $length The minimum length
105
     * @param string $encoding The string encoding
106
     * @return \Caridea\Validate\Rule\Length the created rule
107
     */
108 1
    public static function min(int $length, string $encoding = 'UTF-8'): Length
109
    {
110 1
        return new Length('gt', $length, $encoding);
111
    }
112
113
    /**
114
     * Gets a rule that requires strings to be exactly the length of the limit.
115
     *
116
     * @param int $length The required length
117
     * @param string $encoding The string encoding
118
     * @return \Caridea\Validate\Rule\Length the created rule
119
     */
120 1
    public static function equal(int $length, string $encoding = 'UTF-8'): Length
121
    {
122 1
        return new Length('eq', $length, $encoding);
123
    }
124
125
    /**
126
     * Gets a rule that requires strings to have a minimum and maximum length.
127
     *
128
     * @param int $min The minimum length, inclusive
129
     * @param int $max The maximum length, inclusive
130
     * @param string $encoding The string encoding
131
     * @return \Caridea\Validate\Rule\Length the created rule
132
     */
133 1
    public static function between(int $min, int $max, string $encoding = 'UTF-8'): Length
134
    {
135 1
        $length = [$min, $max];
136 1
        sort($length);
137 1
        return new Length('bt', $length, $encoding);
138
    }
139
}
140