-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathformat.html
502 lines (476 loc) · 14.2 KB
/
format.html
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="./../assets/css/combined.css?20170912">
<link rel="shortcut icon" href="./../favicon.ico" />
<script src="https://www.google.com/jsapi" type="text/javascript"></script>
<script type="text/javascript">
var path = './../';
var class_prefix = "Form::";
</script>
<script src="./../assets/js/combined.js?20170912"></script>
<title>Format - Classes - FuelPHP Documentation</title>
</head>
<body>
<div id="container">
<header id="header">
<div class="table">
<div id="cse">
<gcse:searchbox-only newWindow="true"></gcse:searchbox-only>
</div>
<h1>
<a href="https://fuelphp.com"><img height="37px" width="147px" src="./../assets/img/fuel.png" /></a>
<strong>Documentation</strong>
</h1>
</div>
<nav>
<div class="clear"></div>
</nav>
<a href="#" id="toc_handle">table of contents</a>
<div class="clear"></div>
</header>
<div id="main">
<h2>Format Class</h2>
<p>The Format class helps you convert between various formats such as XML, JSON, CSV, etc.</p>
<article>
<h3 id="configuration">Configuration</h3>
<p>The Format class is configured through the <kbd>fuel/core/config/format.php</kbd> configuration file. It is already populated with a default configuration group. You can override this configuration by copying this config file to your application config directory, and modify that file as needed.</p>
<p>The following configuration settings can be defined:</p>
<h4>CSV</h4>
<table class="config">
<tbody>
<tr class="header">
<th>Param</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
<tr>
<th>delimiter</th>
<td>string</td>
<td><pre class="php"><code>','</code></pre></td>
<td>The field delimiter</td>
</tr>
<tr>
<th>enclosure</th>
<td>string</td>
<td><pre class="php"><code>'"'</code></pre></td>
<td>The enclosure character</td>
</tr>
<tr>
<th>newline</th>
<td>string</td>
<td><pre class="php"><code>'\n'</code></pre></td>
<td>The newline character used by to_csv() method</td>
</tr>
<tr>
<th>escape</th>
<td>string</td>
<td><pre class="php"><code>'\\'</code></pre></td>
<td>The escape character</td>
</tr>
</tbody>
</table>
<p>
By default, these configuration settings are used both for import and for export of CSV data. In case you need to
configure different settings for either import or export, you can an array key for it, and move the configuration
in there. The default configuration file already has this setup for you.
</p>
<table class="config">
<tbody>
<tr>
<th>regex_newline</th>
<td>string</td>
<td><pre class="php"><code>'\n'</code></pre></td>
<td>The newline character used by regex to convert input data</td>
</tr>
<tr>
<th>enclose_numbers</th>
<td>bool</td>
<td><pre class="php"><code>true</code></pre></td>
<td>If false, numbers will not be enclosed. If true, numbers will be enclosed too</td>
</tr>
</tbody>
</table>
<h4>XML</h4>
<table class="config">
<tbody>
<tr class="header">
<th>Param</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
<tr>
<th>basenode</th>
<td>string</td>
<td><pre class="php"><code>'xml'</code></pre></td>
<td>XML basenode name</td>
</tr>
<tr>
<th>use_cdata</th>
<td>boolean</td>
<td><pre class="php"><code>false</code></pre></td>
<td>Whether to use CDATA in nodes</td>
</tr>
<tr>
<th>bool_representation</th>
<td>mixed</td>
<td><pre class="php"><code>null</code></pre></td>
<td>How booleans must be represented (0/1 vs false/true)</td>
</tr>
</tbody>
</table>
<h4>JSON</h4>
<table class="config">
<tbody>
<tr class="header">
<th>Param</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
<tr>
<th>options</th>
<td>integer</td>
<td><pre class="php"><code>JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP</code></pre></td>
<td>json_encode() options</td>
</tr>
</tbody>
</table>
</article>
<p class="note">
Be aware that the result of some combinations are unpredicable. For example, if you have CSV data without enclosure characters, no headings, and embedded newline characters in the first row, the correct number of fields in the file can not be calculated.
</p>
<article>
<h4 class="method" id="method_forge">forge($data = null, $from_type = null, $params = null)</h4>
<p>The <strong>forge</strong> method returns a new format object. <strong>$from_type</strong> supports <strong>'xml'</strong>, <strong>'yaml'</strong>, <strong>'csv'</strong>, <strong>'json'</strong>, <strong>'serialize'</strong> and <strong>'xml:ns'</strong>. 'xml:ns' allows you to parse fully namespaced-xml. When $from_type is not specified, <strong>$data</strong> will be delivered as it is.</p>
<table class="method">
<tbody>
<tr>
<th class="legend">Static</th>
<td>Yes</td>
</tr>
<tr>
<th>Parameters</th>
<td>
<table class="parameters">
<tr>
<th>Param</th>
<th>Default</th>
<th class="description">Description</th>
</tr>
<tr>
<th><kbd>$data</kbd></th>
<td><pre class="php"><code>null</code></pre></td>
<td>Data to be converted.</td>
</tr>
<tr>
<th><kbd>$from_type</kbd></th>
<td><pre class="php"><code>null</code></pre></td>
<td>Format of the data provided.</td>
</tr>
<tr>
<th><kbd>$param</kbd></th>
<td><pre class="php"><code>null</code></pre></td>
<td>Optional parameter to be passed on to the converter.</td>
</tr>
</table>
</td>
</tr>
<tr>
<th>Returns</th>
<td>Fuel\Core\Format Object</td>
</tr>
<tr>
<th>Example</th>
<td>
<pre class="php"><code>$array = array('foo' => 'bar');
print_r(Format::forge($array));
// Returns
Fuel\Core\Format Object
(
[_data:protected] => Array
(
[foo] => bar
)
)</code>
</pre>
</td>
</tr>
</tbody>
</table>
<p class="note">
When converting from CSV format, you can pass <kbd>false</kbd> as <kbd>$param</kbd> to indicate the CSV source does not contain any headers. If so, the end result will be an indexed array.
</p>
</article>
<article>
<h4 class="method" id="method_to_array">to_array($data = null)</h4>
<p>The <strong>to_array</strong> method returns the given data as an array. Do not call this directly, use the <strong>forge</strong> method as described in the example.</p>
<table class="method">
<tbody>
<tr>
<th class="legend">Static</th>
<td>No</td>
</tr>
<tr>
<th>Parameters</th>
<td>
<table class="parameters">
<tr>
<th>Param</th>
<th>Default</th>
<th class="description">Description</th>
</tr>
<tr>
<th><kbd>$data</kbd></th>
<td><pre class="php"><code>null</code></pre></td>
<td>Data to be converted.</td>
</tr>
</table>
</td>
</tr>
<tr>
<th>Returns</th>
<td>array</td>
</tr>
<tr>
<th>Example</th>
<td>
<pre class="php"><code>$json_string = '{"foo":"bar","baz":"qux"}';
print_r(Format::forge($json_string, 'json')->to_array());
// Returns
Array
(
[foo] => bar
[baz] => qux
)</code>
</pre>
</td>
</tr>
</tbody>
</table>
</article>
<article>
<h4 class="method" id="method_to_xml">to_xml($data = null, $structure = null, $basenode = 'xml', $use_cdata = false)</h4>
<p>The <strong>to_xml</strong> method returns the given data as an XML string. Do not call this directly, use the <strong>forge</strong> method as described in the example.</p>
<table class="method">
<tbody>
<tr>
<th class="legend">Static</th>
<td>No</td>
</tr>
<tr>
<th>Parameters</th>
<td>
<table class="parameters">
<tr>
<th>Param</th>
<th>Default</th>
<th class="description">Description</th>
</tr>
<tr>
<th><kbd>$data</kbd></th>
<td><pre class="php"><code>null</code></pre></td>
<td>Data to be converted.</td>
</tr>
<tr>
<th><kbd>$structure</kbd></th>
<td><pre class="php"><code>null</code></pre></td>
<td>Object of class SimpleXMLElement.</td>
</tr>
<tr>
<th><kbd>$basenode</kbd></th>
<td>"xml"</td>
<td>Basenode of XML markup.</td>
</tr>
<tr>
<th><kbd>$use_cdata</kbd></th>
<td>false</td>
<td>Whether to use CDATA in nodes.</td>
</tr>
</table>
</td>
</tr>
<tr>
<th>Returns</th>
<td>string</td>
</tr>
<tr>
<th>Example</th>
<td>
<pre class="php"><code>$array = array('foo' => 'bar', 'baz' => 'qux');
print_r(Format::forge($array)->to_xml());
// Returns
// <?xml version="1.0" encoding="utf-8"?>
// <xml>
// <foo>bar</foo>
// <baz>qux</baz>
// </xml></code>
</pre>
</td>
</tr>
</tbody>
</table>
</article>
<article>
<h4 class="method" id="method_to_csv">to_csv($data = null, $delimiter = null, $enclose_numbers = null, array $headings = array())</h4>
<p>The <strong>to_csv</strong> method returns the given data as a CSV string. Do not call this directly, use the <strong>forge</strong> method as described in the example.</p>
<table class="method">
<tbody>
<tr>
<th class="legend">Static</th>
<td>No</td>
</tr>
<tr>
<th>Parameters</th>
<td>
<table class="parameters">
<tr>
<th>Param</th>
<th>Default</th>
<th class="description">Description</th>
</tr>
<tr>
<th><kbd>$data</kbd></th>
<td><pre class="php"><code>null</code></pre></td>
<td>Data to be converted. If not given, the input of forge() is used.</td>
</tr>
<tr>
<th><kbd>$delimiter</kbd></th>
<td><pre class="php"><code>null</code></pre></td>
<td>CSV field separator. If not given, the configured default is used.</td>
</tr>
<tr>
<th><kbd>$enclose_numbers</kbd></th>
<td><pre class="php"><code>null</code></pre></td>
<td>If false, number will not be enclosed. If true, numbers will be enclosed too. If not given, the configured default is used.</td>
</tr>
<tr>
<th><kbd>$headings</kbd></th>
<td><pre class="php"><code>array()</code></pre></td>
<td>Array of CSV fieldnames. If not given, the fieldnames will be based on what is defined in the first row of the data array.</td>
</tr>
</table>
</td>
</tr>
<tr>
<th>Returns</th>
<td>string</td>
</tr>
<tr>
<th>Example</th>
<td>
<pre class="php"><code>$json_string = '{"foo":"bar","baz":"qux","nr":1}';
print_r(Format::forge($json_string, 'json')->to_csv(null, null, false));
// Returns
// "foo","baz","nr"
// "bar","qux",1
$json_string = '{"foo":"bar","baz":"qux","nr":1}';
print_r(Format::forge($json_string, 'json')->to_csv(null, null, false, array('this', 'that', 'num')));
// Returns
// "this","that","num"
// "bar","qux",1</code>
</pre>
</td>
</tr>
</tbody>
</table>
</article>
<article>
<h4 class="method" id="method_to_json">to_json()</h4>
<p>The <strong>to_json</strong> method returns the given data as a JSON string. Do not call this directly, use the <strong>forge</strong> method as described in the example.</p>
<table class="method">
<tbody>
<tr>
<th class="legend">Static</th>
<td>No</td>
</tr>
<tr>
<th>Returns</th>
<td>string</td>
</tr>
<tr>
<th>Example</th>
<td>
<pre class="php"><code>$array = array('foo' => 'bar', 'baz' => 'qux');
print_r(Format::forge($array)->to_json());
// Returns
// {"foo":"bar","baz":"qux"}</code>
</pre>
</td>
</tr>
</tbody>
</table>
</article>
<article>
<h4 class="method" id="method_to_serialized">to_serialized()</h4>
<p>The <strong>to_serialized</strong> method returns the given data as a serialized string. Do not call this directly, use the <strong>forge</strong> method as described in the example.</p>
<table class="method">
<tbody>
<tr>
<th class="legend">Static</th>
<td>No</td>
</tr>
<tr>
<th>Returns</th>
<td>string</td>
</tr>
<tr>
<th>Example</th>
<td>
<pre class="php"><code>$array = array('foo' => 'bar', 'baz' => 'qux');
print_r(Format::forge($array)->to_serialized());
// Returns
// a:2:{s:3:"foo";s:3:"bar";s:3:"baz";s:3:"qux";}</code>
</pre>
</td>
</tr>
</tbody>
</table>
</article>
<article>
<h4 class="method" id="method_to_php">to_php()</h4>
<p>The <strong>to_php</strong> method returns the given data as a PHP representation of the data in a string. You could pass this into eval() or use it for other crazy things.</p>
<table class="method">
<tbody>
<tr>
<th class="legend">Static</th>
<td>No</td>
</tr>
<tr>
<th>Returns</th>
<td>string</td>
</tr>
<tr>
<th>Example</th>
<td>
<pre class="php"><code>$array = array(1, 2, array('a', 'b', 'c'));
print_r(Format::forge($array)->to_php());
// Returns
Array
(
0 => 1,
1 => 2,
2 => array(
0 => 'a',
1 => 'b',
2 => 'c',
),
)</code></pre>
</td>
</tr>
</tbody>
</table>
</article>
</div>
<footer>
<p>
© FuelPHP Development Team 2010-2025 - <a href="https://fuelphp.com">FuelPHP</a> is released under the MIT license.
</p>
</footer>
</div>
</body>
</html>