DateValidator::format()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
namespace Comfort\Validator;
3
4
class DateValidator extends AbstractValidator
5
{
6
    /**
7
     * Specify format date must be in
8
     *
9
     * @param string $format
10
     * @return $this
11
     */
12
    public function format($format)
13
    {
14
        return $this->add(function ($value, $namekey) use ($format) {
15
            $date = \DateTime::createFromFormat($format, $value);
16
            if (false === $date) {
17
                return $this->createError('date.format', $value, $namekey);
18
            }
19
        });
20
    }
21
22
    /**
23
     * Specify min date
24
     *
25
     * @param $date
26
     * @return $this
27
     */
28 View Code Duplication
    public function min($date)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
    {
30
        return $this->add(function ($value, $nameKey) use ($date) {
31
            $minDate = strtotime($date);
32
            $curDate = strtotime($value);
33
34
            if ($curDate < $minDate) {
35
                return $this->createError('date.min', $value, $nameKey);
36
            }
37
        });
38
    }
39
40
    /**
41
     * Specify max date
42
     *
43
     * @param $date
44
     * @return $this
45
     */
46 View Code Duplication
    public function max($date)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        return $this->add(function ($value, $nameKey) use ($date) {
49
            $maxDate = strtotime($date);
50
            $curDate = strtotime($value);
51
52
            if ($curDate > $maxDate) {
53
                return $this->createError('date.max', $value, $nameKey);
54
            }
55
        });
56
    }
57
58
    /**
59
     * Date must be a unix timestamp
60
     *
61
     * @return $this
62
     */
63
    public function timestamp()
64
    {
65
        return $this->add(function ($value, $nameKey) {
66
            try {
67
                new \DateTime('@' . $value);
68
            } catch (\Exception $e) {
69
                return $this->createError('date.timestamp', $value, $nameKey);
70
            }
71
        });
72
    }
73
74
75
    /**
76
     * Date must be in ISO format
77
     *
78
     * @return $this
79
     */
80
    public function iso()
81
    {
82
        return $this->format(DATE_ISO8601);
83
    }
84
}
85