Skip to content

Commit 525d99e

Browse files
[12.x] Add collection() to Config repository (#56200)
* [12.x] Add `collection()` to Config repository * Update Repository.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 74811d4 commit 525d99e

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/Illuminate/Config/Repository.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use ArrayAccess;
66
use Illuminate\Contracts\Config\Repository as ConfigContract;
77
use Illuminate\Support\Arr;
8+
use Illuminate\Support\Collection;
89
use Illuminate\Support\Traits\Macroable;
910
use InvalidArgumentException;
1011

@@ -177,6 +178,18 @@ public function array(string $key, $default = null): array
177178
return $value;
178179
}
179180

181+
/**
182+
* Get the specified array configuration value as a collection.
183+
*
184+
* @param string $key
185+
* @param (\Closure():(array<array-key, mixed>|null))|array<array-key, mixed>|null $default
186+
* @return Collection<array-key, mixed>
187+
*/
188+
public function collection(string $key, $default = null): Collection
189+
{
190+
return new Collection($this->array($key, $default));
191+
}
192+
180193
/**
181194
* Set a given configuration value.
182195
*

src/Illuminate/Support/Facades/Config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* @method static float float(string $key, \Closure|float|null $default = null)
1212
* @method static bool boolean(string $key, \Closure|bool|null $default = null)
1313
* @method static array array(string $key, \Closure|array|null $default = null)
14+
* @method static \Illuminate\Support\Collection collection(string $key, \Closure|array|null $default = null)
1415
* @method static void set(array|string $key, mixed $value = null)
1516
* @method static void prepend(string $key, mixed $value)
1617
* @method static void push(string $key, mixed $value)

tests/Config/RepositoryTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Illuminate\Tests\Config;
44

55
use Illuminate\Config\Repository;
6+
use Illuminate\Support\Collection;
67
use InvalidArgumentException;
78
use PHPUnit\Framework\TestCase;
89

@@ -319,6 +320,14 @@ public function testItThrowsAnExceptionWhenTryingToGetNonArrayValueAsArray(): vo
319320
$this->repository->array('a.b');
320321
}
321322

323+
public function testItGetsAsCollection(): void
324+
{
325+
$collection = $this->repository->collection('array');
326+
327+
$this->assertInstanceOf(Collection::class, $collection);
328+
$this->assertSame(['aaa', 'zzz'], $collection->toArray());
329+
}
330+
322331
public function testItGetsAsBoolean(): void
323332
{
324333
$this->assertTrue(

0 commit comments

Comments
 (0)