-
Notifications
You must be signed in to change notification settings - Fork 113
/
formatter.php
456 lines (388 loc) · 9.59 KB
/
formatter.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
<?php
/**
* Formatter Bundle
*
* Port of the FuelPHP Formatter Bundle
* http://fuelphp.com/
*
* @package Formatter
* @version 1.0
* @author Daniel Berry <daniel@danielberry.me>
* @license MIT License (see LICENSE.readme included in the bundle)
*
*/
/**
* Setup the Formatter namespace
*/
namespace Formatter;
use Config, Exception;
class FormatterException extends Exception {}
/**
* The Formatter Class
*
* Makes it quick and easy to convert data between several different formats.
*
* @package Formatter
* @author Daniel Berry <danielberrytn@gmail.com>
*
*/
class Formatter
{
/**
* Holds the data that we are converting
* @var array
*/
protected $_data = array();
/**
* Returns an instance of the Formatter Bundle
* @param mixex $data the data we are converting
* @param [type] $from_type what we want to convert to
* @return Formatter
*/
public static function make($data = null, $from_type = null)
{
return new self($data, $from_type);
}
/**
* Should not be called directly. You should be using Formatter::make()
*
* Constructs our class and sets up some vars we will be using throughout
* the conversion process.
*
* @param mixed $data data we will be converting
* @param string $from_type what we are converting form
*/
public function __construct($data = null, $from_type = null)
{
// make sure we have data to convert to
if (empty($data))
{
throw new FormatterException(__('formatter::formatter.no_data', array('from_type' => $from_type)));
}
// make sure our from type has been specified.
if ($from_type !== null)
{
// check to make sure the method exists
if (method_exists($this, "_from_{$from_type}"))
{
$data = call_user_func(array($this, '_from_' . $from_type), $data);
}
else
{
throw new FormatterException(__('formatter::formatter.from_type_not_supported', array('from_type' => $from_type)));
}
}
// set up our data.
$this->_data = $data;
}
/**
* To array conversion
*
* Goes through the input and makes sure everything is either a scalar value or array
*
* @param mixed $data
* @return array
*/
public function to_array($data = null)
{
if ($data === null)
{
$data = $this->_data;
}
$array = array();
if (is_object($data) and ! $data instanceof \Iterator)
{
$data = get_object_vars($data);
}
if (empty($data))
{
return array();
}
foreach ($data as $key => $value)
{
if (is_object($value) or is_array($value))
{
$array[$key] = $this->to_array($value);
}
else
{
$array[$key] = $value;
}
}
return $array;
}
/**
* To CSV conversion
*
* @param mixed $data
* @param mixed $delimiter
* @return string
*/
public function to_csv($data = null, $delimiter = null)
{
// let's get the config file
$config = Config::get('formatter::format.csv');
// csv format settings
$newline = array_get($config, 'delimiter', "\n");
$delimiter or $delimiter = array_get($config, 'delimiter', ",");
$enclosure = array_get($config, 'delimiter', '"');
$escape = array_get($config, 'delimiter', "\\");
// escape function
$escaper = function($items) use($enclosure, $escape) {
return array_map(function($item) use($enclosure, $escape) {
return str_replace($enclosure, $escape.$enclosure, $item);
}, $items);
};
if ($data === null)
{
$data = $this->_data;
}
if (is_object($data) and ! $data instanceof \Iterator)
{
$data = $this->to_array($data);
}
// Multi-dimensional array
if (is_array($data) and self::is_multi($data))
{
$data = array_values($data);
if (self::is_assoc($data[0]))
{
$headings = array_keys($data[0]);
}
else
{
$headings = array_shift($data);
}
}
// Single array
else
{
$headings = array_keys((array) $data);
$data = array($data);
}
$output = $enclosure.implode($enclosure.$delimiter.$enclosure, $escaper($headings)).$enclosure.$newline;
foreach ($data as $row)
{
$output .= $enclosure.implode($enclosure.$delimiter.$enclosure, $escaper((array) $row)).$enclosure.$newline;
}
return rtrim($output, $newline);
}
/**
* Serialize
*
* @param mixed $data
* @return string
*/
public function to_serialized($data = null)
{
if ($data == null)
{
$data = $this->_data;
}
return serialize($data);
}
/**
* To JSON conversion
*
* @param mixed $data
* @param bool wether to make the json pretty
* @return string
*/
public function to_json($data = null, $pretty = false)
{
if ($data == null)
{
$data = $this->_data;
}
// To allow exporting ArrayAccess objects like Orm\Model instances they need to be
// converted to an array first
$data = (is_array($data) or is_object($data)) ? $this->to_array($data) : $data;
return $pretty ? static::pretty_json($data) : json_encode($data);
}
/**
* Return as a string representing the PHP structure
*
* @param mixed $data
* @return string
*/
public function to_php($data = null)
{
if ($data == null)
{
$data = $this->_data;
}
return var_export($data, true);
}
/**
* Convert to YAML
*
* @param mixed $data
* @return string
*/
public function to_yaml($data = null)
{
if ($data == null)
{
$data = $this->_data;
}
$data = (is_array($data) or is_object($data)) ? $this->to_array($data) : $data;
return \Spyc::YAMLDump($data);
}
/**
* To XML conversion
*
* @param mixed $data
* @param null $structure
* @param null|string $basenode
* @return string
*/
public function to_xml($data = null, $structure = null, $basenode = 'xml')
{
if ($data == null)
{
$data = $this->_data;
}
// turn off compatibility mode as simple xml throws a wobbly if you don't.
if (ini_get('zend.ze1_compatibility_mode') == 1)
{
ini_set('zend.ze1_compatibility_mode', 0);
}
if ($structure == null)
{
$structure = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$basenode />");
}
// Force it to be something useful
if ( ! is_array($data) and ! is_object($data))
{
$data = (array) $data;
}
foreach ($data as $key => $value)
{
// no numeric keys in our xml please!
if (is_numeric($key))
{
// make string key...
$key = (\Str::singular($basenode) != $basenode) ? \Str::singular($basenode) : 'item';
}
// replace anything not alpha numeric
$key = preg_replace('/[^a-z_\-0-9]/i', '', $key);
// if there is another array found recrusively call this function
if (is_array($value) or is_object($value))
{
$node = $structure->addChild($key);
// recursive call if value is not empty
if( ! empty($value))
{
$this->to_xml($value, $node, $key);
}
}
else
{
// add single node.
$value = htmlspecialchars(html_entity_decode($value, ENT_QUOTES, 'UTF-8'), ENT_QUOTES, "UTF-8");
$structure->addChild($key, $value);
}
}
// pass back as string. or simple xml object if you want!
return $structure->asXML();
}
/**
* Import JSON data
*
* @param string $string
* @return mixed
*/
private function _from_json($string)
{
return json_decode(trim($string));
}
/**
* Import Serialized data
*
* @param string $string
* @return mixed
*/
private function _from_serialize($string)
{
return unserialize(trim($string));
}
/**
* Import XML data
*
* @param string $string
* @return array
*/
protected function _from_xml($string)
{
$_arr = is_string($string) ? simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA) : $string;
$arr = array();
// Convert all objects SimpleXMLElement to array recursively
foreach ((array)$_arr as $key => $val)
{
$arr[$key] = (is_array($val) or is_object($val)) ? $this->_from_xml($val) : $val;
}
return $arr;
}
/**
* Import CSV data
*
* @param string $string
* @return array
*/
protected function _from_csv($string)
{
$data = array();
// let's get the config file
$config = Config::get('formatter::format.csv');
$rows = preg_split('/(?<='.preg_quote(Config::get('format.csv.enclosure', '"')).')'.Config::get('format.csv.regex_newline', '\n').'/', trim($string));
// csv config
$delimiter = array_get($config, 'delimiter', ",");
$enclosure = array_get($config, 'enclosure', '"');
$escape = array_get($config, 'escape', "\\");
// Get the headings
$headings = str_replace($escape.$enclosure, $enclosure, str_getcsv(array_shift($rows), $delimiter, $enclosure, $escape));
foreach ($rows as $row)
{
$data_fields = str_replace($escape.$enclosure, $enclosure, str_getcsv($row, $delimiter, $enclosure, $escape));
if (count($data_fields) == count($headings))
{
$data[] = array_combine($headings, $data_fields);
}
}
return $data;
}
/**
* Checks if the given array is a multidimensional array.
*
* @param array $arr the array to check
* @param array $all_keys if true, check that all elements are arrays
* @return bool true if its a multidimensional array, false if not
*/
protected static function is_multi($arr, $all_keys = false)
{
$values = array_filter($arr, 'is_array');
return $all_keys ? count($arr) === count($values) : count($values) > 0;
}
/**
* Checks if the given array is an assoc array.
*
* @param array $arr the array to check
* @return bool true if its an assoc array, false if not
*/
public static function is_assoc($arr)
{
if ( ! is_array($arr))
{
throw new FormatterException('The parameter must be an array.');
}
$counter = 0;
foreach ($arr as $key => $unused)
{
if ( ! is_int($key) or $key !== $counter++)
{
return true;
}
}
return false;
}
}