Skip to content

Commit 7d7bff4

Browse files
committed
Started to implement Configuration, added more tests.
1 parent a964425 commit 7d7bff4

File tree

5 files changed

+239
-1
lines changed

5 files changed

+239
-1
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
/*
4+
* This file is a part of dflydev/dot-access-configuration.
5+
*
6+
* (c) Dragonfly Development Inc.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Dflydev\DotAccessConfiguration;
13+
14+
class Configuration implements ConfigurationInterface
15+
{
16+
/**
17+
* Internal representation of configuration data
18+
*
19+
* @var array
20+
*/
21+
protected $data;
22+
23+
/**
24+
* Constructor
25+
*
26+
* @param array|null $data
27+
*/
28+
public function __construct(array $data = null)
29+
{
30+
$this->data = $data ?: array();
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function append($key, $value = null)
37+
{
38+
//
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function set($key, $value = null)
45+
{
46+
//
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function remove($key)
53+
{
54+
//
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
public function get($key)
61+
{
62+
$currentValue = $this->data;
63+
$keyPath = explode('.', $key);
64+
65+
for ( $i = 0; $i < count($keyPath); $i++ ) {
66+
$currentKey = $keyPath[$i];
67+
if (!isset($currentValue[$currentKey]) ) { return null; }
68+
$currentValue = $currentValue[$currentKey];
69+
}
70+
71+
return $currentValue;
72+
}
73+
74+
/**
75+
* {@inheritdoc}
76+
*/
77+
public function getConfiguration($key)
78+
{
79+
$value = $this->get($key);
80+
if (is_array($value) && Util::isAssoc($value)) {
81+
return new Configuration($value);
82+
}
83+
84+
throw new \RuntimeException("Value at '$key' could not be represented as a ConfigurationInterface");
85+
}
86+
87+
/**
88+
* {@inheritdoc}
89+
*/
90+
public function import(array $data, $clobber = true)
91+
{
92+
$this->data = Util::mergeAssocArray($this->data, $data, $clobber);
93+
}
94+
95+
/**
96+
* {@inheritdoc}
97+
*/
98+
public function importConfiguration(ConfigurationInterface $configuration, $clobber = true)
99+
{
100+
$this->import($configuration->export(), $clobber);
101+
}
102+
103+
/**
104+
* {@inheritdoc}
105+
*/
106+
public function export()
107+
{
108+
return $this->data;
109+
}
110+
}

src/Dflydev/DotAccessConfiguration/ConfigurationInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function getConfiguration($key);
5858
* @param array $data
5959
* @param bool $clobber
6060
*/
61-
public function importData(array $data, $clobber = true);
61+
public function import(array $data, $clobber = true);
6262

6363
/**
6464
* Import data from an external configuration into existing configuration

src/Dflydev/DotAccessConfiguration/Util.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@
1313

1414
class Util
1515
{
16+
/**
17+
* Test if array is an associative array
18+
*
19+
* Note that this function will return true if an array is empty. Meaning
20+
* empty arrays will be treated as if they are associative arrays.
21+
*
22+
* @param array $arr
23+
* @return boolean
24+
*/
25+
static public function isAssoc(array $arr)
26+
{
27+
return (is_array($arr) && (!count($arr) || count(array_filter(array_keys($arr),'is_string')) == count($arr)));
28+
}
29+
1630
/**
1731
* Merge contents from one associtative array to another
1832
*
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
/*
4+
* This file is a part of dflydev/dot-access-configuration.
5+
*
6+
* (c) Dragonfly Development Inc.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Dflydev\Tests\DotAccessConfiguration;
13+
14+
use Dflydev\DotAccessConfiguration\Configuration;
15+
use Dflydev\DotAccessConfiguration\ConfigurationInterface;
16+
17+
class ConfigurationTest extends \PHPUnit_Framework_TestCase
18+
{
19+
protected function getSampleData()
20+
{
21+
return array(
22+
'a' => 'A',
23+
'b' => array(
24+
'b' => 'B',
25+
'c' => array('C1', 'C2', 'C3'),
26+
'd' => array(
27+
'd1' => 'D1',
28+
'd2' => 'D2',
29+
'd3' => 'D3',
30+
),
31+
),
32+
'c' => array('c1', 'c2', 'c3'),
33+
);
34+
}
35+
36+
protected function runSampleDataTests(ConfigurationInterface $configuration)
37+
{
38+
$this->assertEquals('A', $configuration->get('a'));
39+
$this->assertEquals('B', $configuration->get('b.b'));
40+
$this->assertEquals(array('C1', 'C2', 'C3'), $configuration->get('b.c'));
41+
$this->assertEquals('D3', $configuration->get('b.d.d3'));
42+
$this->assertEquals(array('c1', 'c2', 'c3'), $configuration->get('c'));
43+
$this->assertEquals(null, $configuration->get('foo'), 'Foo should not exist');
44+
}
45+
46+
public function testAppend()
47+
{
48+
$this->markTestSkipped();
49+
}
50+
51+
public function testSet()
52+
{
53+
$this->markTestSkipped();
54+
}
55+
56+
public function testRemote()
57+
{
58+
$this->markTestSkipped();
59+
}
60+
61+
public function testGet()
62+
{
63+
$configuration = new Configuration($this->getSampleData());
64+
65+
$this->runSampleDataTests($configuration);
66+
}
67+
68+
public function testGetConfiguration()
69+
{
70+
$wrappedConfiguration = new Configuration(array(
71+
'wrapped' => array(
72+
'sampleData' => $this->getSampleData()
73+
),
74+
));
75+
76+
$configuration = $wrappedConfiguration->getConfiguration('wrapped.sampleData');
77+
78+
$this->runSampleDataTests($configuration);
79+
80+
$this->setExpectedException('RuntimeException');
81+
82+
$configuration = $wrappedConfiguration->getConfiguration('wrapped.sampleData.a');
83+
}
84+
85+
public function testImport()
86+
{
87+
$configuration = new Configuration();
88+
$configuration->import($this->getSampleData());
89+
90+
$this->runSampleDataTests($configuration);
91+
}
92+
93+
public function testImportConfiguration()
94+
{
95+
$configuration = new Configuration();
96+
$configuration->importConfiguration(new Configuration($this->getSampleData()));
97+
98+
$this->runSampleDataTests($configuration);
99+
}
100+
101+
public function testExport()
102+
{
103+
$configuration = new Configuration($this->getSampleData());
104+
105+
$this->assertEquals($this->getSampleData(), $configuration->export());
106+
}
107+
}

tests/Dflydev/Tests/DotAccessConfiguration/UtilTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515

1616
class UtilTest extends \PHPUnit_Framework_TestCase
1717
{
18+
public function testIsAssoc()
19+
{
20+
$this->assertTrue(Util::isAssoc(array('a' => 'A',)));
21+
$this->assertTrue(Util::isAssoc(array()));
22+
$this->assertFalse(Util::isAssoc(array(1 => 'One',)));
23+
}
24+
1825
/**
1926
* @dataProvider mergeAssocArrayProvider
2027
*/

0 commit comments

Comments
 (0)