file-parser.txt 1.62 KB
Newer Older
Jonathan.Wage's avatar
Jonathan.Wage committed
1 2 3 4 5 6 7 8 9 10
The parser is built to allow dumping and loading from many different formats. Currently xml and yml are the only drivers but later other file formats such as csv may be added. You can specify the data to load/dump in with the $type argument on dump() and load()

++ Dumping

Dumping array to yml variable

<code type="php">
$array = array('test' => array('key' => 'value'), 'test2' => 'test');

// Dump the array to yml and return, set to $yml(does not write to file). Replace null with a path to a yml file if you wish to write to disk
Jonathan.Wage's avatar
Jonathan.Wage committed
11
$yml = Doctrine_Parser::dump($array, 'yml');
Jonathan.Wage's avatar
Jonathan.Wage committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
</code>

$yml would contain the following
<code type="yaml">
---
test: 
  key: value
test2: test
</code>


Dumping array to yml file

<code type="php">
$array = array('test' => array('key' => 'value'), 'test2' => 'test');

// Dump the above array to test.yml using yml parser
Jonathan.Wage's avatar
Jonathan.Wage committed
29
Doctrine_Parser::dump($array, 'yml', 'test.yml');
Jonathan.Wage's avatar
Jonathan.Wage committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
</code>

A file named test.yml would be created and would contain the following
<code type="yaml">
---
test: 
  key: value
test2: test
</code>

++ Loading

Loading and parsing data from a yml file to a php array

<code type="php">
$array = array('test' => array('key' => 'value'), 'test2' => 'test');

// We dump the above array to test.yml using the yml parser dumper
Jonathan.Wage's avatar
Jonathan.Wage committed
48
Doctrine_Parser::dump($array, 'yml', 'test.yml');
Jonathan.Wage's avatar
Jonathan.Wage committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

// Now we reload that dumped yaml file back to the original array format using the yml parser loder
$array = Doctrine_Parser::load('test.yml', 'yml');

print_r($array);
</code>

The print_r() would output the following

<code>
Array
(
    [test] => Array
        (
            [key] => value
        )

    [test2] => test
)
</code>