/** * Gets the given section's config * @param array $data * @param string $section * @return \Nimbles\App\Config */ public function getSection(array $data, $section = null) { if (null === $section) { return new Config($data); } $config = new Config(); foreach ($data as $key => &$subconfig) { if (preg_match('/^[a-z0-9]+:[a-z0-9]+$/i', $key)) { list($child, $parent) = explode(':', $key); if (!$config->offsetExists($parent)) { throw new Exception\InvalidConfig('Invalid config, parent config not defined: ' . $parent); } $config->{$child} = clone $config->{$parent}; $config->{$child}->merge($subconfig); } $config->{$key} = $subconfig; } if ($config->offsetExists($section)) { return $config->{$section}; } // return first value, no match to environment found return reset($config); }
/** * Tests merging config objects * @return void */ public function testMerge() { $config1 = new Config(array('foo' => 123, 'bar' => 456, 'baz' => array(1, 2, 3))); $config2 = new Config(array('foo' => false, 'baz' => array(4, 5, 6), 'quz' => true)); $config1->merge($config2); $this->assertFalse($config1->foo); $this->assertEquals(456, $config1->bar); $this->assertSame(array(4, 5, 6), $config1->baz->getArrayCopy()); $this->assertTrue($config1->quz); $this->setExpectedException('Nimbles\\App\\Config\\Exception\\InvalidConfig'); $config1->merge(123); }