Skip to content

Commit

Permalink
Merge pull request #31 from boesing/feature/factories
Browse files Browse the repository at this point in the history
feat: add factories to create ordered lists or maps
  • Loading branch information
boesing authored Dec 14, 2020
2 parents a1209cf + 6bb2f29 commit 6d113f6
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/MapFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Boesing\TypedArrays;

interface MapFactoryInterface
{
/**
* @template TKey of string
* @template TValue
* @param array<TKey,TValue> $data
* @return MapInterface<TKey,TValue>
*/
public function createMap(array $data): MapInterface;
}
17 changes: 17 additions & 0 deletions src/OrderedListFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Boesing\TypedArrays;

interface OrderedListFactoryInterface
{
/**
* @param list<TValue> $values
*
* @return OrderedListInterface<TValue>
*
* @template TValue
*/
public function createOrderedList(array $values): OrderedListInterface;
}
18 changes: 18 additions & 0 deletions src/TypedArrayFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Boesing\TypedArrays;

final class TypedArrayFactory implements OrderedListFactoryInterface, MapFactoryInterface
{
public function createMap(array $data): MapInterface
{
return new GenericMap($data);
}

public function createOrderedList(array $values): OrderedListInterface
{
return new GenericOrderedList($values);
}
}
32 changes: 32 additions & 0 deletions tests/TypedArrayFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Boesing\TypedArrays;

use PHPUnit\Framework\TestCase;

final class TypedArrayFactoryTest extends TestCase
{
/** @var TypedArrayFactory */
private $factory;

protected function setUp(): void
{
parent::setUp();
$this->factory = new TypedArrayFactory();
}

public function testCanCreateMapInstance(): void
{
$map = $this->factory->createMap(['foo' => 'bar']);

self::assertEquals(['foo' => 'bar'], $map->toNativeArray());
}

public function testCanCreateOrderedListInstance(): void
{
$list = $this->factory->createOrderedList(['foo', 'bar']);
self::assertEquals(['foo', 'bar'], $list->toNativeArray());
}
}

0 comments on commit 6d113f6

Please sign in to comment.