Jake Vanderwerf
2026-01-28 8c6502de2f8ec2bd8382cd6945c327d7be400e14
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
namespace JVBase\managers\queue\mergers;
 
use JVBase\managers\queue\Mergeable;
use JVBase\managers\queue\Operation;
 
/**
 * Default merger, uses the chunkKey as the mergeable array
 */
final class DefaultMerger implements Mergeable
{
    public function __construct(
        private string|array $chunkKey
    ) {}
 
    public function canMerge(Operation $existing, Operation $incoming): bool
    {
        foreach ((array) $this->chunkKey as $key) {
            if (!isset($incoming->requestData[$key])) {
                return false;
            }
        }
        return true;
    }
 
    public function merge(Operation $existing, Operation $incoming): Operation
    {
        foreach ((array) $this->chunkKey as $key) {
            $existing->requestData[$key] = array_merge(
                $existing->requestData[$key] ?? [],
                $incoming->requestData[$key] ?? []
            );
        }
 
        $existing->totalItems += $incoming->totalItems;
        return $existing;
    }
}