8000 [12.x] Add `collection()` to Config repository by KennedyTedesco · Pull Request #56200 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[12.x] Add collection() to Config repository #56200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[12.x] Add collection() to Config repository
  • Loading branch information
KennedyTedesco committed Jul 2, 2025
commit 0f3e86ff7560d030c96a016cb126cf2c45c90581
13 changes: 13 additions & 0 deletions src/Illuminate/Config/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ArrayAccess;
use Illuminate\Contracts\Config\Repository as ConfigContract;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;

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

/**
* Get the specified array configuration value as a collection.
*
* @param string $key
* @param (\Closure():(array<array-key, mixed>|null))|array<array-key, mixed>|null $default
* @return Collection<array-key, mixed>
*/
public function collection(string $key, $default = null): Collection
{
return collect($this->array($key, $default));
}

/**
* Set a given configuration value.
*
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Support/Facades/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* @method static float float(string $key, \Closure|float|null $default = null)
* @method static bool boolean(string $key, \Closure|bool|null $default = null)
* @method static array array(string $key, \Closure|array|null $default = null)
* @method static \Illuminate\Support\Collection collection(string $key, \Closure|array|null $default = null)
* @method static void set(array|string $key, mixed $value = null)
* @method static void prepend(string $key, mixed $value)
* @method static void push(string $key, mixed $value)
Expand Down
9 changes: 9 additions & 0 deletions tests/Config/RepositoryTest.php
5C9D
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Tests\Config;

use Illuminate\Config\Repository;
use Illuminate\Support\Collection;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

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

public function testItGetsAsCollection(): void
{
$collection = $this->repository->collection('array');

$this->assertInstanceOf(Collection::class, $collection);
$this->assertSame(['aaa', 'zzz'], $collection->toArray());
}

public function testItGetsAsBoolean(): void
{
$this->assertTrue(
Expand Down
0