-
Notifications
You must be signed in to change notification settings - Fork 88
Parse
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 to a format
- Type switchers
Converts data from JSON
Parse::fromJSON('{"foo":"bar","bis":"ter"}') // Returns ['foo' => 'bar', 'bis' => 'ter']
Converts data from CSV
Parse::fromCSV('foo;bar;bis;ter') // Returns ['foo', 'bar', 'bis', 'ter']
Converts data from XML
Parse::fromXML('<article><name>Foo</name><content>Bar</content></article>') // Returns ['article' => ['name' => 'Foo', 'content' => 'Bar']]
Converts data to JSON
Parse::toJSON(array(1, 2, 3)) // Returns [1, 2, 3]
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"
Converts data to an array
Parse::toArray($article) // Returns array('title' => 'My article', 'content' => ...)
Parse::toArray(15) // Returns array(15)
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
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
Converts data to an object
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]"