diff --git a/src/Dictionary.php b/src/Dictionary.php index 76405ed..d747156 100644 --- a/src/Dictionary.php +++ b/src/Dictionary.php @@ -9,6 +9,24 @@ class Dictionary implements \IteratorAggregate */ protected $value = []; + public static function fromArray(array $array): Dictionary + { + $dictionary = new static(); + + foreach ($array as $key => $value) { + if (!$value instanceof TupleInterface) { + if (is_array($value)) { + $value = InnerList::fromArray($value); + } else { + $value = new Item($value); + } + } + $dictionary->{$key} = $value; + } + + return $dictionary; + } + public function __get($name) { return $this->value[$name] ?? null; diff --git a/src/InnerList.php b/src/InnerList.php index 14cc39d..c35152a 100644 --- a/src/InnerList.php +++ b/src/InnerList.php @@ -19,6 +19,20 @@ public function __construct(array $value, ?object $parameters = null) } } + public static function fromArray(array $array): InnerList + { + $list = new static([]); + + foreach ($array as $item) { + if (!$item instanceof TupleInterface) { + $item = new Item($item); + } + $list->value[] = $item; + } + + return $list; + } + private static function validateItemType($value): void { if (is_object($value)) { diff --git a/src/OuterList.php b/src/OuterList.php index 290746d..b72bd49 100644 --- a/src/OuterList.php +++ b/src/OuterList.php @@ -18,6 +18,23 @@ public function __construct($value = []) $this->value = $value; } + public static function fromArray($array): OuterList + { + $list = new static(); + foreach ($array as $value) { + if (!$value instanceof TupleInterface) { + if (is_array($value)) { + $value = InnerList::fromArray($value); + } else { + $value = new Item($value); + } + } + $list[] = $value; + } + + return $list; + } + private static function validateItemType($value): void { if (is_object($value)) { diff --git a/src/Parameters.php b/src/Parameters.php index a0ff4a8..6fce0f9 100644 --- a/src/Parameters.php +++ b/src/Parameters.php @@ -9,7 +9,7 @@ class Parameters implements \IteratorAggregate */ protected $value = []; - public static function fromArray($array): Parameters + public static function fromArray(array $array): Parameters { $parameters = new static(); $parameters->value = $array; diff --git a/tests/DictionaryTest.php b/tests/DictionaryTest.php index 3c9a195..854c480 100644 --- a/tests/DictionaryTest.php +++ b/tests/DictionaryTest.php @@ -3,6 +3,8 @@ namespace gapple\Tests\StructuredFields; use gapple\StructuredFields\Dictionary; +use gapple\StructuredFields\InnerList; +use gapple\StructuredFields\Item; use PHPUnit\Framework\TestCase; class DictionaryTest extends TestCase @@ -20,4 +22,26 @@ public function testPropertyAccess() unset($dictionary->key); $this->assertFalse(isset($dictionary->key)); } + + public function testFromArray() + { + $dictionary = Dictionary::fromArray([ + 'one' => true, + 'two' => new Item(false), + 'three' => [ + 'four', + new Item('five'), + ], + ]); + + $expected = new Dictionary(); + $expected->one = new Item(true); + $expected->two = new Item(false); + $expected->three = new InnerList([ + new Item('four'), + new Item('five'), + ]); + + $this->assertEquals($expected, $dictionary); + } } diff --git a/tests/OuterListTest.php b/tests/OuterListTest.php index eaddcde..d56a347 100644 --- a/tests/OuterListTest.php +++ b/tests/OuterListTest.php @@ -2,6 +2,8 @@ namespace gapple\Tests\StructuredFields; +use gapple\StructuredFields\InnerList; +use gapple\StructuredFields\Item; use gapple\StructuredFields\OuterList; use PHPUnit\Framework\TestCase; @@ -122,4 +124,27 @@ public function testAppendInvalidItem($value) $list = new OuterList(); $list[] = $value; } + + public function testFromArray() + { + $dictionary = OuterList::fromArray([ + true, + new Item(false), + [ + 'four', + new Item('five'), + ], + ]); + + $expected = new OuterList([ + new Item(true), + new Item(false), + new InnerList([ + new Item('four'), + new Item('five'), + ]), + ]); + + $this->assertEquals($expected, $dictionary); + } }