Skip to content

renoki-co/laravel-explicit-array

Repository files navigation

Laravel Explicit Array

CI codecov StyleCI Latest Stable Version Total Downloads Monthly Downloads License

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 [email protected] instead of using the issue tracker.

🎉 Credits