Issues (48)

src/Bindings/MySQL/MySQLDateTimeBindings.php (2 issues)

1
<?php
2
3
namespace GeekLab\GLPDO2\Bindings\MySQL;
4
5
use PDO;
6
use InvalidArgumentException;
7
use GeekLab\GLPDO2\Constants;
8
use GeekLab\GLPDO2\Bindings\DateTimeBindingInterface;
9
10
class MySQLDateTimeBindings implements DateTimeBindingInterface, Constants
11
{
12
    /**
13
     * Bind a date value as date or optional NULL.
14
     * YYYY-MM-DD is the proper date format.
15
     *
16
     * @param string | null $value
17
     * @param bool          $null
18
     *
19
     * @return array{?string, int}
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{?string, int} at position 2 could not be parsed: Expected ':' at position 2, but found '?string'.
Loading history...
20
     * @throws InvalidArgumentException
21
     */
22 16
    public function bDate(?string $value, bool $null = false): array
23
    {
24 16
        if ($value === null && !$null) {
25 1
            throw new InvalidArgumentException('Can not bind NULL in date spot.');
26
        }
27
28 15
        if (empty($value) && $null) {
29 1
            return [null, PDO::PARAM_NULL];
30
        }
31
32 14
        if (!empty($value)) {
33 14
            $value = trim($value);
34 14
            return [preg_match(self::DATE_REGEX, $value) ? $value : '1970-01-01', PDO::PARAM_STR];
35
        }
36
37 1
        return ['1970-01-01', PDO::PARAM_STR];
38
    }
39
40
    /**
41
     * Bind a date value as date time or optional NULL.
42
     * YYYY-MM-DD HH:MM:SS is the proper date format.
43
     *
44
     * @param string | null $value
45
     * @param bool          $null
46
     *
47
     * @return array{?string, int}
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{?string, int} at position 2 could not be parsed: Expected ':' at position 2, but found '?string'.
Loading history...
48
     * @throws InvalidArgumentException
49
     */
50 15
    public function bDateTime(?string $value = null, bool $null = false): array
51
    {
52 15
        if ($value === null && !$null) {
53 1
            throw new InvalidArgumentException('Can not bind NULL in date time spot.');
54
        }
55
56 14
        $isDateTime = 0;
57
58 14
        if ($value !== null) {
59
            // Trim $value and see if it matches full date time string format.
60 13
            $isDateTime = preg_match(self::DATE_TIME_REGEX, trim($value));
61
        }
62
63
        // Use NULL?
64 14
        if ($isDateTime === 0 && $null) {
65 1
            return [null, PDO::PARAM_NULL];
66
        }
67
68 13
        if ($isDateTime === 0 && $value !== null) {
69
            // $value is not a valid date string, set to the earliest date time available (GMT).
70
            // Or $value is a valid date string, add midnight time.
71 1
            $value = preg_match(self::DATE_REGEX, $value) === 0 ? '1970-01-01 00:00:00' : $value . ' 00:00:00';
72
        }
73
74
        // DateTimes are really strings.
75 13
        return [$value, PDO::PARAM_STR];
76
    }
77
}
78