Skip to content

Commit

Permalink
Throw on missing index, extra tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dq5studios committed May 25, 2023
1 parent dcd065b commit f5066b5
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 18 deletions.
6 changes: 6 additions & 0 deletions 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.
2 changes: 1 addition & 1 deletion 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": [
Expand Down
5 changes: 4 additions & 1 deletion src/EntityColumn.php
Expand Up @@ -30,6 +30,7 @@
* @throws NoSuchIndexException
* @throws AccessException
* @throws InvalidArgumentException
* @throws NoSuchIndexException
*/
function entity_column(
array $array,
Expand All @@ -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)) {
Expand Down
96 changes: 80 additions & 16 deletions tests/EntityColumnTest.php
Expand Up @@ -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");
Expand All @@ -28,31 +28,65 @@ 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");

$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;
Expand All @@ -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);
}
}

0 comments on commit f5066b5

Please sign in to comment.