Skip to content

Commit b7915c1

Browse files
authored
Merge pull request #70 from kschroeder/develop
Updated to better handle child nodes of the //element node
2 parents ebd6f52 + d117cb5 commit b7915c1

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

lib/Config/Builder.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,27 @@ protected function mergeElements(\SimpleXMLElement $group, \SimpleXMLElement $ne
309309
foreach ($newElement->attributes() as $name => $value) {
310310
$element[$name] = (string)$value;
311311
}
312-
foreach ($newElement->children() as $key => $item) {
313-
$element->$key = (string)$item;
314-
}
312+
$this->mergeAllChildren($element, $newElement);
315313
}
316314
}
317315
}
318316

317+
protected function mergeAllChildren(\SimpleXMLElement $destination, \SimpleXMLElement $source)
318+
{
319+
foreach ($source->attributes() as $name => $value) {
320+
$destination[$name] = (string)$value;
321+
}
322+
foreach ($source->children() as $key => $item) {
323+
$childNodes = $destination->xpath(sprintf('/%s', $key));
324+
if (!empty($childNodes) && $childNodes[0] instanceof \SimpleXMLElement) {
325+
$element = $childNodes[0];
326+
} else {
327+
$value = trim((string)$item);
328+
$element = $destination->addChild($key, $value);
329+
}
330+
$this->mergeAllChildren($element, $item);
331+
332+
}
333+
}
334+
319335
}

tests/Config/BuilderTest.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,21 @@
44

55
use Interop\Container\ContainerInterface;
66
use Magium\Configuration\Config\Builder;
7-
use Magium\Configuration\Config\BuilderFactory;
87
use Magium\Configuration\Config\BuilderInterface;
9-
use Magium\Configuration\Config\Repository\ConfigInterface;
10-
use Magium\Configuration\Config\Repository\ConfigurationRepository;
118
use Magium\Configuration\Config\InsufficientContainerException;
129
use Magium\Configuration\Config\InvalidArgumentException;
1310
use Magium\Configuration\Config\InvalidConfigurationLocationException;
1411
use Magium\Configuration\Config\InvalidDirectoryException;
1512
use Magium\Configuration\Config\MergedStructure;
1613
use Magium\Configuration\Config\MissingConfigurationException;
14+
use Magium\Configuration\Config\Repository\ConfigInterface;
15+
use Magium\Configuration\Config\Repository\ConfigurationRepository;
1716
use Magium\Configuration\Config\Storage\StorageInterface;
1817
use Magium\Configuration\Config\UncallableCallbackException;
1918
use Magium\Configuration\File\Configuration\ConfigurationFileRepository;
2019
use Magium\Configuration\File\Configuration\UnsupportedFileTypeException;
21-
use Magium\Configuration\File\InvalidFileException;
2220
use Magium\Configuration\File\Configuration\XmlFile;
21+
use Magium\Configuration\File\InvalidFileException;
2322
use Magium\Configuration\InvalidConfigurationException;
2423
use Magium\Configuration\Tests\Container\ModelInjected;
2524
use PHPUnit\Framework\TestCase;
@@ -81,6 +80,26 @@ public function testInvalidConfigurationFileThrowsException()
8180
$builder->build();
8281
}
8382

83+
public function testEnsureThatElementChildrenAreIncluded()
84+
{
85+
$repository = ConfigurationFileRepository::getInstance(
86+
[__DIR__],
87+
[
88+
realpath(__DIR__ . '/xml/config-merge-1.xml'),
89+
realpath(__DIR__ . '/xml/config-merge-2.xml')
90+
]
91+
);
92+
$builder = new Builder(
93+
$this->getCacheStorageMock(),
94+
$this->getPersistenceStorageMock(),
95+
$repository
96+
);
97+
$merged = $builder->getMergedStructure();
98+
$merged->registerXPathNamespace('s', 'http://www.magiumlib.com/Configuration');
99+
$thatThing = $merged->xpath('//s:element[@identifier="title"]/descendant::s:value[@identifier]');
100+
self::assertCount(0, $thatThing);
101+
}
102+
84103
public function testBuildConfigurationNewChildren()
85104
{
86105
/*

tests/Config/xml/config-merge-2.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
<section identifier="general" label="General">
66
<group identifier="website" label="Website">
77
<element identifier="title" label="Title">
8+
<permittedValues>
9+
<value>My Homepage</value>
10+
<value>Your Homepage</value>
11+
</permittedValues>
812
<value>My Homepage</value>
913
</element>
1014
<element identifier="languages" label="Languages" source="Namespace\MySource" type="multi"/>

0 commit comments

Comments
 (0)