Skip to content
This repository has been archived by the owner on Jul 6, 2019. It is now read-only.
Anahkiasen edited this page Jan 25, 2013 · 3 revisions

The Parse class is a general helper from converting from and to various formats. You can either call it directly on content like this Parse::toJSON($array) or chain it from existing Underscore objects, like this : Arrays::from($data)->toJSON()

Available methods


Parse from a format

Parse::fromJSON(data)

Converts data from JSON

Parse::fromJSON('{"foo":"bar","bis":"ter"}') // Returns ['foo' => 'bar', 'bis' => 'ter']

Parse::fromCSV(data)

Converts data from CSV

Parse::fromCSV('foo;bar;bis;ter') // Returns ['foo', 'bar', 'bis', 'ter']

Parse::fromXML(data)

Converts data from XML

Parse::fromXML('<article><name>Foo</name><content>Bar</content></article>') // Returns ['article' => ['name' => 'Foo', 'content' => 'Bar']]

Parse to a format

Parse::toJSON(data)

Converts data to JSON

Parse::toJSON(array(1, 2, 3)) // Returns [1, 2, 3]

Parse::toCSV(data, [delimiter = ;], [exportHeaders = true])

Converts data to CSV. You can specify which delimiter is to be used for the rows, and whether an headers row should be created

$array = array(array('name' => 'foo', 'content' => 'bar'), array('name' => 'bar', 'content' => 'foo'))
Parse::toCSV($array, ';', true) // Returns "name";"content"\n"foo";"bar"\n"bar";"foo"

Type switchers

Parse::toArray(data)

Converts data to an array

Parse::toArray($article) // Returns array('title' => 'My article', 'content' => ...)
Parse::toArray(15) // Returns array(15)

Parse::toBoolean(data)

Converts data to a boolean

Parse::toBoolean(15) // Returns true
Parse::toBoolean('foo') // Returns true
Parse::toBoolean(array()) // Returns false
Parse::toBoolean('') // Returns false
Parse::toBoolean(0) // Returns false

Parse::toInteger(data)

Converts data to a boolean. Arrays will return their size. String will return their length, excepted if the string contains a number.

Parse::toInteger('15') // Returns 15
Parse::toInteger('foo') // Returns 3
Parse::toInteger(array()) // Returns 0
Parse::toInteger(array(1, 2, 3)) // Returns 3
Parse::toInteger('') // Returns 0

Parse::toObject(data)

Converts data to an object

Parse::toString(data)

Converts data to a string. Arrays and objects will be converted to JSON.

Parse::toString(15) // Returns '15'
Parse::toString(array(1, 2, 3)) // Returns "[1, 2, 3]"