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
|
2 |
|
public function bDateNullable(?string $value = null): array |
22
|
|
|
{ |
23
|
2 |
|
if ($value === null) { |
24
|
1 |
|
return [null, PDO::PARAM_NULL]; |
25
|
|
|
} |
26
|
|
|
|
27
|
1 |
|
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
|
2 |
|
public function bDateTimeNullable(?string $value = null): array |
59
|
|
|
{ |
60
|
2 |
|
if ($value === null) { |
61
|
1 |
|
return [null, PDO::PARAM_NULL]; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
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
|
|
|
|