From 7e2b6289ed531ec3798a45c4ba97e1a3d7988218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez?= Date: Tue, 5 Nov 2013 12:49:49 +0000 Subject: [PATCH] Added xml data loader --- DataFixtures/Loader/XmlDataLoader.php | 61 +++++++++++++++++++++++++++ Resources/config/propel.xml | 7 +++ 2 files changed, 68 insertions(+) create mode 100644 DataFixtures/Loader/XmlDataLoader.php diff --git a/DataFixtures/Loader/XmlDataLoader.php b/DataFixtures/Loader/XmlDataLoader.php new file mode 100644 index 0000000..efc3738 --- /dev/null +++ b/DataFixtures/Loader/XmlDataLoader.php @@ -0,0 +1,61 @@ + + */ +class XmlDataLoader extends AbstractDataLoader +{ + /** + * {@inheritdoc} + */ + protected function transformDataToArray($file) + { + $xml = simplexml_load_file($file); + + return $this->simpleXmlToArray($xml); + } + + /** + * @param SimpleXMLElement $xml + * @return array + */ + protected function simpleXmlToArray($xml) + { + $array = array(); + if ($xml instanceof \SimpleXMLElement) { + foreach ($xml as $key => $value) { + // First make a valid key which is the Ns (Namespace) attribute + // + the element name (the class name) + foreach ($value->attributes() as $subkey => $subvalue) { + if ('Namespace' === (string) $subkey) { + $key = $subvalue . '\\' . $key; + break; + } + } + + $array[$key] = array(); + foreach ($value as $elementKey => $elementValue) { + $array[$key][$elementKey] = array(); + + foreach ($elementValue->attributes() as $subkey => $subvalue) { + $array[$key][$elementKey][$subkey] = (string) $subvalue; + } + } + } + } + + return $array; + } +} diff --git a/Resources/config/propel.xml b/Resources/config/propel.xml index b948478..88c8edb 100644 --- a/Resources/config/propel.xml +++ b/Resources/config/propel.xml @@ -15,6 +15,7 @@ Propel\PropelBundle\Form\TypeGuesser Propel\PropelBundle\DataFixtures\Dumper\YamlDataDumper Propel\PropelBundle\DataFixtures\Loader\YamlDataLoader + Propel\PropelBundle\DataFixtures\Loader\XmlDataLoader @@ -53,5 +54,11 @@ %kernel.root_dir% + + + %kernel.root_dir% + + %propel.configuration% +