tobento / service-collection
用于处理数据数组的流畅且方便的包装类。
1.0.7
2023-06-30 15:24 UTC
Requires
- php: >=8.0
- tobento/service-macro: ^1.0
- tobento/service-support: ^1.0
Requires (Dev)
- phpunit/phpunit: ^9.5
- vimeo/psalm: ^4.0
README
集合服务提供了用于处理数据数组的流畅且方便的包装类。
目录
入门
使用以下命令添加最新版本的集合服务。
composer require tobento/service-collection
要求
- PHP 8.0 或更高版本
亮点
- 框架无关,适用于任何项目
- 解耦设计
文档
集合
add()
如果不存在或为 null,则添加项目值。
use Tobento\Service\Collection\Collection; $all = (new Collection([ 'key' => 'car', 'title' => 'Car' ])) ->add('sku', 'car') ->add('title', 'Car New') ->add('meta.color', 'blue') ->all(); /* Array ( [key] => car [title] => Car [sku] => car [meta] => Array ( [color] => blue ) ) */
all()
获取集合中的所有项目。
use Tobento\Service\Collection\Collection; $all = (new Collection([ 'key' => 'car', 'title' => 'Car' ]))->all(); /* Array ( [key] => car [title] => Car ) */
any()
检查是否存在至少一个项目。
use Tobento\Service\Collection\Collection; $exists = (new Collection([ 'key' => 'car', 'title' => 'Car', 'meta' => [ 'color' => 'blue' ] ]))->any('foo', 'meta.color'); var_dump($exists); // bool(true) $exists = (new Collection([ 'key' => 'car', 'title' => 'Car' ]))->any('bar', 'foo'); var_dump($exists); // bool(false)
combine()
将项目值与项目键组合。
use Tobento\Service\Collection\Collection; $all = (new Collection([ 'key', 'title', ]))->combine(['car', 'Car'])->all(); /* Array ( [key] => car [title] => Car ) */
count()
计算项目数量。
use Tobento\Service\Collection\Collection; $count = (new Collection([ 'key', 'title', ]))->count(); var_dump($count); // int(2)
delete()
通过键删除项目。
use Tobento\Service\Collection\Collection; $all = (new Collection([ 'key' => 'car', 'title' => 'Car', 'meta' => [ 'color' => 'red' ] ]))->delete('meta.color')->all(); /* Array ( [key] => car [title] => Car [meta] => Array ( ) ) */
empty()
检查集合或项目是否为空。
use Tobento\Service\Collection\Collection; $empty = (new Collection([ 'key' => 'car', 'title' => 'Car', 'meta' => [ 'color' => 'red' ] ]))->empty('meta.color'); var_dump($empty); // bool(false) $empty = (new Collection([ 'key' => 'car', 'title' => '' ]))->empty('title'); var_dump($empty); // bool(true) // check if collection is empty $empty = (new Collection([ 'key' => 'car', 'title' => '' ]))->empty(); var_dump($empty); // bool(false)
except()
获取除指定键外的所有项目。
use Tobento\Service\Collection\Collection; $all = (new Collection([ 'key' => 'car', 'title' => 'Car', 'meta' => [ 'color' => 'red', 'weight' => 1500, ] ]))->except(['key', 'meta.weight'])->all(); /* Array ( [title] => Car [meta] => Array ( [color] => red ) ) */
filter()
过滤项目。
use Tobento\Service\Collection\Collection; $all = (new Collection([ 'key', 'title', ]))->filter(fn($value, $key) => $key >= 1)->all(); /* Array ( [1] => title ) */
first()
获取通过给定真值测试的第一个项目。
use Tobento\Service\Collection\Collection; // by a truth test $first = (new Collection([ 2, 8, 9 ]))->first(fn($value) => $value > 2); var_dump($first); // int(8) // by a truth test with a fallback $first = (new Collection([ 2, 8, 9 ]))->first(fn($value) => $value > 12, 15); var_dump($first); // int(15) // without truth test $first = (new Collection([ 2, 8, 9 ]))->first(); var_dump($first); // int(2)
flatten()
将数组展平到单级数组。
use Tobento\Service\Collection\Collection; $all = (new Collection([ 'key' => 'car', 'title' => 'Car', 'meta' => [ 'foo' => 'bar' ] ]))->flatten()->all(); /* Array ( [0] => car [1] => Car [2] => bar ) */
get()
通过键获取项目值。
use Tobento\Service\Collection\Collection; $value = (new Collection([ 'key' => 'car', 'title' => 'Car' ]))->get('title'); var_dump($value); // string(3) "Car" // With fallback. If the value exist but does not match the fallback type, // it returns the fallback instead. $value = (new Collection([ 'key' => 'car', 'title' => 'Car' ]))->get('title', ['foo']); var_dump($value); // array(1) { [0]=> string(3) "foo" } // using dot notation for array depth $value = (new Collection([ 'key' => 'car', 'title' => 'Car', 'meta' => [ 'color' => 'red' ] ]))->get('meta.color'); var_dump($value); // string(3) "red"
has()
检查通过键的项目是否存在。
use Tobento\Service\Collection\Collection; $has = (new Collection([ 'key' => 'car', 'title' => 'Car' ]))->has('title', 'key'); var_dump($has); // bool(true) $has = (new Collection([ 'key' => 'car', 'title' => 'Car' ]))->has('title', 'foo'); var_dump($has); // bool(false) // using dot notation for array depth $has = (new Collection([ 'key' => 'car', 'title' => 'Car', 'meta' => [ 'color' => 'red' ] ]))->has('title', 'meta.color'); var_dump($has); // bool(true)
keys()
获取项目键。仅第一级数组。
use Tobento\Service\Collection\Collection; $all = (new Collection([ 'key' => 'car', 'title' => 'Car' ]))->keys()->all(); /* Array ( [0] => key [1] => title ) */ // With a search value $all = (new Collection([ 'blue', 'red', 'blue', 'green' ]))->keys('blue', strict: false)->all(); /* Array ( [0] => 0 [1] => 2 ) */
last()
获取通过给定真值测试的最后一个项目。
use Tobento\Service\Collection\Collection; // by a truth test $last = (new Collection([ 2, 8, 9 ]))->last(fn($value) => $value < 7); var_dump($last); // int(2) // by a truth test with a fallback $last = (new Collection([ 2, 8, 9 ]))->last(fn($value) => $value > 12, 15); var_dump($last); // int(15) // without truth test $last = (new Collection([ 2, 8, 9 ]))->last(); var_dump($last); // int(9)
map()
遍历每个项目。
use Tobento\Service\Collection\Collection; $all = (new Collection([ 'key' => 'car', 'title' => 'Car' ]))->map(fn($value, $key) => strtoupper($value))->all(); /* Array ( [key] => CAR [title] => CAR ) */
merge()
合并项目。
use Tobento\Service\Collection\Collection; $all = (new Collection([ 'key' => 'car', 'title' => 'Car' ]))->merge(['foo' => 'bar', 'title' => 'Car VW'])->all(); /* Array ( [key] => car [title] => Car VW [foo] => bar ) */
mergeRecursive()
递归合并项目。
use Tobento\Service\Collection\Collection; $all = (new Collection([ 'key' => 'car', 'title' => 'Car' ]))->mergeRecursive(['foo' => 'bar', 'title' => 'Car VW'])->all(); /* Array ( [key] => car [title] => Array ( [0] => Car [1] => Car VW ) [foo] => bar ) */
only()
获取指定键的项目。
use Tobento\Service\Collection\Collection; $all = (new Collection([ 'key' => 'car', 'title' => 'Car', 'meta' => [ 'color' => 'red', 'weight' => 1500, ] ]))->only(['title', 'meta.color'])->all(); /* Array ( [title] => Car [meta] => Array ( [color] => red ) ) */ // Define a default value. $all = (new Collection([ 'key' => 'car', 'title' => 'Car' ]))->only(['title', 'foo'], 'default value')->all(); /* Array ( [title] => Car [foo] => default value ) */
onlyPresent()
获取指定键存在的项目。
use Tobento\Service\Collection\Collection; $all = (new Collection([ 'key' => 'car', 'title' => 'Car', 'meta' => [ 'color' => 'red', 'weight' => 1500, ] ]))->onlyPresent(['title', 'meta.color', 'foo'])->all(); /* Array ( [title] => Car [meta] => Array ( [color] => red ) ) */
replace()
替换项目。
use Tobento\Service\Collection\Collection; $all = (new Collection([ 'key' => 'car', 'title' => 'Car' ]))->replace(['title' => 'Car VW', 'foo' => 'bar'])->all(); /* Array ( [key] => car [title] => Car VW [foo] => bar ) */
replaceRecursive()
递归替换项目。
use Tobento\Service\Collection\Collection; $all = (new Collection([ 'key' => 'car', 'title' => 'Car', 'colors' => [ 'blue', 'red' ] ]))->replaceRecursive(['colors' => [0 => 'green']])->all(); /* Array ( [key] => car [title] => Car [colors] => Array ( [0] => green [1] => red ) ) */
set()
通过键设置项目值。
use Tobento\Service\Collection\Collection; $all = (new Collection([ 'key' => 'car', 'title' => 'Car' ]))->set('color', 'red')->all(); /* Array ( [key] => car [title] => Car [color] => red ) */ // using dot notation for array depth $all = (new Collection([ 'key' => 'car', 'title' => 'Car' ]))->set('meta.color', 'red')->all(); /* Array ( [key] => car [title] => Car [meta] => Array ( [color] => red ) ) */
setAll()
设置所有项目。
use Tobento\Service\Collection\Collection; $all = (new Collection([ 'key' => 'car', 'title' => 'Car' ]))->setAll(['title' => 'foo'])->all(); /* Array ( [title] => foo ) */
toArray()
将集合转换为普通的 PHP 数组。
use Tobento\Service\Collection\Collection; $array = (new Collection([ 'key' => 'car', 'title' => 'Car' ]))->toArray(); /* Array ( [key] => car [title] => Car ) */
toJson()
将集合转换为 JSON 序列化的字符串。
use Tobento\Service\Collection\Collection; $json = (new Collection([ 'key' => 'car', 'title' => 'Car' ]))->toJson(); // {"key":"car","title":"Car"}
union()
与 merge 相同,但现有项目不会被覆盖。
use Tobento\Service\Collection\Collection; $all = (new Collection([ 'key' => 'car', 'title' => 'Car' ]))->union(['title' => 'foo', 'foo' => 'bar'])->all(); /* Array ( [key] => car [title] => Car [foo] => bar ) */
values()
获取项目值。
use Tobento\Service\Collection\Collection; $all = (new Collection([ 'key' => 'car', 'title' => 'Car' ]))->values()->all(); /* Array ( [0] => car [1] => Car ) */
翻译
创建一个新的翻译集合。
use Tobento\Service\Collection\Translations; $trans = new Translations([ 'de' => [ 'title' => 'title de', 'desc' => 'desc de', ], 'en' => [ 'title' => 'title en', 'desc' => 'desc en', ], 'fr' => [ 'title' => 'title fr', 'desc' => 'desc fr', ], ], 'en'); $trans->setLocaleFallbacks(['it' => 'en', 'de' => 'en']); // only get() method takes fallbacks into account. $trans->setLocaleMapping(['de-CH' => 'de', 1 => 'de']); // ['de-CH' (requested) => 'de' (stored)] $trans->setLocale('de'); // change default locale
setAll()
设置所有翻译。这将覆盖所有之前的翻译。
use Tobento\Service\Collection\Translations; $trans = new Translations(); $trans->setAll([ 'de' => [ 'title' => 'title de', 'desc' => 'desc de', ], ]);
set()
通过键设置翻译值。
use Tobento\Service\Collection\Translations; $trans = new Translations(locale: 'en'); $trans->setLocaleMapping(['de-CH' => 'de']); // set a title for the default locale. $trans->set('title', 'Title'); // set a title for specific locale. $trans->set('title', 'Title De', 'de'); // set a value for all current locales. $trans->set('foo', 'Bar', []); // set a value for specific locales. $trans->set('description', 'Description', ['de-CH', 'fr']); // using dot notation $trans->set('meta.color', 'red', []); /* Array ( [en] => Array ( [title] => Title [foo] => Bar [meta] => Array ( [color] => red ) ) [de] => Array ( [title] => Title De [foo] => Bar [description] => Description [meta] => Array ( [color] => red ) ) [fr] => Array ( [description] => Description [meta] => Array ( [color] => red ) ) ) */
add()
如果不存在或为 null,则通过键添加翻译值。
use Tobento\Service\Collection\Translations; $trans = new Translations(locale: 'en'); $trans->setLocaleMapping(['de-CH' => 'de']); // add title for default locale if not exist or is null. $trans->add('title', 'Title'); // add title for specific locale if not exist or is null. $trans->add('title', 'Title De', 'de'); // add a value for all current locales if not exist or is null. $trans->add('foo', 'Bar', []); // add a value for specific locales if not exist or is null. $trans->add('description', 'Description', ['de-CH', 'fr']); // using dot notation $trans->add('meta.color', 'red', []);
get()
获取默认区域值的值
use Tobento\Service\Collection\Translations; $trans = new Translations([ 'de' => [ 'title' => 'Title de', ], 'en' => [ 'title' => 'Title en', 'meta' => [ 'color' => 'blue', ], ], ], 'en'); // get title for default locale. var_dump($trans->get('title')); // string(8) "Title en" // with a fallback value. var_dump($trans->get('foo', 'default')); // string(7) "default" // using dot notation. var_dump($trans->get('meta.color')); // string(4) "blue"
获取特定区域值的值
use Tobento\Service\Collection\Translations; $trans = new Translations([ 'de' => [ 'title' => 'Title de', ], 'en' => [ 'title' => 'Title en', ], ], 'en'); $trans->setLocaleMapping(['de-CH' => 'de']); $trans->setLocaleFallbacks(['fr' => 'en']); // get title for specific locale. var_dump($trans->get('title', null, 'de-CH')); // string(8) "Title de" var_dump($trans->get('desc', null, 'de')); // NULL var_dump($trans->get('desc', 'Desc De', 'de')); // string(7) "Desc De" // As locale fallback is defined for fr and value fallback is null. var_dump($trans->get('title', null, 'fr')); // string(8) "Title en" // Does not fallback to en as fallback value is defined. var_dump($trans->get('title', 'Title Fr', 'fr')); // string(8) "Title Fr"
获取当前区域的值
use Tobento\Service\Collection\Translations; $trans = new Translations([ 'de' => [ 'title' => 'Title de', ], 'en' => [ 'title' => 'Title en', ], ], 'en'); $titles = $trans->get('title', null, []); /*Array ( [de] => Title de [en] => Title en )*/
获取特定区域的值
use Tobento\Service\Collection\Translations; $trans = new Translations([ 'de' => [ 'title' => 'Title de', ], 'en' => [ 'title' => 'Title en', ], ], 'en'); $trans->setLocaleMapping(['de-CH' => 'de']); // fr locale fallsback to the default locale en // if no specific locale fallback is defined and // fallback value is null. $titles = $trans->get('title', null, ['de-CH', 'fr']); /*Array ( [de-CH] => Title de [fr] => Title en )*/ $trans->setLocaleFallbacks(['fr' => 'de']); // with fallback value ignoring any locale fallbacks $titles = $trans->get('title', 'Fallback value', ['de', 'fr']); /*Array ( [de] => Title de [fr] => Fallback value )*/ // without fallback value uses the above locale fallbacks. $titles = $trans->get('title', null, ['de', 'fr']); /*Array ( [de] => Title de [fr] => Title de )*/
has()
如果通过键的翻译存在。
use Tobento\Service\Collection\Translations; $trans = new Translations([ 'de' => [ 'title' => 'Title de', ], 'en' => [ 'title' => 'Title en', 'meta' => [ 'color' => 'blue', ], ], ], 'en'); $trans->setLocaleMapping(['de-CH' => 'de']); // uses default locale. var_dump($trans->has('title')); // bool(true) // with specific locale. var_dump($trans->has('title', 'de')); // bool(true) // uses all current locales. var_dump($trans->has('title', [])); // bool(true) // with specific locales var_dump($trans->has('title', ['de-CH', 'en'])); // bool(true) // using dot notation. var_dump($trans->has('meta.color', ['en'])); // bool(true)
all()
获取所有翻译。
use Tobento\Service\Collection\Translations; $trans = new Translations([ 'de' => [ 'title' => 'Title de', ], 'en' => [ 'title' => 'Title en', ], ], 'en'); $trans->setLocaleMapping(['de-CH' => 'de']); // get all, default locale. $all = $trans->all(); // get all, specific locale. $all = $trans->all('de-CH'); // get all, all current locales. $all = $trans->all([]); // get all, specific locales. $all = $trans->all(['de-CH', 'fr']);
delete()
通过键删除翻译。
use Tobento\Service\Collection\Translations; $trans = new Translations([ 'de' => [ 'title' => 'Title de', ], 'en' => [ 'title' => 'Title en', 'meta' => [ 'color' => 'blue', ], ], ], 'en'); $trans->setLocaleMapping(['de-CH' => 'de']); // delete default locale title. $trans->delete('title'); // delete specific locale title. $trans->delete('title', 'de-CH'); // delete all current locales titles. $trans->delete('title', []); // delete specific locales titles. $trans->delete('title', ['de-CH', 'fr']); // using dot notation $trans->delete('meta.color', []);
deleteAll()
删除所有翻译。
use Tobento\Service\Collection\Translations; $trans = new Translations([ 'de' => [ 'title' => 'Title de', ], 'en' => [ 'title' => 'Title en', ], ], 'en'); $trans->setLocaleMapping(['de-CH' => 'de']); // delete all, default locale. $trans->deleteAll(); // delete all, specific locale. $trans->deleteAll('de-CH'); // delete all, all current locales. $trans->deleteAll([]); // delete all, specific locales. $trans->deleteAll(['de-CH', 'fr']);
toArray()
use Tobento\Service\Collection\Translations; $trans = new Translations([ 'de' => [ 'title' => 'Title de', ], 'en' => [ 'title' => 'Title en', ], ], 'en'); $array = $trans->toArray();
toJson()
use Tobento\Service\Collection\Translations; $trans = new Translations([ 'de' => [ 'title' => 'Title de', ], 'en' => [ 'title' => 'Title en', ], ], 'en'); $jsonString = $trans->toJson();
鸣谢
灵感和一些代码片段取自Collections of the Laravel 框架。