Completed
Push — master ( 9588f6...a8c5db )
by Jonathan
03:49
created

Blank::apply()   B

Complexity

Conditions 11
Paths 11

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 11.055

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 12
cts 13
cp 0.9231
rs 7.1162
c 0
b 0
f 0
cc 11
eloc 12
nc 11
nop 2
crap 11.055

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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-2016 LibreWorks contributors
19
 * @license   http://opensource.org/licenses/Apache-2.0 Apache 2.0 License
20
 */
21
namespace Caridea\Validate\Rule;
22
23
/**
24
 * Rules for empty values
25
 *
26
 * @copyright 2015-2016 LibreWorks contributors
27
 * @license   http://opensource.org/licenses/Apache-2.0 Apache 2.0 License
28
 */
29
class Blank implements \Caridea\Validate\Rule
30
{
31
    /**
32
     * @var string The operator type
33
     */
34
    private $operator;
35
    
36
    /**
37
     * Creates a new EmptyRule.
38
     *
39
     * @param string $operator The operator type
40
     */
41 3
    protected function __construct(string $operator)
42
    {
43 3
        $this->operator = $operator;
44 3
    }
45
    
46
    /**
47
     * Validates the provided value.
48
     *
49
     * @param mixed $value A value to validate against the rule
50
     * @param array|object $data The dataset which contains this field
51
     * @return array An array of error codes or null if validation succeeded
52
     */
53 3
    public function apply($value, $data = [])
54
    {
55 3
        switch ($this->operator) {
56 3
            case "required":
57 1
                return $value === null || $value === '' ? ['REQUIRED'] : null;
58 2
            case "empty":
59 1
                return $value === '' ? ['CANNOT_BE_EMPTY'] : null;
60 1
            case "list":
61 1
                if (empty($value)) {
62 1
                    return ['CANNOT_BE_EMPTY'];
63 1
                } elseif (!is_array($value) && !($value instanceof \Countable)) {
64 1
                    return ['FORMAT_ERROR'];
65
                }
66 1
                return count($value) === 0 ? ['CANNOT_BE_EMPTY'] : null;
67
        }
68
    }
69
    
70
    /**
71
     * Gets a rule that requires values to be non-null and not empty string.
72
     *
73
     * @return \Caridea\Validate\Rule\Blank the created rule
74
     */
75 1
    public static function required(): Blank
76
    {
77 1
        return new Blank('required');
78
    }
79
    
80
    /**
81
     * Gets a rule that requires strings to be non-empty.
82
     *
83
     * @return \Caridea\Validate\Rule\Blank
84
     */
85 1
    public static function notEmpty(): Blank
86
    {
87 1
        return new Blank('empty');
88
    }
89
    
90
    /**
91
     * Gets a rule that requires an array or `Countable` to be non-empty.
92
     *
93
     * @return \Caridea\Validate\Rule\Blank
94
     */
95 1
    public static function notEmptyList(): Blank
96
    {
97 1
        return new Blank('list');
98
    }
99
}
100