Repository navigation
Expand file tree
/
Copy pathStatusMessagesComparer.php
More file actions
63 lines (54 loc) · 1.78 KB
/
Copy pathStatusMessagesComparer.php
File metadata and controls
63 lines (54 loc) · 1.78 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
declare(strict_types=1);
namespace Rechtlogisch\Evatr\Util;
final class StatusMessagesComparer
{
/**
* Build a map of status code => German message from decoded JSON array.
*
* @param array<int, array<string, mixed>> $data
* @return array<string, string>
*/
public static function toGermanMap(array $data): array
{
$map = [];
foreach ($data as $item) {
$status = isset($item['status']) && is_string($item['status']) ? $item['status'] : '';
$message = isset($item['meldung']) && is_string($item['meldung']) ? $item['meldung'] : '';
if ($status !== '') {
$map[$status] = $message;
}
}
ksort($map);
return $map;
}
/**
* Compute differences between local and remote maps.
*
* @param array<string, string> $local
* @param array<string, string> $remote
* @return array{missing: list<string>, new: list<string>, changed: array<string, array{local: string, remote: string}>}
*/
public static function diff(array $local, array $remote): array
{
$missing = array_values(array_diff(array_keys($local), array_keys($remote)));
$new = array_values(array_diff(array_keys($remote), array_keys($local)));
$changed = [];
foreach ($local as $code => $text) {
if (array_key_exists($code, $remote) && $remote[$code] !== $text) {
$changed[$code] = [
'local' => $text,
'remote' => $remote[$code],
];
}
}
sort($missing);
sort($new);
ksort($changed);
return [
'missing' => $missing,
'new' => $new,
'changed' => $changed,
];
}
}