PHP Class Format

Show file Open project: chriskacerguis/codeigniter-restserver Class Usage Examples

Protected Properties

Property Type Description
$_data mixed Data to parse
$_from_type string Type to convert from

Public Methods

Method Description
__construct ( null $data = NULL, null $from_type = NULL ) DO NOT CALL THIS DIRECTLY, USE factory()
factory ( mixed $data, string $from_type = NULL ) : object Create an instance of the format class e.g: echo $this->format->factory(['foo' => 'bar'])->to_csv();
to_array ( mixed | null $data = NULL ) : array Format data as an array
to_csv ( mixed | null $data = NULL, string $delimiter = ',', string $enclosure = '"' ) : string
to_html ( mixed | null $data = NULL ) : mixed Format data as HTML
to_json ( mixed | null $data = NULL ) : string Encode data as json
to_php ( mixed | null $data = NULL ) : mixed Format data using a PHP structure
to_serialized ( mixed | null $data = NULL ) : string Encode data as a serialized array
to_xml ( mixed | null $data = NULL, null $structure = NULL, string $basenode = 'xml' ) : mixed Format data as XML

Protected Methods

Method Description
_from_csv ( string $data, string $delimiter = ',', string $enclosure = '"' ) : array
_from_json ( string $data ) : mixed
_from_php ( string $data ) : string
_from_serialize ( string $data ) : mixed
_from_xml ( string $data ) : array

Method Details

__construct() public method

DO NOT CALL THIS DIRECTLY, USE factory()
public __construct ( null $data = NULL, null $from_type = NULL )
$data null
$from_type null

_from_csv() protected method

protected _from_csv ( string $data, string $delimiter = ',', string $enclosure = '"' ) : array
$data string CSV string
$delimiter string The optional delimiter parameter sets the field delimiter (one character only). NULL will use the default value (,)
$enclosure string The optional enclosure parameter sets the field enclosure (one character only). NULL will use the default value (")
return array A multi-dimensional array with the outer array being the number of rows and the inner arrays the individual fields

_from_json() protected method

protected _from_json ( string $data ) : mixed
$data string Encoded json string
return mixed Decoded json string with leading and trailing whitespace removed

_from_php() protected method

protected _from_php ( string $data ) : string
$data string Data to trim leading and trailing whitespace
return string Data with leading and trailing whitespace removed

_from_serialize() protected method

protected _from_serialize ( string $data ) : mixed
$data string Data to unserialize
return mixed Unserialized data

_from_xml() protected method

protected _from_xml ( string $data ) : array
$data string XML string
return array XML element object; otherwise, empty array

factory() public method

Create an instance of the format class e.g: echo $this->format->factory(['foo' => 'bar'])->to_csv();
public factory ( mixed $data, string $from_type = NULL ) : object
$data mixed Data to convert/parse
$from_type string Type to convert from e.g. json, csv, html
return object Instance of the format class

to_array() public method

Format data as an array
public to_array ( mixed | null $data = NULL ) : array
$data mixed | null Optional data to pass, so as to override the data passed to the constructor
return array Data parsed as an array; otherwise, an empty array

to_csv() public method

public to_csv ( mixed | null $data = NULL, string $delimiter = ',', string $enclosure = '"' ) : string
$data mixed | null Optional data to pass, so as to override the data passed to the constructor
$delimiter string The optional delimiter parameter sets the field delimiter (one character only). NULL will use the default value (,)
$enclosure string The optional enclosure parameter sets the field enclosure (one character only). NULL will use the default value (")
return string A csv string

to_html() public method

Format data as HTML
public to_html ( mixed | null $data = NULL ) : mixed
$data mixed | null Optional data to pass, so as to override the data passed to the constructor
return mixed

to_json() public method

Encode data as json
public to_json ( mixed | null $data = NULL ) : string
$data mixed | null Optional data to pass, so as to override the data passed to the constructor
return string Json representation of a value

to_php() public method

Format data using a PHP structure
public to_php ( mixed | null $data = NULL ) : mixed
$data mixed | null Optional data to pass, so as to override the data passed to the constructor
return mixed String representation of a variable

to_serialized() public method

Encode data as a serialized array
public to_serialized ( mixed | null $data = NULL ) : string
$data mixed | null Optional data to pass, so as to override the data passed to the constructor
return string Serialized data

to_xml() public method

Format data as XML
public to_xml ( mixed | null $data = NULL, null $structure = NULL, string $basenode = 'xml' ) : mixed
$data mixed | null Optional data to pass, so as to override the data passed to the constructor
$structure null
$basenode string
return mixed

Property Details

$_data protected property

Data to parse
protected mixed $_data
return mixed

$_from_type protected property

Type to convert from
protected string $_from_type
return string