Date::validate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Linna Filter
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Filter\Rules;
13
14
use DateTime;
15
use UnexpectedValueException;
16
17
/**
18
 * Check if one date is valid.
19
 */
20
class Date extends AbstractDate implements RuleValidateInterface
21
{
22
    /**
23
     * @var array Rule properties
24
     */
25
    public static $config = [
26
        'full_class' => __CLASS__,
27
        'alias' => ['date', 'dat', 'd'],
28
        'args_count' => 1,
29
        'args_type' => ['string']
30
    ];
31
32
    /**
33
     * @var string Valid date.
34
     */
35
    private $date;
36
37
    /**
38
     * @var string Date format.
39
     */
40
    private $format;
41
42
    /**
43
     * @var string Error message
44
     */
45
    private $message = '';
46
47
    /**
48
     * Validate.
49
     *
50
     * @return bool
51
     */
52 29
    public function validate(): bool
53
    {
54 29
        $args = \func_get_args();
55
56 29
        return $this->concreteValidate($args[0], $args[1]);
57
    }
58
59
    /**
60
     * Concrete validate.
61
     *
62
     * @param string $received
63
     * @param string $format
64
     *
65
     * @return bool
66
     */
67 29
    private function concreteValidate(string $received, string $format): bool
68
    {
69 29
        if ($this->parseDate($received, $format)) {
70 20
            return true;
71
        }
72
73 9
        $this->date = $received;
74 9
        $this->format = $format;
75
76 9
        return false;
77
    }
78
79
    /**
80
     * Parse date.
81
     *
82
     * @param string $received
83
     * @param string $format
84
     *
85
     * @return bool
86
     */
87 29
    private function parseDate(string $received, string $format): bool
88
    {
89 29
        $date = \date_parse_from_format($format, $received);
90
91 29
        $message = "Received date is not in expected format {$format}";
92
93 29
        if ($date['warning_count']) {
94 9
            $this->message = $message;
95 9
            return true;
96
        }
97
98 20
        if ($date['error_count']) {
99 11
            $this->message = $message;
100 11
            return true;
101
        }
102
103 9
        return false;
104
    }
105
106
    /**
107
     * Return DateTime object.
108
     *
109
     * @return DateTime
110
     *
111
     * @throws UnexpectedValueException
112
     */
113 6
    public function getDateTimeObject(): DateTime
114
    {
115 6
        $dateTimeObject = DateTime::createFromFormat($this->format, $this->date);
116
117 6
        if (!($dateTimeObject instanceof DateTime)) {
118
            throw new UnexpectedValueException('Failed to create DateTime object.');
119
        }
120
121 6
        return $dateTimeObject;
122
    }
123
124
    /**
125
     * Return error message.
126
     *
127
     * @return string Error message
128
     */
129 1
    public function getMessage(): string
130
    {
131 1
        return $this->message;
132
    }
133
}
134