Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/renoki-co/laravel-explicit-array

Improved Laravel dot notation for explicit array keys.
https://github.com/renoki-co/laravel-explicit-array

arr array laravel php support

Last synced: about 1 month ago
JSON representation

Improved Laravel dot notation for explicit array keys.

Awesome Lists containing this project

README

        

Laravel Explicit Array
======================

![CI](https://github.com/renoki-co/laravel-explicit-array/workflows/CI/badge.svg?branch=master)
[![codecov](https://codecov.io/gh/renoki-co/laravel-explicit-array/branch/master/graph/badge.svg)](https://codecov.io/gh/renoki-co/laravel-explicit-array/branch/master)
[![StyleCI](https://github.styleci.io/repos/421948177/shield?branch=master)](https://github.styleci.io/repos/421948177)
[![Latest Stable Version](https://poser.pugx.org/renoki-co/laravel-explicit-array/v/stable)](https://packagist.org/packages/renoki-co/laravel-explicit-array)
[![Total Downloads](https://poser.pugx.org/renoki-co/laravel-explicit-array/downloads)](https://packagist.org/packages/renoki-co/laravel-explicit-array)
[![Monthly Downloads](https://poser.pugx.org/renoki-co/laravel-explicit-array/d/monthly)](https://packagist.org/packages/renoki-co/laravel-explicit-array)
[![License](https://poser.pugx.org/renoki-co/laravel-explicit-array/license)](https://packagist.org/packages/renoki-co/laravel-explicit-array)

Improved Laravel dot notation for explicit array keys.

## 🚀 Installation

You can install the package via composer:

```bash
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`.

```php
$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.**

```php
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:

```php
use RenokiCo\ExplicitArray\Arr;

Arr::set($annotations, 'annotations.nested."some.annotation.com/ttl"', 1800);

// [
// 'annotations' => [
// 'nested' => [
// 'some.annotation.com/ttl' => 1800
// ]
// ]
// ]
```

## 🐛 Testing

``` bash
vendor/bin/phpunit
```

## 🤝 Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

## 🔒 Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

## 🎉 Credits

- [Alex Renoki](https://github.com/rennokki)
- [All Contributors](../../contributors)