Allow Dictionary to take varargs as constructor arguments.
Allow Dictionary to take varargs as constructor arguments.
diff --git a/base/dictionary.php b/base/dictionary.php
index 2e792c2..e4d206e 100644
--- a/base/dictionary.php
+++ b/base/dictionary.php
@@ -26,12 +26,20 @@ require_once PHALANX_ROOT . '/base/key_descender.php';
// object, the contents are copied rather than referenced.
class Dictionary extends KeyDescender
{
+ // A Dictionary can either be created from a descendable type, or from
+ // a varargs of key-value pairs. If the varags constructor is used,
+ // the form is |new Dictionary(key1, val1, key2, val2).
public function __construct($properties = array())
{
- if (self::IsDescendable($properties))
- $this->root = $properties;
- else
+ $args = func_get_args();
+ if (count($args) == 1 && self::IsDescendable($args[0])) {
+ $this->root = $args[0];
+ } else {
$this->root = array();
+ for ($i = 0; $i < count($args); $i += 2) {
+ $this->root[$args[$i]] = $args[$i + 1];
+ }
+ }
}
// Sets a key-value pair.
@@ -43,7 +51,7 @@ class Dictionary extends KeyDescender
// Returns the value for a given key.
public function __get($key)
{
- return $this->getSilent($key);
+ return $this->GetSilent($key);
}
// Returns the number of items in the Dictionary.
diff --git a/testing/tests/base/dictionary_test.php b/testing/tests/base/dictionary_test.php
index 450f08e..636e6c1 100644
--- a/testing/tests/base/dictionary_test.php
+++ b/testing/tests/base/dictionary_test.php
@@ -26,6 +26,19 @@ class DictionaryTest extends \PHPUnit_Framework_TestCase
$this->bag = new base\Dictionary();
}
+ public function testAraryCtor()
+ {
+ $this->bag = new base\Dictionary(array('foo' => 'bar'));
+ $this->assertEquals('bar', $this->bag->foo);
+ }
+
+ public function testVarArgsCtor()
+ {
+ $this->bag = new base\Dictionary('foo', 'bar', 'baz', 'boo');
+ $this->assertEquals('bar', $this->bag->foo);
+ $this->assertEquals('boo', $this->bag->baz);
+ }
+
public function testCount()
{
$this->assertEquals(0, $this->bag->Count());
@@ -53,7 +66,7 @@ class DictionaryTest extends \PHPUnit_Framework_TestCase
$this->bag->bar = 'hij';
$keys = array('foo', 'moo', 'bar');
- $this->assertEquals($keys, $this->bag->allKeys());
+ $this->assertEquals($keys, $this->bag->AllKeys());
}
public function testAllValues()
@@ -63,7 +76,7 @@ class DictionaryTest extends \PHPUnit_Framework_TestCase
$this->bag->bar = 'hij';
$values = array('abc', 'def', 'hij');
- $this->assertEquals($values, $this->bag->allValues());
+ $this->assertEquals($values, $this->bag->AllValues());
}
public function testToArray()
@@ -77,7 +90,7 @@ class DictionaryTest extends \PHPUnit_Framework_TestCase
'moo' => 'def',
'bar' => 'hij'
);
- $this->assertEquals($array, $this->bag->toArray());
+ $this->assertEquals($array, $this->bag->ToArray());
}
public function testHasKey()
@@ -90,7 +103,7 @@ class DictionaryTest extends \PHPUnit_Framework_TestCase
public function testContains()
{
$this->bag->foo = 'moo';
- $this->assertTrue($this->bag->contains('moo'));
- $this->assertFalse($this->bag->contains('foo'));
+ $this->assertTrue($this->bag->Contains('moo'));
+ $this->assertFalse($this->bag->Contains('foo'));
}
}