danilopolani / laravel-array-destructuring
强大的Laravel数组解构
1.0.0
2021-02-26 14:08 UTC
Requires
- php: ^7.4|^8.0
- illuminate/support: ^8.0
Requires (Dev)
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-09-26 22:22:37 UTC
README
通过添加一个destructure
方法来扩展Arr
支持,在Laravel中实现强大的数组解构。
目录
安装
该包支持Laravel 8.x
和PHP >= 7.4
。
您可以通过Composer安装此包
composer require danilopolani/laravel-array-destructuring
由于自动发现功能,它将自动注册。
使用
注意:如果找不到键,其值将是
null
。如果一组键中的所有元素都未找到,则返回的值将为空数组[]
。
仅针对单个键的基本解构
$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
使用多个键解构
$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']
使用多个分组键解构
$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); // []
变更日志
有关最近更改的更多信息,请参阅变更日志。
贡献
有关详细信息,请参阅贡献指南。
测试
克隆存储库并运行以下命令
composer test
使用Docker(Windows)
docker run --rm -v %cd%:/app composer:latest bash -c "cd /app && composer install --ignore-platform-reqs && ./vendor/bin/phpunit"
使用Docker(Linux/OSX)
docker run --rm -v $(pwd):/app composer:latest bash -c "cd /app && composer install && ./vendor/bin/phpunit"
安全
如果您发现任何安全问题,请通过danilo.polani@gmail.com发送电子邮件,而不是使用问题跟踪器。
鸣谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。
Laravel包模板
此包是用Laravel包模板生成的。