Completed
Push — master ( 39ca89...64a0db )
by James Ekow Abaka
03:49
created

LengthValidation::run()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5.0406

Importance

Changes 0
Metric Value
dl 0
loc 23
c 0
b 0
f 0
ccs 15
cts 17
cp 0.8824
rs 8.5906
cc 5
eloc 18
nc 2
nop 2
crap 5.0406
1
<?php
2
3
namespace ntentan\utils\validator\validations;
4
use ntentan\utils\validator\Validation;
5
6
class LengthValidation extends Validation
7
{
8 2
    public function run($field, $data)
9
    {
10 2
        $value = $this->getFieldValue($field, $data);        
11 2
        $length = strlen($value);
12 2
        if (is_array($field['options'])) {
13 1
            $hasMin = isset($field['options']['min']);
14 1
            $hasMax = isset($field['options']['max']);
15 1
            $max = $field['options']['max'];
16 1
            $min = $field['options']['min'];
17 1
            return $this->evaluateResult(
18
                $field,
19 1
                ($hasMax ? $length <= $max : true) &&
20 1
                ($hasMin ? $length >= $min : true), 
21 1
                $this->getLenghtValidationMessage($field['name'], $hasMin, $hasMax, $min, $max)
22
            );
23
        } else {
24 1
            return $this->evaluateResult(
25
                $field,
26 1
                $length <= $field['options'], 
27 1
                "The length of the {$field['name']} field must be {$field['options']} characters or less"
28
            );
29
        }
30
    }
31
32 1
    private function getLenghtValidationMessage($name, $hasMin, $hasMax, $min, $max)
33
    {
34 1
        return "The length of the {$name} field must be" .
35 1
            ($hasMin ? " $min characters or greater" : '') .
36 1
            ($hasMax && $hasMin ? " and" : '') .
37 1
            ($hasMax ? " $max characters or lesser" : '');
38
    }
39
}