DateValidation::isDateValid()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
ccs 7
cts 8
cp 0.875
cc 4
eloc 9
nc 4
nop 1
crap 4.0312
1
<?php
2
declare(strict_types = 1);
3
4
namespace AmmitPhp\Ammit\Domain;
5
6
class DateValidation
7
{
8
    const FORMAT_SIMPLE = 'Y-m-d';
9
    const FORMAT_RFC3339 = \DateTime::RFC3339;
10
11
    /**
12
     * Valid against Y-m-d format
13
     * @param string $dateString String to validate
14
     * @return bool
15
     */
16
    public function isDateValid(string $dateString): bool
17
    {
18 1
        if (!$this->isValidDateAgainstRegex($dateString)) {
19 1
            return false;
20
        }
21
22 1
        $date = $this->createDateFromString($dateString);
23 1
        if (false === $date) {
24
            return false;
25
        }
26
27 1
        if ($date->format(self::FORMAT_SIMPLE) === $dateString) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $date->format(sel...IMPLE) === $dateString;.
Loading history...
28 1
            return true;
29
        }
30
31 1
        return false;
32
    }
33
34
    public function isDateTimeValid(string $dateString): bool
35
    {
36 1
        if (!$this->isValidDateTimeAgainstRegex($dateString)) {
37 1
            return false;
38
        }
39
40 1
        $date = $this->createDateTimeFromString($dateString);
41 1
        if (false === $date) {
42
            return false;
43
        }
44
45 1
        if ($date->format(self::FORMAT_RFC3339) === $dateString) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $date->format(sel...C3339) === $dateString;.
Loading history...
46 1
            return true;
47
        }
48
49 1
        return false;
50
    }
51
52
    /**
53
     * @return \DateTime|false
54
     */
55
    public function createDateFromString(string $dateString)
56
    {
57 1
        return \DateTime::createFromFormat(self::FORMAT_SIMPLE, $dateString);
58
    }
59
60
    /**
61
     * @return \DateTime|false
62
     */
63
    public function createDateTimeFromString(string $dateString)
64
    {
65 1
        return \DateTime::createFromFormat(self::FORMAT_RFC3339, $dateString);
66
    }
67
68
    private function isValidDateAgainstRegex($string): bool
69
    {
70 1
        if (preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/',$string)) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return (bool) preg_match...]|3[0-1])$/', $string);.
Loading history...
71 1
            return true;
72
        }
73
74 1
        return false;
75
    }
76
77
    private function isValidDateTimeAgainstRegex($string): bool
78
    {
79 1
        if (preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])T(0[0-9]|1[0-9]|2[0-3]):\d\d:\d\d\+\d\d:\d\d$/',$string)) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return (bool) preg_match...\d:\\d\\d$/', $string);.
Loading history...
80 1
            return true;
81
        }
82
83 1
        return false;
84
    }
85
}
86