feat: ColorDiff beschreibt Farbänderungen (angelegt, geändert, deaktiviert, aktiviert) samt Zusammenfassung
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5.5
parent
2428801bc9
commit
39a5782562
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace LIT\MCP\Integrations\Divi\Design;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* What a color change does (spec §4 step 4, "diff"): per color added, changed (with the changed fields), deactivated
|
||||
* or activated (status change; "fields" lists further changed fields), removed (only a restore can drop a color).
|
||||
* lastUpdated and usedInPosts are no change of their own. Sorted by ID. Pure PHP.
|
||||
*/
|
||||
final class ColorDiff
|
||||
{
|
||||
private const COMPARED = ['color', 'folder', 'label', 'status'];
|
||||
|
||||
/**
|
||||
* @return list<array{id: string, name: string, action: string, fields?: list<string>}>
|
||||
* action: added|changed|deactivated|activated|removed
|
||||
*/
|
||||
public static function between(ColorBook $before, ColorBook $after): array
|
||||
{
|
||||
$ids = array_unique(array_merge(array_keys($before->data()), array_keys($after->data())));
|
||||
sort($ids);
|
||||
$rows = [];
|
||||
foreach ($ids as $id) {
|
||||
$id = (string) $id;
|
||||
$was = $before->entry($id);
|
||||
$is = $after->entry($id);
|
||||
if ($was === null || $is === null) {
|
||||
$book = $is === null ? $before : $after;
|
||||
$rows[] = ['id' => $id, 'name' => $book->label($id), 'action' => $is === null ? 'removed' : 'added'];
|
||||
continue;
|
||||
}
|
||||
$action = self::action($was['status'], $is['status']);
|
||||
// A status switch is the action itself; "fields" then names only the other changes.
|
||||
$skip = $action === 'changed' ? [] : ['status'];
|
||||
$fields = array_values(array_filter(
|
||||
self::COMPARED,
|
||||
static fn (string $key): bool => $was[$key] !== $is[$key] && ! in_array($key, $skip, true)
|
||||
));
|
||||
$row = ['id' => $id, 'name' => $after->label($id), 'action' => $action];
|
||||
if ($fields !== []) {
|
||||
$row['fields'] = $fields;
|
||||
}
|
||||
if ($fields !== [] || $action !== 'changed') {
|
||||
$rows[] = $row;
|
||||
}
|
||||
}
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* One German sentence for the dry run and the change log (spec §4 step 6).
|
||||
*
|
||||
* @param list<array<string, mixed>> $diff
|
||||
*/
|
||||
public static function summary(array $diff): string
|
||||
{
|
||||
$count = array_count_values(array_map(static fn (array $row): string => (string) $row['action'], $diff));
|
||||
$words = [
|
||||
'added' => 'angelegt', 'changed' => 'geändert', 'deactivated' => 'deaktiviert',
|
||||
'activated' => 'aktiviert', 'removed' => 'gelöscht',
|
||||
];
|
||||
$parts = [];
|
||||
foreach ($words as $action => $word) {
|
||||
if (isset($count[$action])) {
|
||||
$parts[] = $count[$action] . ' ' . $word;
|
||||
}
|
||||
}
|
||||
|
||||
return 'Farben: ' . ($parts === [] ? 'keine Änderung' : implode(', ', $parts)) . '.';
|
||||
}
|
||||
|
||||
private static function action(string $was, string $is): string
|
||||
{
|
||||
return match (true) {
|
||||
$was === $is => 'changed',
|
||||
$is === 'inactive' => 'deactivated',
|
||||
$is === 'active' => 'activated',
|
||||
default => 'changed',
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user