Fuel Documentation

Format Class

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

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

The factory 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::factory( $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 factory 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::factory( $json_string, 'json' )->to_array() );

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

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

The to_xml method returns the given data as an XML string. Do not call this directly, use the factory 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.
Returns string
Example
$array = array('foo' => 'bar', 'baz' => 'qux');
print_r( Format::factory( $array )->to_xml() );

// Returns
// <?xml version="1.0" encoding="utf-8"?>
// <xml>
// 	<foo>bar</foo>
// 	<baz>qux</baz>
// </xml>
						

to_csv()

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

Static No
Returns string
Example
$json_string = '{"foo":"bar","baz":"qux"}';
print_r( Format::factory( $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 factory method as described in the example.

Static No
Returns string
Example
$array = array('foo' => 'bar', 'baz' => 'qux');
print_r( Format::factory( $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 factory method as described in the example.

Static No
Returns string
Example
$array = array('foo' => 'bar', 'baz' => 'qux');
print_r( Format::factory( $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"));
echo Format::factory( $array )->to_php();

// Returns
"array (
  0 => 1,
  1 => 2,
  2 => 
  array (
    0 => 'a',
    1 => 'b',
    2 => 'c',
  ),
)"