-
Notifications
You must be signed in to change notification settings - Fork 506
/
DataTransformer.php
234 lines (211 loc) · 5.73 KB
/
DataTransformer.php
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* Ushahidi Data Transformer Trait
*
* Gives objects new `transform($data)` and `getDefinition()` methods,
* which can be used to ensure data type consistency.
*
* @todo rename to differentiate from Transformer tools
*
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi\Platform
* @copyright 2014 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
*/
namespace Ushahidi\Core\Traits;
trait DataTransformer
{
/**
* Transform a string into an email, removing all characters that cannot
* exist in an email address.
*
* @return String $value
* @return String
*/
protected static function transformEmail($value)
{
return filter_var($value, FILTER_SANITIZE_EMAIL);
}
/**
* Transforms a JSON string to native type. Objects will be represented
* with associative arrays.
*
* @param String $value
* @return Mixed
*/
protected static function transformJson($value)
{
$originalVal = $value;
if (is_string($value)) {
$value = json_decode($value, true);
if (json_last_error() !== JSON_ERROR_NONE) {
$value = $originalVal;
}
}
if (static::optionJsonAlwaysArray()) {
$value = (array) $value;
}
return $value;
}
/**
* Requires that all `json` type fields are returned as arrays.
*
* @return Boolean
*/
protected static function optionJsonAlwaysArray()
{
return true;
}
/**
* Transform a string to a slug, replacing non-alphanumeric characters
* with dashes.
*
* @param String $value
* @return String
*/
protected static function transformSlug($value)
{
// Make it lowercase
$value = mb_strtolower($value, 'utf-8');
// .. anything not the separator, letters, numbers or whitespace is replaced
$value = preg_replace('/[^\pL\pN\-\s]+/u', '', $value);
// .. replace whitespace and multiple separator chars with a single separator
$value = preg_replace('/[\-\s]+/u', '-', $value);
// ... and replace spaces with hypens
return str_replace(' ', '-', $value);
}
/**
* Transform a string into a URL, removing all characters that cannot
* exist in a URL address.
*
* @return String $value
* @return String
*/
protected static function transformUrl($value)
{
return filter_var($value, FILTER_SANITIZE_URL);
}
/**
* Transforms a date(time) string to a UNIX timestamp.
*
* @param String $value
* @return Integer
*/
protected static function transformTimestamp($value)
{
// Convert a date string to a timestamp
return strtotime($value);
}
/**
* Transforms a date(time) string to a PHP Date
*
* @param String|DateTimeInterface $value
* @return DateTimeInterface
*/
protected function transformDate($value)
{
// If this is already a DateTime object clone it
if ($value instanceof \DateTimeInterface) {
$value = clone $value;
} elseif (is_array($value)) {
$value = new \DateTime($value['date'], new \DateTimeZone($value['timezone']));
} else {
// Convert post_date to DateTime
$trialValue = date_create($value, new \DateTimeZone('UTC'));
// If that didn't work, try assuming treating the value as a
$value = $trialValue ?: date_create('@'.$value, new \DateTimeZone('UTC'));
}
// Always use UTC
$value->setTimezone(new \DateTimeZone('UTC'));
return $value;
}
/**
* Transforms a string to a lowercase string.
*
* @param String $value
* @return Integer
*/
protected static function transformLowercasestring($value)
{
// Convert a string to lowercase
return mb_strtolower($value, 'utf-8');
}
/**
* Transforms all values in an array to ints
*
* @param String $value
* @return Integer
*/
protected static function transformArrayInt($value)
{
return array_map('intval', $value);
}
/**
* Get the custom transformer name for a type, if it exists.
*
* Custom transform types are denoted by prepending the type with a star:
*
* 'foo' => '*custom',
*
* This example would call `static::transformCustom` on the `foo` value.
*
* @param String $type
* @return Boolean
*/
protected function getCustomTransformer($type)
{
if ('*' === $type[0]) {
return 'transform' . ucfirst(substr($type, 1));
}
}
/**
* Transform an array of data, setting correct types to ensure consistency.
*
* NOTE: Unless an anonymous function is used, null values in the data will
* be ignored! Any definition that uses a closure will always be executed.
*
* @param Array $data
* @return Array
*/
protected function transform(array $data)
{
$definition = $this->getDefinition();
foreach ($data as $key => $val) {
if (!isset($definition[$key])) {
continue;
}
if ($definition[$key] instanceof \Closure) {
// Closures are always executed, regardless of value type.
$data[$key] = $definition[$key]($val);
} elseif (is_array($val) && is_array($definition[$key])) {
// Arrays can be recursively transformed.
$data[$key] = $this->transform($data[$key], $definition[$key]);
} elseif ($definition[$key] === false) {
// Definition requires the value to be removed.
unset($data[$key]);
} elseif (null !== $val) {
if ($func = $this->getCustomTransformer($definition[$key])) {
// Use a custom transformer for this type.
$data[$key] = static::$func($data[$key]);
} else {
// Cast the value to the specified type.
settype($data[$key], $definition[$key]);
}
}
}
return $data;
}
/**
* Return the transform definition for this object:
*
* return [
* 'id' => 'int',
* 'username' => 'string',
* 'role' => 'string',
* 'email' => function($val) { return filter_val($val, FILTER_SANITIZE_EMAIL); }
* ];
*
* @return Array
*/
abstract protected function getDefinition();
}