CSVasXMLHydrator.php 774 Bytes
Newer Older
Olivier Lequeux's avatar
Olivier Lequeux committed
1 2 3
<?php
namespace Lavoisier\Hydrators;

4
use \Lavoisier\Hydrators\HydratorInterface;
Olivier Lequeux's avatar
Olivier Lequeux committed
5 6 7 8 9

/**
 * @author Olivier LEQUEUX
 * hydrate parsing lavoisier XML format after a CSV file conversion
 */
10
class CSVasXMLHydrator implements HydratorInterface
Olivier Lequeux's avatar
Olivier Lequeux committed
11 12 13 14 15 16 17 18
{

    function hydrate($str)
    {
        $rows = simplexml_load_string($str);

        $result = new \ArrayObject();
        foreach ($rows as $row) {
Olivier Lequeux's avatar
Olivier Lequeux committed
19
            foreach ($row as  $block) {
Olivier Lequeux's avatar
Olivier Lequeux committed
20
                $tmp_col = new \ArrayObject();
Olivier Lequeux's avatar
Olivier Lequeux committed
21
                foreach ($block as $column) {
Olivier Lequeux's avatar
Olivier Lequeux committed
22 23 24 25 26 27 28 29 30 31 32
                    $col_attr = $column->attributes();
                    $tmp_col[strval($col_attr['label'])] = strval($column);
                }
                $result->append($tmp_col);
            }

        }

        return $result;
    }
}