michiruf/laravel-collection-differ

用于比较集合的微型助手

0.1 2024-08-30 13:40 UTC

This package is auto-updated.

Last update: 2024-09-06 16:41:45 UTC


README

Run Tests

用于比较集合的微型助手。

安装

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();

对于额外的运行示例,请查看 测试