Skip to content

Commit

Permalink
Add fromArray() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gapple committed Jan 15, 2022
1 parent 726b83b commit 5b1f2d3
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions src/InnerList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
17 changes: 17 additions & 0 deletions src/OuterList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 24 additions & 0 deletions tests/DictionaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
25 changes: 25 additions & 0 deletions tests/OuterListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace gapple\Tests\StructuredFields;

use gapple\StructuredFields\InnerList;
use gapple\StructuredFields\Item;
use gapple\StructuredFields\OuterList;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -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);
}
}

0 comments on commit 5b1f2d3

Please sign in to comment.