Skip to content

Commit 520dd69

Browse files
committed
Add array fill macro
1 parent c94643b commit 520dd69

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

bootstrap/helpers.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ function str_chop(string $value, int $limit, string $center = '...'): string
2121
}
2222
}
2323

24+
if (! function_exists('array_fill_index_gaps')) {
25+
function array_fill_index_gaps(array $array, int $count, mixed $value): array
26+
{
27+
return \Illuminate\Support\Arr::fill($array, $count, $value);
28+
}
29+
}
30+
2431
if (! function_exists('preg_split_trim')) {
2532
function preg_split_trim(string $value, string $pattern = '/,|;|\s+/', int $sort = -1): array
2633
{

src/EssentialsServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ private function registerArrayMacros(): void
3636
{
3737
$macros = [
3838
'toJson' => \Codebarista\LaravelEssentials\Macros\Array\Json::class,
39+
'fill' => \Codebarista\LaravelEssentials\Macros\Array\Fill::class,
3940
];
4041

4142
foreach ($macros as $name => $class) {

src/Macros/Array/Fill.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Codebarista\LaravelEssentials\Macros\Array;
4+
5+
class Fill
6+
{
7+
public function __invoke(array $array, int $count, mixed $value): array
8+
{
9+
return array_replace(array_fill(0, $count, $value), $array);
10+
}
11+
}

0 commit comments

Comments
 (0)