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

DateValidation::isValid()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 10
cc 4
nc 4
nop 1
crap 4
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
}