konkod/evenly-distribute

任何事物的均匀分布的通用服务

1.0.0 2024-06-13 09:22 UTC

This package is auto-updated.

Last update: 2024-09-13 18:12:01 UTC


README

任何事物的均匀分布的通用服务

特性

  • 按字段非唯一实体ID进行排序

  • 根据池ID中定义的值进行均匀分配

    • (删除池ID的每个元素,并使用唯一的本地池ID构建数组)

用法

// See EvenlyDistributeServiceTest::testExampleWorksFine

$input = [
        ['poolId' => 'A', 'uniqueEntityId' => '123', 'nonUniqueEntityId' => 1, 'entity' => ['id' => 123]],
        ['poolId' => 'B', 'uniqueEntityId' => '456', 'nonUniqueEntityId' => 2, 'entity' => ['id' => 456]],
        ['poolId' => 'B', 'uniqueEntityId' => '678', 'nonUniqueEntityId' => 2, 'entity' => ['id' => 678]],
];

$output = EvenlyDistributeService::distributeByPools($input);

$excepted = [
    'A' => [
        ['poolId' => 'A', 'uniqueEntityId' => '123', 'nonUniqueEntityId' => 1, 'entity' => ['id' => 123]]
    ],
    'B' => [
        ['poolId' => 'B', 'uniqueEntityId' => '456', 'nonUniqueEntityId' => 2, 'entity' => ['id' => 456]]
    ],
];

echo $excepted === $output;

测试

# Run phpunit
vendor/bin/phpunit tests

# PhpStan
composer run phpstan