Laravel Explicit Array
Improved Laravel dot notation for explicit array keys.
🤝
Supporting
If you are using one or more Renoki Co. open-source packages in your production apps, in presentation demos, hobby projects, school projects or so, spread some kind words about our work or sponsor our work via Patreon.
You will sometimes get exclusive content on tips about Laravel, AWS or Kubernetes on Patreon and some early-access to projects or packages.
🚀
Installation
You can install the package via composer:
composer require renoki-co/laravel-explicit-array
🙌
Usage
The original Laravel's Arr::set()
method treats the dots within the key as separators for nested values. This is expected. The segments will create a nested value some -> annotation -> com/ttl
with a value of 1800
.
$annotations = [
'some.annotation.com/ttl' => 900,
];
Arr::set($annotations, 'some.annotation.com/ttl', 1800);
// Current result
// [
// 'some' => [
// 'annotation' => [
// 'com/ttl' => 1800
// ]
// ]
// ]
// Desired result
// [
// 'some.annotation.com/ttl' => 1800
// ]
To fix this, Explicit Array introduces a new RenokiCo\ExplicitArray\Arr
class, which altered the ::set()
method, so that will make sure to read the segments between quotes as literal keys.
You may use this class as your regular Arr
class because it extends the original \Illuminate\Support\Arr
class.
use RenokiCo\ExplicitArray\Arr;
Arr::set($annotations, '"some.annotation.com/ttl"', 1800);
// [
// 'some.annotation.com/ttl' => 1800
// ]
This can work with mixed segments, meaning that as long as you keep the dots outside the quotes, you can specify nested values:
use RenokiCo\ExplicitArray\Arr;
Arr::set($annotations, 'annotations.nested."some.annotation.com/ttl"', 1800);
// [
// 'annotations' => [
// 'nested' => [
// 'some.annotation.com/ttl' => 1800
// ]
// ]
// ]
🐛
Testing
vendor/bin/phpunit
🤝
Contributing
Please see CONTRIBUTING for details.
🔒
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.