diff --git a/README.md b/README.md index 546ee02..57f61c4 100644 --- a/README.md +++ b/README.md @@ -1 +1,7 @@ +[![Build Status](https://img.shields.io/github/checks-status/dq5studios/entity_column/master)](https://github.com/dq5studios/entity_column/actions) +[![codecov](https://codecov.io/gh/dq5studios/entity_column/branch/master/graph/badge.svg?token=4a2C2rnBuw)](https://codecov.io/gh/dq5studios/entity_column) +[![shepherd](https://shepherd.dev/github/dq5studios/entity_column/coverage.svg)](https://shepherd.dev/github/dq5studios/entity_column) + # entity_column + +A drop-in replacement for `array_column()` that supports objects with getters. diff --git a/composer.json b/composer.json index bdd8df3..714878d 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "dq5studios/entity_column", - "description": "", + "description": "A drop-in replacement for `array_column()` that supports objects with getters.", "type": "library", "license": "BSD-3-Clause", "authors": [ diff --git a/src/EntityColumn.php b/src/EntityColumn.php index a1eb2f9..7d90671 100644 --- a/src/EntityColumn.php +++ b/src/EntityColumn.php @@ -30,6 +30,7 @@ * @throws NoSuchIndexException * @throws AccessException * @throws InvalidArgumentException + * @throws NoSuchIndexException */ function entity_column( array $array, @@ -39,7 +40,9 @@ function entity_column( $output = []; static $accessor = null; /** @var PropertyAccessor */ - $accessor ??= PropertyAccess::createPropertyAccessor(); + $accessor ??= PropertyAccess::createPropertyAccessorBuilder() + ->enableExceptionOnInvalidIndex() + ->getPropertyAccessor(); foreach ($array as $row) { if (is_array($row)) { diff --git a/tests/EntityColumnTest.php b/tests/EntityColumnTest.php index 88b296f..4e5354a 100644 --- a/tests/EntityColumnTest.php +++ b/tests/EntityColumnTest.php @@ -17,9 +17,9 @@ class EntityColumnTest extends TestCase public function testCompareArrayWithArrayColumn(): void { $array = [ - ["key" => 1, "value" => "AAA"], - ["key" => 2, "value" => "BBB"], - ["key" => 3, "value" => "CCC"], + ["key" => 1, "value" => "Skimbleshanks"], + ["key" => 2, "value" => "Mungojerrie"], + ["key" => 3, "value" => "Rumpelteazer"], ]; $expected = array_column($array, "value", "key"); @@ -28,18 +28,32 @@ public function testCompareArrayWithArrayColumn(): void $this->assertEquals($expected, $actual); } + public function testCompareArrayListWithArrayColumn(): void + { + $array = [ + ["key" => 1, "value" => "Skimbleshanks"], + ["key" => 2, "value" => "Mungojerrie"], + ["key" => 3, "value" => "Rumpelteazer"], + ]; + + $expected = array_column($array, "value"); + $actual = entity_column($array, "value"); + + $this->assertEquals($expected, $actual); + } + public function testCompareObjectWithArrayColumn(): void { - $aaa = new stdClass(); - $aaa->key = 1; - $aaa->value = "AAA"; - $bbb = new stdClass(); - $bbb->key = 2; - $bbb->value = "BBB"; - $ccc = new stdClass(); - $ccc->key = 3; - $ccc->value = "BBB"; - $array = [$aaa, $bbb, $ccc]; + $Skimbleshanks = new stdClass(); + $Skimbleshanks->key = 1; + $Skimbleshanks->value = "Skimbleshanks"; + $Mungojerrie = new stdClass(); + $Mungojerrie->key = 2; + $Mungojerrie->value = "Mungojerrie"; + $Rumpelteazer = new stdClass(); + $Rumpelteazer->key = 3; + $Rumpelteazer->value = "Mungojerrie"; + $array = [$Skimbleshanks, $Mungojerrie, $Rumpelteazer]; $expected = array_column($array, "value", "key"); $actual = entity_column($array, "value", "key"); @@ -47,12 +61,32 @@ public function testCompareObjectWithArrayColumn(): void $this->assertEquals($expected, $actual); } + public function testCompareObjectListWithArrayColumn(): void + { + $Skimbleshanks = new stdClass(); + $Skimbleshanks->key = 1; + $Skimbleshanks->value = "Skimbleshanks"; + $Mungojerrie = new stdClass(); + $Mungojerrie->key = 2; + $Mungojerrie->value = "Mungojerrie"; + $Rumpelteazer = new stdClass(); + $Rumpelteazer->key = 3; + $Rumpelteazer->value = "Mungojerrie"; + $array = [$Skimbleshanks, $Mungojerrie, $Rumpelteazer]; + + $expected = array_column($array, "value"); + $actual = entity_column($array, "value"); + + $this->assertEquals($expected, $actual); + } + public function testObjectWithGetters(): void { - $expected = [1 => "AAA", 2 => "BBB", 3 => "CCC"]; - $class = new class { + $expected = [1 => "Skimbleshanks", 2 => "Mungojerrie", 3 => "Rumpelteazer"]; + $class = new class() { private ?int $key; private ?string $value; + public function __construct(int $key = null, string $value = null) { $this->key = $key; @@ -69,10 +103,40 @@ public function getValue(): string return $this->value; } }; - $array = [new $class(1, "AAA"), new $class(2, "BBB"), new $class(3, "CCC")]; + $array = [new $class(1, "Skimbleshanks"), new $class(2, "Mungojerrie"), new $class(3, "Rumpelteazer")]; $actual = entity_column($array, "value", "key"); $this->assertEquals($expected, $actual); } + + public function testObjectListWithGetters(): void + { + $expected = ["Skimbleshanks", "Mungojerrie", "Rumpelteazer"]; + $class = new class() { + private ?int $key; + private ?string $value; + + public function __construct(int $key = null, string $value = null) + { + $this->key = $key; + $this->value = $value; + } + + public function getKey(): int + { + return $this->key; + } + + public function getValue(): string + { + return $this->value; + } + }; + $array = [new $class(1, "Skimbleshanks"), new $class(2, "Mungojerrie"), new $class(3, "Rumpelteazer")]; + + $actual = entity_column($array, "value"); + + $this->assertEquals($expected, $actual); + } }