-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from boesing/feature/factories
feat: add factories to create ordered lists or maps
- Loading branch information
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |