Overview

Packages

  • CodeIgniter
    • Libraries
    • Rest
  • None

Classes

  • Format
  • Rest_server
  • Welcome
  • Overview
  • Package
  • Class

Class Format

Format class Help convert between various formats such as XML, JSON, CSV, etc.

License: http://www.dbad-license.org/
Author: Phil Sturgeon, Chris Kacerguis, @softwarespot
Located at libraries/Format.php
Methods summary
public
# __construct( null $data = NULL, null $from_type = NULL )

DO NOT CALL THIS DIRECTLY, USE factory()

DO NOT CALL THIS DIRECTLY, USE factory()

Parameters

$data
$from_type

Throws

Exception
public object
# factory( mixed $data, string $from_type = NULL )

Create an instance of the format class e.g: echo $this->format->factory(['foo' => 'bar'])->to_csv();

Create an instance of the format class e.g: echo $this->format->factory(['foo' => 'bar'])->to_csv();

Parameters

$data
Data to convert/parse
$from_type
Type to convert from e.g. json, csv, html

Returns

object
Instance of the format class
public array
# to_array( mixed|null $data = NULL )

Format data as an array

Format data as an array

Parameters

$data

Optional data to pass, so as to override the data passed to the constructor

Returns

array
Data parsed as an array; otherwise, an empty array
public mixed
# to_xml( mixed|null $data = NULL, null $structure = NULL, string $basenode = 'xml' )

Format data as XML

Format data as XML

Parameters

$data

Optional data to pass, so as to override the data passed to the constructor

$structure
$basenode

Returns

mixed
public mixed
# to_html( mixed|null $data = NULL )

Format data as HTML

Format data as HTML

Parameters

$data

Optional data to pass, so as to override the data passed to the constructor

Returns

mixed
public string
# to_csv( mixed|null $data = NULL, string $delimiter = ',', string $enclosure = '"' )

Parameters

$data

Optional data to pass, so as to override the data passed to the constructor

$delimiter

The optional delimiter parameter sets the field delimiter (one character only). NULL will use the default value (,)

$enclosure

The optional enclosure parameter sets the field enclosure (one character only). NULL will use the default value (")

Returns

string
A csv string

Link

http://www.metashock.de/2014/02/create-csv-file-in-memory-php/
public string
# to_json( mixed|null $data = NULL )

Encode data as json

Encode data as json

Parameters

$data

Optional data to pass, so as to override the data passed to the constructor

Returns

string
Json representation of a value
public string
# to_serialized( mixed|null $data = NULL )

Encode data as a serialized array

Encode data as a serialized array

Parameters

$data

Optional data to pass, so as to override the data passed to the constructor

Returns

string
Serialized data
public mixed
# to_php( mixed|null $data = NULL )

Format data using a PHP structure

Format data using a PHP structure

Parameters

$data

Optional data to pass, so as to override the data passed to the constructor

Returns

mixed
String representation of a variable
protected SimpleXMLElement
# _from_xml( $data )

Parameters

$data
string

Returns

SimpleXMLElement
XML element object; otherwise, empty array
protected array
# _from_csv( string $data, string $delimiter = ',', string $enclosure = '"' )

Parameters

$data
CSV string
$delimiter

The optional delimiter parameter sets the field delimiter (one character only). NULL will use the default value (,)

$enclosure

The optional enclosure parameter sets the field enclosure (one character only). NULL will use the default value (")

Returns

array

A multi-dimensional array with the outer array being the number of rows and the inner arrays the individual fields

protected mixed
# _from_json( $data )

Parameters

$data
json string

Returns

mixed
Decoded json string with leading and trailing whitespace removed
protected mixed
# _from_serialize( string $data )

Parameters

$data
to unserialized

Returns

mixed
Unserialized data
protected string
# _from_php( $data )

Parameters

$data
to trim leading and trailing whitespace

Returns

string
Data with leading and trailing whitespace removed
Constants summary
string ARRAY_FORMAT

Array output format

Array output format

# 'array'
string CSV_FORMAT

Comma Separated Value (CSV) output format

Comma Separated Value (CSV) output format

# 'csv'
string JSON_FORMAT

Json output format

Json output format

# 'json'
string HTML_FORMAT

HTML output format

HTML output format

# 'html'
string PHP_FORMAT

PHP output format

PHP output format

# 'php'
string SERIALIZED_FORMAT

Serialized output format

Serialized output format

# 'serialized'
string XML_FORMAT

XML output format

XML output format

# 'xml'
string DEFAULT_FORMAT

Default format of this class

Default format of this class

# Format::JSON_FORMAT
Properties summary
protected mixed $_data

Data to parse

Data to parse

# []
protected string $_from_type

Type to convert from

Type to convert from

# NULL
API documentation generated by ApiGen