renoki-co / laravel-explicit-array
Improved Laravel dot notation for explicit array keys.
Fund package maintenance!
rennokki
Requires
- illuminate/support: ^9.0
Requires (Dev)
- mockery/mockery: ^1.5
- orchestra/testbench: ^7.0
- phpunit/phpunit: ^9.0
- vimeo/psalm: 4.19.0
This package is auto-updated.
Last update: 2024-11-27 05:39:31 UTC
README
Improved Laravel dot notation for explicit array keys.
🚀 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 alex@renoki.org instead of using the issue tracker.