Skip to content

Commit

Permalink
Add removeTypes and removeNull to Arrays class (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
cardoso123inkt authored Nov 8, 2023
1 parent ed9f235 commit 8cbfbbe
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ A library with everyday use classes and methods:
- `remove` - Remove an element from an array based on the callback. Supports `EquatableInterface`.
- `removeKey` - Remove an element from an array based on the key.
- `removeKeys` - Remove multiple elements from an array based on the keys.
- `removeTypes` - Filters an array by removing all values that match the provided types.
- `removeNull` - Filters an array by removing all null values.
- `search` - Find the key of an element in an array or false otherwise. Supports `EquatableInterface`.
- `unique` - Remove duplicate values from an array. Supports `EquatableInterface`.
- `wrap` - Wrap a value in an array, unless it is already an array.
Expand Down
30 changes: 30 additions & 0 deletions src/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,36 @@ public static function removeKeys(array $items, array $keys, bool $caseSensitive
return array_filter($items, static fn($itemKey) => in_array(strtolower((string)$itemKey), $keys, true) === false, ARRAY_FILTER_USE_KEY);
}

/**
* Filter out elements of specified types from an array.
* @template T
* @template K
*
* @param array<K, T> $items
* @param string[] $disallowedTypes
*
* @return array<K, T> The filtered array containing only elements not matching the specified types.
*/
public static function removeTypes(array $items, array $disallowedTypes): array
{
return array_filter($items, static fn($element) => in_array(get_debug_type($element), $disallowedTypes, true) === false);
}

/**
* @template T
* @template K
* @param array<K, T|null> $items
*
* @return array<K, T>
*/
public static function removeNull($items): array
{
/** @var array<K, T> $result */
$result = self::removeTypes($items, ['null']);

return $result;
}

/**
* Strict test if `value` is contained within `items`. Method supports `EquatableInterface`.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/ArraysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace DR\Utils\Tests\Unit;

use DR\Utils\Arrays;
use DR\Utils\Assert;
use DR\Utils\Tests\Mock\MockComparable;
use DR\Utils\Tests\Mock\MockEquatable;
use InvalidArgumentException;
Expand Down Expand Up @@ -231,4 +232,22 @@ public function testWrap(): void
static::assertSame(['foobar'], Arrays::wrap('foobar'));
static::assertSame(['foobar'], Arrays::wrap(['foobar']));
}

public function testRemoveTypes(): void
{
$input = [false, 0, '0', 'false', true, 1, '1', 'true'];
static::assertEqualsCanonicalizing([0, '0', 'false', 1, '1', 'true'], Arrays::removeTypes($input, ['bool']));
static::assertSame(['null'], Arrays::removeTypes(['null', null], ['null']));
static::assertSame(['1', 2.00], Arrays::removeTypes(['1', 2.00, 3], ['int']));
static::assertSame([1, '2.00'], Arrays::removeTypes([1, '2.00', 3.00], ['float']));
static::assertSame([], Arrays::removeTypes(['UT string'], ['string']));
static::assertSame([], Arrays::removeTypes([[1, 2, 3]], ['array']));
static::assertSame([], Arrays::removeTypes([new stdClass()], ['stdClass']));
static::assertSame([], Arrays::removeTypes([new Arrays()], [Arrays::class]));
}

public function testRemoveNull(): void
{
static::assertSame(['null'], Arrays::removeNull(['null', null]));
}
}

0 comments on commit 8cbfbbe

Please sign in to comment.