mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 09:31:51 +01:00
[Arrays] Function that returns elements from given level
This commit is contained in:
@@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
Common and useful classes, methods, exceptions etc.
|
Common and useful classes, methods, exceptions etc.
|
||||||
|
|
||||||
|
# 1.1.6
|
||||||
|
|
||||||
|
1. [Arrays] Function that returns elements from given level
|
||||||
|
|
||||||
# 1.1.5
|
# 1.1.5
|
||||||
|
|
||||||
1. [BaseCollection] Prepare elements while adding them by `addMultiple()` method in the same way as passing them in
|
1. [BaseCollection] Prepare elements while adding them by `addMultiple()` method in the same way as passing them in
|
||||||
|
|||||||
@@ -1642,6 +1642,43 @@ class Arrays
|
|||||||
return '' === trim(implode('', $array));
|
return '' === trim(implode('', $array));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function getElementsFromLevel(array $array, int $level): ?array
|
||||||
|
{
|
||||||
|
if (empty($array) || $level <= 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = [];
|
||||||
|
|
||||||
|
foreach ($array as $value) {
|
||||||
|
// This is the expected level (the deepest). Comparing with 1, because level will be decreased by 1, and
|
||||||
|
// finally we will get the latest/deepest level that equals 1.
|
||||||
|
if ($level === 1) {
|
||||||
|
$result[] = $value;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// There is no deeper level
|
||||||
|
if (!is_array($value)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Let's dive one level down
|
||||||
|
$elements = self::getElementsFromLevel($value, $level - 1);
|
||||||
|
|
||||||
|
if ($elements === null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// I have to load each element separately to avoid issue with incorrectly nested values
|
||||||
|
foreach ($elements as $element) {
|
||||||
|
$result[] = $element;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns neighbour (next or previous element) for given element
|
* Returns neighbour (next or previous element) for given element
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1535,6 +1535,116 @@ letsTest[2] = value_2;';
|
|||||||
static::assertSame($expected, Arrays::containsEmptyStringsOnly($array));
|
static::assertSame($expected, Arrays::containsEmptyStringsOnly($array));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGetElementsFromLevelIfArrayIsEmpty(): void
|
||||||
|
{
|
||||||
|
self::assertNull(Arrays::getElementsFromLevel([], -1));
|
||||||
|
self::assertNull(Arrays::getElementsFromLevel([], 0));
|
||||||
|
self::assertNull(Arrays::getElementsFromLevel([], 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetElementsFromLevelIfThereIsNoGivenLevel(): void
|
||||||
|
{
|
||||||
|
self::assertSame([], Arrays::getElementsFromLevel([1, 2, 3], 9999));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetElementsFromLevelIfGivenLevelIsNotPositiveValue(): void
|
||||||
|
{
|
||||||
|
self::assertNull(Arrays::getElementsFromLevel([1, 2, 3], -1));
|
||||||
|
self::assertNull(Arrays::getElementsFromLevel([1, 2, 3], 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetElementsFromLevelIfArrayHasOneLevelOnly(): void
|
||||||
|
{
|
||||||
|
$array = [
|
||||||
|
// Level 1:
|
||||||
|
'ab',
|
||||||
|
'cd',
|
||||||
|
'ef',
|
||||||
|
];
|
||||||
|
|
||||||
|
self::assertSame($array, Arrays::getElementsFromLevel($array, 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testGetElementsFromLevel(): void
|
||||||
|
{
|
||||||
|
$array = [
|
||||||
|
// Level 1:
|
||||||
|
'ab',
|
||||||
|
[
|
||||||
|
// Level 2:
|
||||||
|
'cd',
|
||||||
|
'ef',
|
||||||
|
],
|
||||||
|
|
||||||
|
// Level 1:
|
||||||
|
[
|
||||||
|
// Level 2:
|
||||||
|
'gh',
|
||||||
|
[
|
||||||
|
// Level 3:
|
||||||
|
'ij',
|
||||||
|
'kl',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
|
||||||
|
// Level 1:
|
||||||
|
[
|
||||||
|
// Level 2:
|
||||||
|
[
|
||||||
|
// Level 3:
|
||||||
|
'mn',
|
||||||
|
'op',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
$expectedLevel1 = [
|
||||||
|
'ab',
|
||||||
|
[
|
||||||
|
'cd',
|
||||||
|
'ef',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'gh',
|
||||||
|
[
|
||||||
|
'ij',
|
||||||
|
'kl',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[
|
||||||
|
'mn',
|
||||||
|
'op',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
$expectedLevel2 = [
|
||||||
|
'cd',
|
||||||
|
'ef',
|
||||||
|
'gh',
|
||||||
|
[
|
||||||
|
'ij',
|
||||||
|
'kl',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'mn',
|
||||||
|
'op',
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
$expectedLevel3 = [
|
||||||
|
'ij',
|
||||||
|
'kl',
|
||||||
|
'mn',
|
||||||
|
'op',
|
||||||
|
];
|
||||||
|
|
||||||
|
self::assertSame($expectedLevel1, Arrays::getElementsFromLevel($array, 1));
|
||||||
|
self::assertSame($expectedLevel2, Arrays::getElementsFromLevel($array, 2));
|
||||||
|
self::assertSame($expectedLevel3, Arrays::getElementsFromLevel($array, 3));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides simple array to set/replace values with keys
|
* Provides simple array to set/replace values with keys
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user