1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JosKolenberg\EloquentEmptyDateNullifier; |
4
|
|
|
|
5
|
|
|
trait NullifiesEmptyDates |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Add the date attributes to the attributes array. |
9
|
|
|
* |
10
|
|
|
* @param array $attributes |
11
|
|
|
* @return array |
12
|
|
|
*/ |
13
|
16 |
|
protected function addDateAttributesToArray(array $attributes) |
14
|
|
|
{ |
15
|
16 |
|
foreach ($this->getDates() as $key) { |
|
|
|
|
16
|
16 |
|
if (! isset($attributes[$key])) { |
17
|
16 |
|
continue; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Additional check for valid date. |
22
|
|
|
*/ |
23
|
16 |
|
if (! $this->isValidDate($attributes[$key])) { |
24
|
16 |
|
$attributes[$key] = null; |
25
|
|
|
|
26
|
16 |
|
continue; |
27
|
|
|
} |
28
|
|
|
|
29
|
16 |
|
$attributes[$key] = $this->serializeDate($this->asDateTime($attributes[$key])); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
16 |
|
return $attributes; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Cast an attribute to a native PHP type. |
37
|
|
|
* |
38
|
|
|
* @param string $key |
39
|
|
|
* @param mixed $value |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
20 |
|
protected function castAttribute($key, $value) |
43
|
|
|
{ |
44
|
|
|
/** |
45
|
|
|
* Additional check for valid date if it needs casting |
46
|
|
|
* to a date. Proceed to normal behaviour otherwise. |
47
|
|
|
*/ |
48
|
20 |
|
if (in_array($this->getCastType($key), [ |
|
|
|
|
49
|
20 |
|
'date', |
50
|
|
|
'datetime', |
51
|
|
|
'custom_datetime', |
52
|
|
|
'timestamp', |
53
|
20 |
|
]) && ! $this->isValidDate($value)) { |
54
|
20 |
|
return null; |
55
|
|
|
} |
56
|
|
|
|
57
|
16 |
|
return parent::castAttribute($key, $value); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Check if the given value is not on the empty dates blacklist. |
62
|
|
|
* |
63
|
|
|
* @param $value |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
20 |
|
protected function isValidDate($value) |
67
|
|
|
{ |
68
|
20 |
|
return ! in_array($value, [ |
69
|
20 |
|
'0000-00-00 00:00:00', |
70
|
|
|
'0000-00-00', |
71
|
|
|
]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get a plain attribute (not a relationship). |
76
|
|
|
* |
77
|
|
|
* Additional check for valid date. Proceed to |
78
|
|
|
* normal behaviour when date is valid. |
79
|
|
|
* |
80
|
|
|
* @param string $key |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
12 |
|
public function getAttributeValue($key) |
84
|
|
|
{ |
85
|
12 |
|
$value = $this->getAttributeFromArray($key); |
|
|
|
|
86
|
|
|
|
87
|
12 |
|
if (in_array($key, $this->getDates()) && ! $this->isValidDate($value)) { |
88
|
4 |
|
return null; |
89
|
|
|
} |
90
|
|
|
|
91
|
12 |
|
return parent::getAttributeValue($key); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Convert a DateTime to a storable string. |
96
|
|
|
* |
97
|
|
|
* Additional check for valid date. Proceed to |
98
|
|
|
* normal behaviour when date is valid. |
99
|
|
|
* |
100
|
|
|
* @param mixed $value |
101
|
|
|
* @return string|null |
102
|
|
|
*/ |
103
|
8 |
|
public function fromDateTime($value) |
104
|
|
|
{ |
105
|
8 |
|
if (! $this->isValidDate($value)) { |
106
|
8 |
|
return null; |
107
|
|
|
} |
108
|
|
|
|
109
|
8 |
|
return parent::fromDateTime($value); |
110
|
|
|
} |
111
|
|
|
} |