Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
DateTime
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
3 / 3
11
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 validate
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
8
 getInverseError
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Verja\Validator;
4
5use Verja\Error;
6use Verja\Validator;
7
8class DateTime extends Validator
9{
10    /** @var string */
11    protected $format;
12
13    /** @var bool */
14    protected $strict;
15
16    /**
17     * DateTime constructor.
18     *
19     * When strict is set to true the resulting date will be formatted back using the given format and the value
20     * has to equal this value. This is helpful to prevent month and day interchanges but will fail for some natural
21     * date formats like `'dS of F Y' => '21st of January 2016`.
22     *
23     * @param string $format
24     * @param bool   $strict
25     */
26    public function __construct(string $format = null, bool $strict = false)
27    {
28        $this->format = $format;
29        $this->strict = $strict;
30    }
31
32
33    /**
34     * Validate $value
35     *
36     * @param mixed $value
37     * @param array $context
38     * @return bool
39     */
40    public function validate($value, array $context = []): bool
41    {
42        if ($value instanceof \DateTime) {
43            return true;
44        }
45
46        if (empty($this->format) && strtotime($value) === false) {
47            $this->error = new Error('NO_DATE', $value, 'value should be a valid date');
48            return false;
49        }
50
51        if (!empty($this->format)) {
52            $date = date_create_from_format($this->format, $value);
53            if ($date === false || $this->strict && $date->format($this->format) !== $value) {
54                $this->error = new Error(
55                    'NO_FORMATTED_DATE',
56                    $value,
57                    sprintf('value should be a valid date in format %s', $this->format),
58                    [ 'format' => $this->format, 'strict' => $this->strict ]
59                );
60                return false;
61            }
62        }
63
64        return true;
65    }
66
67    public function getInverseError($value)
68    {
69        return is_null($this->format) ?
70            new Error('IS_DATE', $value, 'value should not be a valid date') :
71            new Error(
72                'IS_FORMATTED_DATE',
73                $value,
74                sprintf(
75                    'value should not be valid date in format %s',
76                    $this->format
77                ),
78                [ 'format' => $this->format, 'strict' => $this->strict ]
79            );
80    }
81}