|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Divergence package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Henry Paradiz <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace Divergence\Models\SetMappers; |
|
11
|
|
|
|
|
12
|
|
|
use Divergence\IO\Database\MySQL as DB; |
|
13
|
|
|
use Divergence\Models\Interfaces\FieldSetMapper; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* This is the default type mapping from PHP userspace to our Model field value |
|
17
|
|
|
* |
|
18
|
|
|
* @package Divergence |
|
19
|
|
|
* @author Henry Paradiz <[email protected]> |
|
20
|
|
|
* |
|
21
|
|
|
*/ |
|
22
|
|
|
class DefaultSetMapper implements FieldSetMapper |
|
23
|
|
|
{ |
|
24
|
|
|
public function setStringValue($value): ?string |
|
25
|
|
|
{ |
|
26
|
|
|
return mb_convert_encoding($value, DB::$encoding, 'auto'); // normalize encoding to ASCII |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function setBooleanValue($value): bool |
|
30
|
|
|
{ |
|
31
|
|
|
return (boolean)$value; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function setDecimalValue($value): ?float |
|
35
|
|
|
{ |
|
36
|
|
|
return is_null($value) ? null : (float)preg_replace('/[^-\d.]/', '', $value); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function setIntegerValue($value): ?int |
|
40
|
|
|
{ |
|
41
|
|
|
return is_null($value) ? null : (int)preg_replace('/[^-\d]/', '', $value); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function setTimestampValue($value): ?string |
|
45
|
|
|
{ |
|
46
|
|
|
if (is_numeric($value)) { |
|
47
|
|
|
return date('Y-m-d H:i:s', $value); |
|
48
|
|
|
} elseif (is_string($value)) { |
|
49
|
|
|
// trim any extra stuff, or leave as-is if it doesn't fit the pattern |
|
50
|
|
|
if (preg_match('/^(\d{4})\D?(\d{2})\D?(\d{2})T?(\d{2})\D?(\d{2})\D?(\d{2})/', $value)) { |
|
51
|
|
|
return preg_replace('/^(\d{4})\D?(\d{2})\D?(\d{2})T?(\d{2})\D?(\d{2})\D?(\d{2})/', '$1-$2-$3 $4:$5:$6', $value); |
|
52
|
|
|
} else { |
|
53
|
|
|
return date('Y-m-d H:i:s', strtotime($value)); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
return null; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Parses a potential date value. |
|
61
|
|
|
* - If passed a number will assume it's a unix timestamp and convert to Y-m-d based on the provided timestamp. |
|
62
|
|
|
* - If passed a string will attempt to match m/d/y format. |
|
63
|
|
|
* - If not valid will then attempt Y/m/d |
|
64
|
|
|
* - If passed an array will attempt to look for numeric values in the keys 'yyyy' for year, 'mm' for month, and 'dd' for day. |
|
65
|
|
|
* - If none of the above worked will attempt to use PHP's strtotime. |
|
66
|
|
|
* - Otherwise null. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string|int|array $value Date or timestamp in various formats. |
|
69
|
|
|
* @return null|string Date formatted as Y-m-d |
|
70
|
|
|
*/ |
|
71
|
|
|
public function setDateValue($value): ?string |
|
72
|
|
|
{ |
|
73
|
|
|
if (is_numeric($value)) { |
|
74
|
|
|
$value = date('Y-m-d', $value); |
|
|
|
|
|
|
75
|
|
|
} elseif (is_string($value)) { |
|
76
|
|
|
// check if m/d/y format |
|
77
|
|
|
if (preg_match('/^(\d{2})\D?(\d{2})\D?(\d{4}).*/', $value)) { |
|
78
|
|
|
$value = preg_replace('/^(\d{2})\D?(\d{2})\D?(\d{4}).*/', '$3-$1-$2', $value); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// trim time and any extra crap, or leave as-is if it doesn't fit the pattern |
|
82
|
|
|
$value = preg_replace('/^(\d{4})\D?(\d{2})\D?(\d{2}).*/', '$1-$2-$3', $value); |
|
83
|
|
|
} elseif (is_array($value) && count(array_filter($value))) { |
|
84
|
|
|
// collapse array date to string |
|
85
|
|
|
$value = sprintf( |
|
86
|
|
|
'%04u-%02u-%02u', |
|
87
|
|
|
is_numeric($value['yyyy']) ? $value['yyyy'] : 0, |
|
88
|
|
|
is_numeric($value['mm']) ? $value['mm'] : 0, |
|
89
|
|
|
is_numeric($value['dd']) ? $value['dd'] : 0 |
|
90
|
|
|
); |
|
91
|
|
|
} else { |
|
92
|
|
|
if ($value = strtotime($value)) { |
|
93
|
|
|
$value = date('Y-m-d', $value) ?: null; |
|
94
|
|
|
} else { |
|
95
|
|
|
$value = null; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
return $value; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function setSerializedValue($value): string |
|
102
|
|
|
{ |
|
103
|
|
|
return serialize($value); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function setEnumValue(array $values, $value) |
|
107
|
|
|
{ |
|
108
|
|
|
return (in_array($value, $values) ? $value : null); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function setListValue($value, ?string $delimiter): array |
|
112
|
|
|
{ |
|
113
|
|
|
if (!is_array($value)) { |
|
114
|
|
|
$delim = empty($delimiter) ? ',' : $delimiter; |
|
115
|
|
|
$value = array_filter(preg_split('/\s*'.$delim.'\s*/', $value)); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
return $value; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|