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

LengthValidation   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 34
c 0
b 0
f 0
ccs 20
cts 22
cp 0.9091
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 23 5
B getLenghtValidationMessage() 0 7 5
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
}