Completed
Push — master ( 5ad706...418f81 )
by Sebastian
03:53
created

Date   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 88.46%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 115
ccs 23
cts 26
cp 0.8846
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDateTimeObject() 0 9 2
A getMessage() 0 3 1
A concreteValidate() 0 10 2
A parseDate() 0 17 3
A validate() 0 5 1
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
        'class' => 'Date',
27
        'full_class' => __CLASS__,
28
        'alias' => ['date', 'dat', 'd'],
29
        'args_count' => 1,
30
        'args_type' => ['string'],
31
        'has_validate' => true,
32
        //'has_sanitize' => false
33
    ];
34
35
    /**
36
     * @var string Valid date.
37
     */
38
    private $date;
39
40
    /**
41
     * @var string Date format.
42
     */
43
    private $format;
44
45
    /**
46
     * @var string Error message
47
     */
48
    private $message = '';
49
50
    /**
51
     * Validate.
52
     *
53
     * @return bool
54
     */
55 28
    public function validate(): bool
56
    {
57 28
        $args = func_get_args();
58
59 28
        return $this->concreteValidate($args[0], $args[1]);
60
    }
61
62
    /**
63
     * Concrete validate.
64
     *
65
     * @param string $received
66
     * @param string $format
67
     *
68
     * @return bool
69
     */
70 28
    private function concreteValidate(string $received, string $format): bool
71
    {
72 28
        if ($this->parseDate($received, $format)) {
73 19
            return true;
74
        }
75
76 9
        $this->date = $received;
77 9
        $this->format = $format;
78
79 9
        return false;
80
    }
81
82
    /**
83
     * Parse date.
84
     *
85
     * @param string $received
86
     * @param string $format
87
     *
88
     * @return bool
89
     */
90 28
    private function parseDate(string $received, string $format): bool
91
    {
92 28
        $date = date_parse_from_format($format, $received);
93
94 28
        $message = "Received date is not in expected format {$format}";
95
96 28
        if ($date['warning_count']) {
97 9
            $this->message = $message;
98 9
            return true;
99
        }
100
101 19
        if ($date['error_count']) {
102 10
            $this->message = $message;
103 10
            return true;
104
        }
105
106 9
        return false;
107
    }
108
109
    /**
110
     * Return DateTime object.
111
     *
112
     * @return DateTime
113
     *
114
     * @throws UnexpectedValueException
115
     */
116 6
    public function getDateTimeObject(): DateTime
117
    {
118 6
        $dateTimeObject = DateTime::createFromFormat($this->format, $this->date);
119
120 6
        if (!($dateTimeObject instanceof DateTime)) {
121
            throw new UnexpectedValueException();
122
        }
123
124 6
        return $dateTimeObject;
125
    }
126
127
    /**
128
     * Return error message.
129
     *
130
     * @return string Error message
131
     */
132
    public function getMessage(): string
133
    {
134
        return $this->message;
135
    }
136
}
137