Skip to content

Commit

Permalink
Add ArrayUtils::isArrayEmpty and ArrayUtils::deleteKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis Fortunier committed Nov 5, 2024
1 parent 4758545 commit 9376c1e
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG_add_arry_utils.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Added
- `ArrayUtils::isArrayEmpty` + tests Check if an array, can be multidimensional, is empty
- `ArrayUtils::deleteKeys` + tests Delete keys of array and multidimensional array
64 changes: 64 additions & 0 deletions src/Utils/ArrayUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,68 @@ public static function hasDuplicateValue(array $array): bool
{
return count($array) !== count(array_flip($array));
}

/**
* Check if an array, can be multidimensional, is empty
* @param array $array <p>
* The input array.
* </p>
* @param array $emptyValues <p>
* values to be considered empty
* </p>
*
* <pre>
* <?php
* isArrayEmpty([[], [], ['0']], [0]);
* ?>
* </pre>
* The above example will output:
* <pre>
* true
* </pre>
*/
public static function isArrayEmpty(array $array, array $emptyValues = []): bool
{
foreach ($array as $value) {
if (is_array($value)) {
if (!self::isArrayEmpty($value, $emptyValues)) {
return false;
}
} else {
if (count($emptyValues) > 0) {
if (!in_array($value, $emptyValues)) {
return false;
}
} else {
return false;
}
}
}

return true;
}

/**
* Delete keys of array and multidimensional array
*
* <pre>
* <?php
* deleteKeys(['john' => 1, 'doe' => ['smart' => 100, 'booster' => 200]);
* ?>
* </pre>
* The above example will output:
* <pre>
* [1, [100, 200]]
* </pre>
*/
public static function deleteKeys(array $array): array
{
foreach ($array as $key => $value) {
if (is_array($value)) {
$array[$key] = self::deleteKeys($value);
}
}

return array_values($array);
}
}
54 changes: 54 additions & 0 deletions tests/Utils/ArrayUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,4 +606,58 @@ public function hasDuplicateProvider(): array
],
];
}

/**
* @dataProvider isArrayEmptyProvider
*/
public function testIsArrayEmpty(bool $expected, array $array): void
{
$this->assertSame($expected, ArrayUtils::isArrayEmpty($array));
}

public function isArrayEmptyProvider(): array
{
return [
'simple_array' => [true, []],
'multidimensional_array' => [true, [[], [], []]],
'multidimensional_array_with_0' => [false, [[0], [], []]],
'multidimensional_array_with_1' => [false, [[], [], [1]]],
];
}

/**
* @dataProvider isArrayEmptyValuesProvider
*/
public function testIsArrayEmptyValues(bool $expected, array $array, array $values): void
{
$this->assertSame($expected, ArrayUtils::isArrayEmpty($array, $values));
}

public function isArrayEmptyValuesProvider(): array
{
return [
'simple_array' => [true, [], [0]],
'multidimensional_array' => [true, [[], [], []], [0]],
'multidimensional_array_with_0' => [true, [[0], [], []], [0]],
'multidimensional_array_with_0_string' => [true, [['0'], [], []], [0]],
'multidimensional_array_with_multiple_values' => [true, [['1'], [], ['0']], [0, '1']],
'multidimensional_array_with_multiple_values_false' => [false, [['1'], ['2'], ['0']], [0, '1']],
];
}

/**
* @dataProvider deleteKeysProvider
*/
public function testDeleteKeys(array $expected, array $array): void
{
$this->assertSame($expected, ArrayUtils::deleteKeys($array));
}

public function deleteKeysProvider(): array
{
return [
'simple_array' => [[1, 2, 3], ['3' => 1, '2' => 2, '1' => 3]],
'multidimensional_array' => [[[1, 2], [3, 4]], [['dummy' => 1, 'test' => 2], ['8' => 3, '1' => 4]]],
];
}
}

0 comments on commit 9376c1e

Please sign in to comment.