bnomei / kirby3-collect
为 Kirby 添加对 Laravel 集合的支持
Requires
- php: >=8.0.0
- getkirby/composer-installer: ^1.2
- illuminate/collections: ^9.12
Requires (Dev)
- getkirby/cms: ^3.6
- php-coveralls/php-coveralls: ^2.1
- phpunit/phpunit: ^9.5
README
为 Kirby 添加对 Laravel 集合的支持
商业使用
支持开源!
此插件是免费的,但如果您在商业项目中使用它,请考虑赞助我或捐款。
如果我的工作帮助您赚了钱,我觉得我应该得到一点回报,对吧?
善良一点。分享一点。谢谢。
- Bruno
安装
- 将 master.zip 解压为文件夹
site/plugins/kirby3-collect
或 - 使用
git submodule add https://github.com/bnomei/kirby3-collect.git site/plugins/kirby3-collect
或 使用
composer require bnomei/kirby3-collect
为什么要用 Laravel 集合?
这是一个为在 Kirby 中创建需要处理大量数组数据的 Web 项目而设计的插件,这些项目希望 重构到集合 以避免过度使用 foreach
-循环和 if
-语句。
用法
有关如何使用 collect-helper 和集合方法的官方 Laravel 文档。
all, average, avg, chunk, chunkWhile, collapse, collect, combine, concat, contains, containsStrict, count, countBy, crossJoin, dd, diff, diffAssoc, diffKeys, doesntContain, dump duplicates, duplicatesStrict, each, eachSpread, every, except, filter, first, firstOrFail, firstWhere, flatMap, flatten, flip, forget, forPage, get, groupBy, has, implode, intersect, intersectByKeys, isEmpty, isNotEmpty, join, keyBy, keys, last, lazy, macro, make, map, mapInto, mapSpread, mapToGroups, mapWithKeys, max, median, merge, mergeRecursive, min, mode, nth, only, pad, partition, pipe, pipeInto, pipeThrough, pluck, pop, prepend, pull, push, put, random, range, reduce, reduceSpread, reject, replace, replaceRecursive, reverse search, shift, shuffle, sliding, skip, skipUntil, skipWhile, slice, sole, some, sort, sortBy, sortByDesc, sortDesc, sortKeys, sortKeysDesc, sortKeysUsing, splice, split, splitIn, sum, take, takeUntil, takeWhile, tap, times, toArray, toJson, transform, undot, union, unique, uniqueStrict, unless, unlessEmpty, unlessNotEmpty, unwrap, values, when, whenEmpty, whenNotEmpty, where, whereStrict, whereBetween, whereIn, whereInStrict, whereInstanceOf, whereNotBetween, whereNotIn, whereNotInStrict, whereNotNull, whereNull, wrap, zip
粗体显示的方法名表示 Kirby 有类似的集合方法。它还提供了一些额外的 方法。
从数组获取 Laravel 集合
您还可以从任何数组获取 Laravel 集合对象。
$laravelCollection = collect($array);
从 Kirby 集合创建 Laravel 集合
// retrieve a kirby collection $kirbyCollection = site()->index()->children(); // get a laravel collection object from a kirby collection $laravelCollection = $kirbyCollection->collect(); $laravelCollection = $kirbyCollection->_();
仅原数据类型
您还可以使用 JSON 编码和解码将所有对象转换为原始类型。但请注意,大多数 Kirby 的核心对象(尚未)将自动序列化自己。
$laravelCollection = $kirbyCollection->collect(true); $laravelCollection = $kirbyCollection->_(true);
何时使用 Laravel 集合方法(以及何时不使用)
为了实现某些常见用例,Laravel 集合方法比 Kirby 的集合方法更为明确。
$mostPopularPage = $kirbyCollection ->sortBy('viewcount') ->last() ->viewcount() ->toInt(); $mostPopularPage = $kirbyCollection ->collect() ->max('content.viewcount');
$hasProductWithBookcase = $kirbyCollection ->filterBy('product', '==', 'Bookcase') ->count() > 0; $hasProductWithBookcase = $kirbyCollection ->collect() ->contains('content.product', 'Bookcase');
但请记住,Kirby 的集合方法已经针对核心对象(如 Page
)进行了优化,使某些任务变得非常容易,而使用 Laravel 集合方法可能会更繁琐。
$allTags = $kirbyCollection ->pluck('tags', ', ', true); $allTags = $kirbyCollection ->collect() ->pluck('content.tags') ->flatMap(fn($item) => explode(', ', $item)) ->unique();
下划线缩写
每次调用 ->collect()
/_()
收集方法时,您也可以使用下划线来 开始 使用laravel集合对象。但是,任何后续的方法调用都不需要 _
前缀。
$mostPopularPage = $kirbyCollection ->_max('content.viewcount'); $hasProductWithBookcase = $kirbyCollection ->_contains('content.product', 'Bookcase'); $allTags = $kirbyCollection ->_pluck('content.tags') ->flatMap(fn($item) => explode(', ', $item)) ->unique();
依赖
免责声明
本插件提供“原样”且无任何保证。使用时请自行承担风险,并在将其用于生产环境之前始终自行测试。如果您发现任何问题,请创建新问题。
许可证
不鼓励在任何推广种族主义、性别歧视、恐同、动物虐待、暴力或其他任何形式的仇恨言论的项目中使用此插件。