Format Class

The Format class helps you convert between various formats such as XML, JSON, CSV, etc.

Configuration

The Format class is configured through the fuel/core/config/format.php 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.

The following configuration settings can be defined:

CSV

Param Type Default Description
delimiter string
','
The field delimiter
enclosure string
'"'
The enclosure character
newline string
'\n'
The newline character used by to_csv() method
regex_newline string
'\n'
The newline character used by regex to convert input data
escape string
'\\'
The escape character

XML

Param Type Default Description
basenode string
'xml'
XML basenode name
use_cdata boolean
false
Whether to use CDATA in nodes

forge($data = null, $from_type = null)

The forge method returns a new format object.

Static Yes
Parameters
Param Default Description
$data
null
Data to be converted.
$from_type
null
Format of the data provided.
Returns Fuel\Core\Format Object
Example
$array = array('foo' => 'bar');
print_r(Format::forge($array));

// Returns
Fuel\Core\Format Object
(
	[_data:protected] => Array
	(
		[foo] => bar
	)
)
							

to_array($data = null)

The to_array method returns the given data as an array. Do not call this directly, use the forge method as described in the example.

Static No
Parameters
Param Default Description
$data
null
Data to be converted.
Returns array
Example
$json_string = '{"foo":"bar","baz":"qux"}';
print_r(Format::forge($json_string, 'json')->to_array());

// Returns
Array
(
	[foo] => bar
	[baz] => qux
)
							

to_xml($data = null, $structure = null, $basenode = 'xml', $use_cdata = false)

The to_xml method returns the given data as an XML string. Do not call this directly, use the forge method as described in the example.

Static No
Parameters
Param Default Description
$data
null
Data to be converted.
$structure
null
Object of class SimpleXMLElement.
$basenode "xml" Basenode of XML markup.
$use_cdata false Whether to use CDATA in nodes.
Returns string
Example
$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>
							

to_csv($data = null, $separator = ',')

The to_csv method returns the given data as a CSV string. Do not call this directly, use the forge method as described in the example.

Static No
Parameters
Param Default Description
$data
null
Data to be converted.
$separator
','
CSV field separator. Defaults to a comma.
Returns string
Example
$json_string = '{"foo":"bar","baz":"qux"}';
print_r(Format::forge($json_string, 'json')->to_csv());

// Returns
// foo,baz
// "bar","qux"
							

to_json()

The to_json method returns the given data as a JSON string. Do not call this directly, use the forge method as described in the example.

Static No
Returns string
Example
$array = array('foo' => 'bar', 'baz' => 'qux');
print_r(Format::forge($array)->to_json());

// Returns
// {"foo":"bar","baz":"qux"}
							

to_serialized()

The to_serialized method returns the given data as a serialized string. Do not call this directly, use the forge method as described in the example.

Static No
Returns string
Example
$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";}
							

to_php()

The to_php 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.

Static No
Returns string
Example
$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',
	),
)