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