Passed
Push — develop ( 44a7ff...abefc0 )
by Kenneth
02:20
created

MySQLDateTimeBindings::bDateNullable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 0
b 0
f 0
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
use TypeError;
10
11
class MySQLDateTimeBindings implements DateTimeBindingInterface, Constants
12
{
13
    /**
14
     * Bind a date value as date or null.
15
     * YYYY-MM-DD is the proper date format.
16
     *
17
     * @param string|null $value
18
     *
19
     * @return array
20
     */
21 1
    public function bDateNullable(?string $value = null): array
22
    {
23 1
        if ($value === null) {
24 1
            return [null, PDO::PARAM_NULL];
25
        }
26
27
        return $this->bDate($value);
28
    }
29
30
    /**
31
     * Bind a date value as date.
32
     * YYYY-MM-DD is the proper date format.
33
     *
34
     * @todo Use PHP's date stuff for validation?
35
     *
36
     * @param string|null $value
37
     *
38
     * @return array
39
     */
40 15
    public function bDate(?string $value): array
41
    {
42 15
        if ($value === null) {
43 1
            throw new TypeError('Can not bind NULL in date spot.');
44
        }
45
46 14
        $value = trim($value);
47 14
        return [preg_match(self::DATE_REGEX, $value) ? $value : '1970-01-01', PDO::PARAM_STR];
48
    }
49
50
    /**
51
     * Bind a date value as date time or null.
52
     * YYYY-MM-DD HH:MM:SS is the proper date format.
53
     *
54
     * @param string|null $value
55
     *
56
     * @return array
57
     */
58 1
    public function bDateTimeNullable(?string $value = null): array
59
    {
60 1
        if ($value === null) {
61 1
            return [null, PDO::PARAM_NULL];
62
        }
63
64
        return $this->bDateTime($value);
65
    }
66
67
    /**
68
     * Bind a date value as date time.
69
     * YYYY-MM-DD HH:MM:SS is the proper date time format.
70
     *
71
     * @todo Use PHP's date stuff for validation?
72
     *
73
     * @param string|null $value
74
     *
75
     * @return array
76
     */
77 14
    public function bDateTime(?string $value): array
78
    {
79 14
        if ($value === null) {
80 1
            throw new TypeError('Can not bind NULL in date time spot.');
81
        }
82
83 13
        $value = trim($value);
84 13
        $isDateTime = preg_match(self::DATE_TIME_REGEX, trim($value));
85
86
87 13
        if ($isDateTime === 0) {
88
            // $value is not a valid date string, set to earliest date time available (GMT).
89
            // Or $value is a valid date string, add midnight time.
90 1
            $value = preg_match(self::DATE_REGEX, $value) === 0 ? '1970-01-01 00:00:00' : $value . ' 00:00:00';
91
        }
92
93
        // DateTimes are really strings.
94 13
        return [$value, PDO::PARAM_STR];
95
    }
96
}
97