Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
39 / 39 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| ResultFilter | |
100.00% |
39 / 39 |
|
100.00% |
4 / 4 |
16 | |
100.00% |
1 / 1 |
| unify | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
6 | |||
| scoreItem | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
| excludeEmpty | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| excludeNotAllowed | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | /** |
| 5 | * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors |
| 6 | * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | */ |
| 8 | |
| 9 | namespace OCA\Libresign\Service\Identify; |
| 10 | |
| 11 | class ResultFilter { |
| 12 | public function unify(array $list): array { |
| 13 | $ids = []; |
| 14 | $scores = []; |
| 15 | $return = []; |
| 16 | foreach ($list as $items) { |
| 17 | foreach ($items as $item) { |
| 18 | $shareWith = $item['value']['shareWith'] ?? null; |
| 19 | if ($shareWith === null) { |
| 20 | continue; |
| 21 | } |
| 22 | $score = $this->scoreItem($item); |
| 23 | $existingIndex = array_search($shareWith, $ids, true); |
| 24 | if ($existingIndex !== false) { |
| 25 | if ($score >= ($scores[$existingIndex] ?? -1)) { |
| 26 | $return[$existingIndex] = $item; |
| 27 | $scores[$existingIndex] = $score; |
| 28 | } |
| 29 | continue; |
| 30 | } |
| 31 | $ids[] = $shareWith; |
| 32 | $scores[] = $score; |
| 33 | $return[] = $item; |
| 34 | } |
| 35 | } |
| 36 | return $return; |
| 37 | } |
| 38 | |
| 39 | private function scoreItem(array $item): int { |
| 40 | $label = (string)($item['label'] ?? ''); |
| 41 | $unique = (string)($item['shareWithDisplayNameUnique'] ?? ''); |
| 42 | $shareWith = (string)($item['value']['shareWith'] ?? ''); |
| 43 | |
| 44 | if ($label === '') { |
| 45 | return 0; |
| 46 | } |
| 47 | if ($label !== $unique && $label !== $shareWith) { |
| 48 | return 2; |
| 49 | } |
| 50 | return 1; |
| 51 | } |
| 52 | |
| 53 | public function excludeEmpty(array $list): array { |
| 54 | return array_filter($list, fn ($result) => strlen((string)($result['value']['shareWith'] ?? '')) > 0); |
| 55 | } |
| 56 | |
| 57 | public function excludeNotAllowed(array $list, array $allowedMethods = []): array { |
| 58 | $allowedMethods = array_values(array_filter(array_map( |
| 59 | static fn (mixed $method): string => is_string($method) ? trim($method) : '', |
| 60 | $allowedMethods, |
| 61 | ), static fn (string $method): bool => $method !== '')); |
| 62 | |
| 63 | return array_filter($list, static function (array $result) use ($allowedMethods): bool { |
| 64 | if (!isset($result['method']) || empty($result['method'])) { |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | if ($allowedMethods === []) { |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | return in_array((string)$result['method'], $allowedMethods, true); |
| 73 | }); |
| 74 | } |
| 75 | } |