3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Config\Writer
;
13 use Zend\Config\Exception
;
15 class Xml
extends AbstractWriter
18 * processConfig(): defined by AbstractWriter.
20 * @param array $config
23 public function processConfig(array $config)
25 $writer = new XMLWriter('UTF-8');
26 $writer->openMemory();
27 $writer->setIndent(true);
28 $writer->setIndentString(str_repeat(' ', 4));
30 $writer->startDocument('1.0', 'UTF-8');
31 $writer->startElement('zend-config');
33 foreach ($config as $sectionName => $data) {
34 if (!is_array($data)) {
35 $writer->writeElement($sectionName, (string) $data);
37 $this->addBranch($sectionName, $data, $writer);
41 $writer->endElement();
42 $writer->endDocument();
44 return $writer->outputMemory();
48 * Add a branch to an XML object recursively.
50 * @param string $branchName
51 * @param array $config
52 * @param XMLWriter $writer
54 * @throws Exception\RuntimeException
56 protected function addBranch($branchName, array $config, XMLWriter
$writer)
60 foreach ($config as $key => $value) {
61 if ($branchType === null) {
62 if (is_numeric($key)) {
63 $branchType = 'numeric';
65 $writer->startElement($branchName);
66 $branchType = 'string';
68 } elseif ($branchType !== (is_numeric($key) ?
'numeric' : 'string')) {
69 throw new Exception\
RuntimeException('Mixing of string and numeric keys is not allowed');
72 if ($branchType === 'numeric') {
73 if (is_array($value)) {
74 $this->addBranch($value, $value, $writer);
76 $writer->writeElement($branchName, (string) $value);
79 if (is_array($value)) {
80 $this->addBranch($key, $value, $writer);
82 $writer->writeElement($key, (string) $value);
87 if ($branchType === 'string') {
88 $writer->endElement();