Skip to content

danilopolani/laravel-array-destructuring

Repository files navigation

Laravel Array Destructuring

Latest Version on Packagist Build Status


Extend Arr support adding a destructure method to have a powerful array destruction in Laravel.

Table of Contents

  1. Installation
  2. Usage
  3. Changelog
  4. Contributing
  5. Testing
  6. Security
  7. Credits
  8. License

Installation

The package supports Laravel 8.x and PHP >= 7.4.

You can install the package via composer:

composer require danilopolani/laravel-array-destructuring

Thanks to the package auto-discovery feature, it will register itself automatically.

Usage

Note: if a key is not found its value will be null. If all the elements inside a group of keys are not found, the returned value will be an empty array [].

Basic destructuring for a key only

$post = [
	'title' => 'Article 1',
	'slug' => 'article-1',
	'description' => 'Lorem ipsum',
	'tags' => ['foo', 'bar'],
	'gallery' => [
	    ['image' => 'image.jpg'],
	    ['image' => 'image2.jpg'],
	],
];

[$tags, $article] = Arr::destructure($post, 'tags');

dump($tags); // ['foo', 'bar']
dump($article) // ['title' => 'Article 1', 'slug' => 'article-1', ...] without tags

[$notFoundKey, $article] = Arr::destructure($post, 'notFoundKey');

dump($notFoundKey); // null

Destructuring with multiple keys

$post = [
	'title' => 'Article 1',
	'slug' => 'article-1',
	'description' => 'Lorem ipsum',
	'tags' => ['foo', 'bar'],
	'gallery' => [
	    ['image' => 'image.jpg'],
	    ['image' => 'image2.jpg'],
	],
];

[$tags, $gallery, $article] = Arr::destructure($post, ['tags', 'gallery']);

dump($tags); // ['foo', 'bar']
dump($gallery); // [['image' => 'image.jpg'], ['image' => 'image2.jpg']]
dump($article) // ['title' => 'Article 1', 'slug' => 'article-1', 'description' => 'Lorem ipsum']

Destructuring with multiple grouped keys

$post = [
	'title' => 'Article 1',
	'slug' => 'article-1',
	'description' => 'Lorem ipsum',
	'tags' => ['foo', 'bar'],
	'gallery' => [
	    ['image' => 'image.jpg'],
	    ['image' => 'image2.jpg'],
	],
];

[$slug, $meta, $article] = Arr::destructure($post, ['slug', ['tags', 'gallery']]);

dump($slug); // article-1
dump($meta); // ['tags' => ['foo', 'bar'], 'gallery' => ['image' => 'image.jpg'], ['image' => 'image2.jpg']]
dump($article) // ['title' => 'Article 1', 'description' => 'Lorem ipsum']

[$notFoundGroup, $article] = Arr::destructure($post, [['notFound1', 'notFound2']]);

dump($notFoundGroup); // []

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Testing

Clone the repository and just run

composer test

With Docker (Windows):

docker run --rm -v %cd%:/app composer:latest bash -c "cd /app && composer install --ignore-platform-reqs && ./vendor/bin/phpunit"

With Docker (Linux/OSX):

docker run --rm -v $(pwd):/app composer:latest bash -c "cd /app && composer install && ./vendor/bin/phpunit"

Security

If you discover any security related issues, please email danilo.polani@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.

About

Powerful array destructuring for Laravel

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages