Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
91.30% |
21 / 23 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ApprovalGroupsPolicyValue | |
91.30% |
21 / 23 |
|
66.67% |
2 / 3 |
10.07 | |
0.00% |
0 / 1 |
| decode | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
5 | |||
| encode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| normalizeGroupIds | |
81.82% |
9 / 11 |
|
0.00% |
0 / 1 |
4.10 | |||
| 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\Policy\Provider\ApprovalGroups; |
| 10 | |
| 11 | final class ApprovalGroupsPolicyValue { |
| 12 | /** @var list<string> */ |
| 13 | public const DEFAULT_GROUPS = ['admin']; |
| 14 | |
| 15 | /** @return list<string> */ |
| 16 | public static function decode(mixed $rawValue): array { |
| 17 | if (is_array($rawValue)) { |
| 18 | return self::normalizeGroupIds($rawValue); |
| 19 | } |
| 20 | |
| 21 | if (!is_string($rawValue)) { |
| 22 | return []; |
| 23 | } |
| 24 | |
| 25 | $trimmed = trim($rawValue); |
| 26 | if ($trimmed === '') { |
| 27 | return []; |
| 28 | } |
| 29 | |
| 30 | $decoded = json_decode($trimmed, true); |
| 31 | if (is_array($decoded)) { |
| 32 | return self::normalizeGroupIds($decoded); |
| 33 | } |
| 34 | |
| 35 | return self::normalizeGroupIds(array_map('trim', explode(',', $trimmed))); |
| 36 | } |
| 37 | |
| 38 | public static function encode(mixed $rawValue): string { |
| 39 | return json_encode(self::decode($rawValue), JSON_THROW_ON_ERROR); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @param array<mixed> $rawGroups |
| 44 | * @return list<string> |
| 45 | */ |
| 46 | private static function normalizeGroupIds(array $rawGroups): array { |
| 47 | $normalized = []; |
| 48 | foreach ($rawGroups as $groupId) { |
| 49 | if (!is_string($groupId)) { |
| 50 | continue; |
| 51 | } |
| 52 | |
| 53 | $trimmed = trim($groupId); |
| 54 | if ($trimmed === '') { |
| 55 | continue; |
| 56 | } |
| 57 | |
| 58 | $normalized[] = $trimmed; |
| 59 | } |
| 60 | |
| 61 | $unique = array_values(array_unique($normalized)); |
| 62 | sort($unique); |
| 63 | |
| 64 | return $unique; |
| 65 | } |
| 66 | } |