Passed
Push — master ( 5d30f6...461084 )
by Alexey
02:28
created

DateValidation   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 10
c 1
b 0
f 0
dl 0
loc 23
ccs 13
cts 13
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A errorMessage() 0 5 2
A isValid() 0 8 4
A afterSuccessValidation() 0 4 1
1
<?php
2
3
4
namespace Lexuss1979\Validol\Validations;
5
6
7
use DateTime;
8
use Lexuss1979\Validol\ValueObject;
9
10
class DateValidation extends AbstractValidation implements ValidationInterface
11
{
12 60
    public function isValid(ValueObject $data)
13
    {
14 60
        if( ! is_string($data->value()) ) return false;
15 54
        if(isset($this->options[0])){
16 11
            $dateObj = DateTime::createFromFormat($this->options[0], $data->value());
17 11
            return $dateObj && $dateObj->format($this->options[0]) === $data->value();
18
        }
19 43
       return strtotime($data->value()) > 0;
20
    }
21
22 35
    protected function afterSuccessValidation()
23
    {
24 35
        $this->data->setType(ValueObject::DATE);
25 35
        parent::afterSuccessValidation();
26 35
    }
27
28 25
    public function errorMessage()
29
    {
30 25
        if(isset($this->options[0]))   return "{$this->data->name()} is not in date format {$this->options[0]}";
31
32 18
        return "{$this->data->name()} must be a valid date";
33
    }
34
}