Skip to content

Commit 7058660

Browse files
committed
Arrays::insertBefore and Arrays::insertAfter added
1 parent c34e688 commit 7058660

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/Arrays.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,64 @@ public static function mapByProperty($subject, $keyPropertyPath)
777777
return $output;
778778
}
779779

780+
/**
781+
* @param array $subject
782+
* @param string|int $key
783+
* @param mixed $value
784+
* @param string|int $insertAfterKey
785+
* @return array
786+
*/
787+
public static function insertAfter(array $subject, $key, $value, $insertAfterKey)
788+
{
789+
ParamsChecker::isIntOrString('$key', $key, __METHOD__);
790+
ParamsChecker::isIntOrString('$insertAfterKey', $insertAfterKey, __METHOD__);
791+
$output = [];
792+
$inserted = false;
793+
foreach ($subject as $i => $item)
794+
{
795+
$output[$i] = $item;
796+
if (!$inserted && $i == $insertAfterKey)
797+
{
798+
$output[$key] = $value;
799+
$inserted = true;
800+
}
801+
}
802+
if (!$inserted)
803+
{
804+
$output[$key] = $value;
805+
}
806+
return $output;
807+
}
808+
809+
/**
810+
* @param array $subject
811+
* @param string|int $key
812+
* @param mixed $value
813+
* @param string|int $insertBeforeKey
814+
* @return array
815+
*/
816+
public static function insertBefore(array $subject, $key, $value, $insertBeforeKey)
817+
{
818+
ParamsChecker::isIntOrString('$key', $key, __METHOD__);
819+
ParamsChecker::isIntOrString('$insertBeforeKey', $insertBeforeKey, __METHOD__);
820+
$output = [];
821+
$inserted = false;
822+
foreach ($subject as $i => $item)
823+
{
824+
if (!$inserted && $i == $insertBeforeKey)
825+
{
826+
$output[$key] = $value;
827+
$inserted = true;
828+
}
829+
$output[$i] = $item;
830+
}
831+
if (!$inserted)
832+
{
833+
$output[$key] = $value;
834+
}
835+
return $output;
836+
}
837+
780838
/**
781839
* @param array $subject
782840
* @param string $keyPath

tests/ArraysTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,27 @@ public function testMergeArrayConfig()
302302
self::assertException(function () use ($config, $defaultConfig) { Arrays::mergeArrayConfig($config, $defaultConfig, "blue"); }, InvalidArgumentException::class);
303303
}
304304

305+
public static function testInsertAfter()
306+
{
307+
$target = ["one" => 1, "three" => 3];
308+
self::assertEquals(["one" => 1, "two" => 2, "three" => 3], Arrays::insertAfter($target, "two", 2, "one"));
309+
self::assertEquals(["one" => 1, "three" => 3, "key" => "key"], Arrays::insertAfter($target, "key", "key", "two"));
310+
$target = [1 => 1, 3 => 3];
311+
self::assertEquals([1 => 1, 2 => 2, 3 => 3], Arrays::insertAfter($target, 2, 2, 1));
312+
self::assertEquals([1 => 1], Arrays::insertAfter([], 1, 1, "one"));
313+
self::assertException(function () use ($target) { Arrays::insertAfter($target, null, 2, "one"); }, InvalidArgumentException::class);
314+
self::assertException(function () use ($target) { Arrays::insertAfter($target, 2, 2, null); }, InvalidArgumentException::class);
315+
}
316+
317+
public static function testInsertBefore()
318+
{
319+
$target = ["one" => 1, "three" => 3];
320+
self::assertEquals(["one" => 1, "two" => 2, "three" => 3], Arrays::insertBefore($target, "two", 2, "three"));
321+
self::assertEquals(["one" => 1, "three" => 3, "key" => "key"], Arrays::insertBefore($target, "key", "key", "two"));
322+
$target = [1 => 1, 3 => 3];
323+
self::assertEquals([1 => 1, 2 => 2, 3 => 3], Arrays::insertBefore($target, 2, 2, 3));
324+
self::assertEquals([1 => 1], Arrays::insertBefore([], 1, 1, "one"));
325+
self::assertException(function () use ($target) { Arrays::insertBefore($target, null, 2, "one"); }, InvalidArgumentException::class);
326+
self::assertException(function () use ($target) { Arrays::insertBefore($target, 2, 2, null); }, InvalidArgumentException::class);
327+
}
305328
}

0 commit comments

Comments
 (0)