|
| 1 | +# Laravel Array Destructuring |
| 2 | + |
| 3 | +[](https://packagist.org/packages/danilopolani/laravel-array-destructuring) |
| 4 | +[](https://travis-ci.com/danilopolani/laravel-array-destructuring) |
| 5 | + |
| 6 | +<!-- PROJECT LOGO --> |
| 7 | +<br /> |
| 8 | +<p align="center"> |
| 9 | + <a href="https://github.com/danilopolani/laravel-array-destructuring"> |
| 10 | + <img src="https://banners.beyondco.de/Laravel%20Array%20Destructuring.png?theme=light&packageManager=composer+require&packageName=danilopolani%2Flaravel-array-destructuring&pattern=dominos&style=style_1&description=Powerful+array+destructuring+for+Laravel&md=1&showWatermark=1&fontSize=100px&images=dots-horizontal"> |
| 11 | + </a> |
| 12 | +</p> |
| 13 | + |
| 14 | + |
| 15 | +Extend `Arr` support adding a `destructure` method to have a powerful array destruction in Laravel. |
| 16 | + |
| 17 | +<!-- TABLE OF CONTENTS --> |
| 18 | +## Table of Contents |
| 19 | +<ol> |
| 20 | + <li><a href="#installation">Installation</a></li> |
| 21 | + <li><a href="#usage">Usage</a></li> |
| 22 | + <li><a href="#changelog">Changelog</a></li> |
| 23 | + <li><a href="#contributing">Contributing</a></li> |
| 24 | + <li><a href="#testing">Testing</a></li> |
| 25 | + <li><a href="#security">Security</a></li> |
| 26 | + <li><a href="#credits">Credits</a></li> |
| 27 | + <li><a href="#license">License</a></li> |
| 28 | +</ol> |
| 29 | + |
| 30 | +<!-- GETTING STARTED --> |
| 31 | +## Installation |
| 32 | + |
| 33 | +The package supports `Laravel 8.x` and `PHP >= 7.4`. |
| 34 | + |
| 35 | +You can install the package via composer: |
| 36 | + |
| 37 | +```bash |
| 38 | +composer require danilopolani/laravel-array-destructuring |
| 39 | +``` |
| 40 | + |
| 41 | +Thanks to the package auto-discovery feature, it will register itself automatically. |
| 42 | + |
| 43 | +## Usage |
| 44 | + |
| 45 | +> **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 `[]`. |
| 46 | +
|
| 47 | +Basic destructuring for a key only |
| 48 | + |
| 49 | +```php |
| 50 | +$post = [ |
| 51 | + 'title' => 'Article 1', |
| 52 | + 'slug' => 'article-1', |
| 53 | + 'description' => 'Lorem ipsum', |
| 54 | + 'tags' => ['foo', 'bar'], |
| 55 | + 'gallery' => [ |
| 56 | + ['image' => 'image.jpg'], |
| 57 | + ['image' => 'image2.jpg'], |
| 58 | + ], |
| 59 | +]; |
| 60 | + |
| 61 | +[$tags, $article] = Arr::destructure($post, 'tags'); |
| 62 | + |
| 63 | +dump($tags); // ['foo', 'bar'] |
| 64 | +dump($article) // ['title' => 'Article 1', 'slug' => 'article-1', ...] without tags |
| 65 | + |
| 66 | +[$notFoundKey, $article] = Arr::destructure($post, 'notFoundKey'); |
| 67 | + |
| 68 | +dump($notFoundKey); // null |
| 69 | +``` |
| 70 | + |
| 71 | +Destructuring with multiple keys |
| 72 | + |
| 73 | +```php |
| 74 | +$post = [ |
| 75 | + 'title' => 'Article 1', |
| 76 | + 'slug' => 'article-1', |
| 77 | + 'description' => 'Lorem ipsum', |
| 78 | + 'tags' => ['foo', 'bar'], |
| 79 | + 'gallery' => [ |
| 80 | + ['image' => 'image.jpg'], |
| 81 | + ['image' => 'image2.jpg'], |
| 82 | + ], |
| 83 | +]; |
| 84 | + |
| 85 | +[$tags, $gallery, $article] = Arr::destructure($post, ['tags', 'gallery']); |
| 86 | + |
| 87 | +dump($tags); // ['foo', 'bar'] |
| 88 | +dump($gallery); // [['image' => 'image.jpg'], ['image' => 'image2.jpg']] |
| 89 | +dump($article) // ['title' => 'Article 1', 'slug' => 'article-1', ...] without tags and gallery |
| 90 | +``` |
| 91 | + |
| 92 | +Destructuring with multiple grouped keys |
| 93 | + |
| 94 | +```php |
| 95 | +$post = [ |
| 96 | + 'title' => 'Article 1', |
| 97 | + 'slug' => 'article-1', |
| 98 | + 'description' => 'Lorem ipsum', |
| 99 | + 'tags' => ['foo', 'bar'], |
| 100 | + 'gallery' => [ |
| 101 | + ['image' => 'image.jpg'], |
| 102 | + ['image' => 'image2.jpg'], |
| 103 | + ], |
| 104 | +]; |
| 105 | + |
| 106 | +[$slug, $meta, $article] = Arr::destructure($post, ['slug', ['tags', 'gallery']]); |
| 107 | + |
| 108 | +dump($slug); // article-1 |
| 109 | +dump($meta); // ['tags' => ['foo', 'bar'], 'gallery' => ['image' => 'image.jpg'], ['image' => 'image2.jpg']] |
| 110 | +dump($article) // ['title' => 'Article 1', 'slug' => 'article-1', ...] without slug, tags and gallery |
| 111 | + |
| 112 | +[$notFoundGroup, $article] = Arr::destructure($post, [['notFound1', 'notFound2']]); |
| 113 | + |
| 114 | +dump($notFoundGroup); // [] |
| 115 | +``` |
| 116 | + |
| 117 | +## Changelog |
| 118 | + |
| 119 | +Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. |
| 120 | + |
| 121 | +## Contributing |
| 122 | + |
| 123 | +Please see [CONTRIBUTING](CONTRIBUTING.md) for details. |
| 124 | + |
| 125 | +## Testing |
| 126 | + |
| 127 | +Clone the repository and just run |
| 128 | + |
| 129 | +``` bash |
| 130 | +composer test |
| 131 | +``` |
| 132 | + |
| 133 | +With Docker (Windows): |
| 134 | + |
| 135 | +```bash |
| 136 | +docker run --rm -v %cd%:/app composer:latest bash -c "cd /app && composer install --ignore-platform-reqs && ./vendor/bin/phpunit" |
| 137 | +``` |
| 138 | + |
| 139 | +With Docker (Linux/OSX): |
| 140 | + |
| 141 | +```bash |
| 142 | +docker run --rm -v $(pwd):/app composer:latest bash -c "cd /app && composer install && ./vendor/bin/phpunit" |
| 143 | +``` |
| 144 | + |
| 145 | +## Security |
| 146 | + |
| 147 | +If you discover any security related issues, please email [email protected] instead of using the issue tracker. |
| 148 | + |
| 149 | +## Credits |
| 150 | + |
| 151 | +- [Danilo Polani](https://github.com/danilopolani) |
| 152 | +- [All Contributors](../../contributors) |
| 153 | + |
| 154 | +## License |
| 155 | + |
| 156 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
| 157 | + |
| 158 | +## Laravel Package Boilerplate |
| 159 | + |
| 160 | +This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com). |
0 commit comments