michiruf / laravel-collection-differ
用于比较集合的微型助手
0.1
2024-08-30 13:40 UTC
Requires
- php: ^8.2
- illuminate/contracts: ^10.0||^11.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- laravel/pint: ^1.14
- laravel/sail: ^1.31
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- spatie/laravel-ray: ^1.35
README
用于比较集合的微型助手。
安装
composer require michiruf/laravel-collection-differ
使用方法
结果使用示例
$result = collect([1, 2, 3])->differ([2, 4])->diff(); dump($result->unmatchedSource); // [1, 3] dump($result->unmatchedDestination); // [4] dump($result->matched); // [[2, 2]]
回调使用示例
ProductModel::all() ->differ(ProductApi::getAll()) ->handleUnmatchedSourceUsing(fn (ProductModel $model) => $model->delete()) ->handleUnmatchedDestinationUsing(fn (ProductDto) $dto => ProductModel::createFromDto($dto)) ->handleMatchedUsing(fn (ProductModel $model, ProductDto $dto) => $model->updateWithDto($dto)) ->diff();
标识符使用示例
ProductModel::all() ->differ(ProductApi::getAll()) ->identifySourceUsing(fn (ProductModel $model) => $model->id) ->identifyDestinationUsing('meta.id') // or: fn (ProductDto $dto) => $dto->meta->id ->diff();
对于额外的运行示例,请查看 测试。