Format クラス

Format クラスは、XML、JSON、CSV等の様々なフォーマット間の変換ができます。

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

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

The forge method returns a new format object.

静的 はい
パラメータ
パラメータ 規定値 説明
$data
null
Data to be converted.
$from_type
null
Format of the data provided.
返り値 Fuel\Core\Format Object
$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.

静的 No
パラメータ
パラメータ 規定値 説明
$data
null
Data to be converted.
返り値 array
$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')

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.

静的 No
パラメータ
パラメータ 規定値 説明
$data
null
Data to be converted.
$structure
null
Object of class SimpleXMLElement.
$basenode "xml" Basenode of XML markup.
返り値 string
$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.

静的 No
パラメータ
パラメータ 規定値 説明
$data
null
Data to be converted.
$separator
','
CSV field separator. Defaults to a comma.
返り値 string
$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.

静的 No
返り値 string
$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.

静的 No
返り値 string
$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.

静的 No
返り値 string
$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',
	),
)