Issues (281)

Branch: master

src/Common/Core/FormDate.php (1 issue)

1
<?php
2
3
namespace Common\Core;
4
5
use SpoonFormDate;
6
7
/**
8
 * This is our extended version of \SpoonFormDate
9
 */
10
class FormDate extends SpoonFormDate
11
{
12
    /**
13
     * Checks if this field is correctly submitted.
14
     *
15
     * @param string $error The error message to set.
16
     *
17
     * @return bool
18
     */
19 2
    public function isValid($error = null): bool
20
    {
21 2
        $return = parent::isValid($error);
22
23 2
        if ($return === false) {
24
            return false;
25
        }
26
27 2
        $longMask = str_replace(['d', 'm', 'y', 'Y'], ['dd', 'mm', 'yy', 'yyyy'], $this->mask);
28
29 2
        $data = $this->getMethod(true);
30
31 2
        $year = (mb_strpos($longMask, 'yyyy') !== false) ? mb_substr(
32 2
            $data[$this->attributes['name']],
33 2
            mb_strpos($longMask, 'yyyy'),
34 2
            4
35 2
        ) : mb_substr($data[$this->attributes['name']], mb_strpos($longMask, 'yy'), 2);
36 2
        $month = mb_substr($data[$this->attributes['name']], mb_strpos($longMask, 'mm'), 2);
37 2
        $day = mb_substr($data[$this->attributes['name']], mb_strpos($longMask, 'dd'), 2);
38
39
        // validate datefields that have a from-date set
40 2
        if (mb_strpos($this->attributes['class'], 'inputDatefieldFrom') !== false) {
41
            $fromDateChunks = explode('-', $this->attributes['data-startdate']);
42
            $fromDateTimestamp = mktime(12, 00, 00, $fromDateChunks[1], $fromDateChunks[2], $fromDateChunks[0]);
0 ignored issues
show
A parse error occurred: The alleged octal '0' is invalid
Loading history...
43
44
            $givenDateTimestamp = mktime(12, 00, 00, $month, $day, $year);
45
46
            if ($givenDateTimestamp < $fromDateTimestamp) {
47
                if ($error !== null) {
48
                    $this->setError($error);
49
                }
50
51
                return false;
52
            }
53 2
        } elseif (mb_strpos($this->attributes['class'], 'inputDatefieldTill') !== false) {
54
            $tillDateChunks = explode('-', $this->attributes['data-enddate']);
55
            $tillDateTimestamp = mktime(12, 00, 00, $tillDateChunks[1], $tillDateChunks[2], $tillDateChunks[0]);
56
57
            $givenDateTimestamp = mktime(12, 00, 00, $month, $day, $year);
58
59
            if ($givenDateTimestamp > $tillDateTimestamp) {
60
                if ($error !== null) {
61
                    $this->setError($error);
62
                }
63
64
                return false;
65
            }
66 2
        } elseif (mb_strpos($this->attributes['class'], 'inputDatefieldRange') !== false) {
67
            $fromDateChunks = explode('-', $this->attributes['data-startdate']);
68
            $fromDateTimestamp = mktime(12, 00, 00, $fromDateChunks[1], $fromDateChunks[2], $fromDateChunks[0]);
69
70
            $tillDateChunks = explode('-', $this->attributes['data-enddate']);
71
            $tillDateTimestamp = mktime(12, 00, 00, $tillDateChunks[1], $tillDateChunks[2], $tillDateChunks[0]);
72
73
            $givenDateTimestamp = mktime(12, 00, 00, $month, $day, $year);
74
75
            if ($givenDateTimestamp < $fromDateTimestamp || $givenDateTimestamp > $tillDateTimestamp) {
76
                if ($error !== null) {
77
                    $this->setError($error);
78
                }
79
80
                return false;
81
            }
82
        }
83
84 2
        return true;
85
    }
86
}
87