Skip to content

Commit 0e3c36a

Browse files
committed
Add createFromArray method
1 parent 169244c commit 0e3c36a

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/ArrayReader.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class ArrayReader
1616
private $data;
1717

1818
/**
19-
* Constructor.
19+
* The constructor.
2020
*
2121
* @param array<mixed> $data Data
2222
*/
@@ -25,6 +25,18 @@ public function __construct(array $data = [])
2525
$this->data = $data;
2626
}
2727

28+
/**
29+
* Crate instance from array.
30+
*
31+
* @param array $data The data
32+
*
33+
* @return $this The new instance
34+
*/
35+
public static function createFromArray(array $data = []): self
36+
{
37+
return new static($data);
38+
}
39+
2840
/**
2941
* Get value as integer.
3042
*

tests/ArrayReaderTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@
1111
*/
1212
class ArrayReaderTest extends TestCase
1313
{
14+
/**
15+
* Test.
16+
*
17+
* @dataProvider providerGetInt
18+
*
19+
* @param mixed $data The data
20+
* @param string $key The lookup key
21+
* @param mixed $default The default value
22+
* @param mixed $expected The expected value
23+
*
24+
* @return void
25+
*/
26+
public function testCreateFromArray($data, string $key, $default, $expected)
27+
{
28+
$reader = ArrayReader::createFromArray($data);
29+
static::assertSame($expected, $reader->getInt($key, $default));
30+
}
31+
1432
/**
1533
* Test.
1634
*

0 commit comments

Comments
 (0)