<?php
|
namespace JVBase\managers\queue\mergers;
|
|
use JVBase\managers\queue\Mergeable;
|
use JVBase\managers\queue\Operation;
|
|
/**
|
* Field-aware merger for upload operations.
|
* Ensures uploads only merge when targeting the same field + post/term context.
|
* Post_group uploads (no field_name) merge by content type only.
|
*/
|
final class UploadMerger implements Mergeable
|
{
|
public function __construct(
|
private string|array $chunkKey
|
) {}
|
|
public function matchCriteria(Operation $incoming): array
|
{
|
$data = $incoming->requestData;
|
$criteria = [];
|
|
if (!empty($data['content'])) {
|
$criteria['content'] = $data['content'];
|
}
|
|
// Meta destination: scope to specific field + target
|
if (($data['destination'] ?? 'meta') === 'meta') {
|
if (!empty($data['field_name'])) {
|
$criteria['field_name'] = $data['field_name'];
|
}
|
if (!empty($data['post_id'])) {
|
$criteria['post_id'] = $data['post_id'];
|
} elseif (!empty($data['term_id'])) {
|
$criteria['term_id'] = $data['term_id'];
|
}
|
}
|
|
return $criteria;
|
}
|
|
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_replace(
|
$existing->requestData[$key] ?? [],
|
$incoming->requestData[$key] ?? []
|
);
|
}
|
|
$existing->totalItems += $incoming->totalItems;
|
return $existing;
|
}
|
}
|