Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
22 / 22 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| IdentificationDocumentsPolicyValue | |
100.00% |
22 / 22 |
|
100.00% |
3 / 3 |
7 | |
100.00% |
1 / 1 |
| normalize | |
100.00% |
18 / 18 |
|
100.00% |
1 / 1 |
5 | |||
| isEnabled | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getApprovers | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 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\IdentificationDocuments; |
| 10 | |
| 11 | final class IdentificationDocumentsPolicyValue { |
| 12 | /** @var list<string> */ |
| 13 | private const DEFAULT_APPROVERS = ['admin']; |
| 14 | |
| 15 | /** |
| 16 | * Normalize payload structure. |
| 17 | * Expected format: {enabled: bool, approvers: string[]} |
| 18 | * |
| 19 | * @return array{enabled: bool, approvers: list<string>} |
| 20 | */ |
| 21 | public static function normalize(mixed $rawValue, bool $enabledDefault = false): array { |
| 22 | if (!is_array($rawValue)) { |
| 23 | return [ |
| 24 | 'enabled' => $enabledDefault, |
| 25 | 'approvers' => self::DEFAULT_APPROVERS, |
| 26 | ]; |
| 27 | } |
| 28 | |
| 29 | $enabled = (bool)($rawValue['enabled'] ?? $enabledDefault); |
| 30 | $approvers = self::DEFAULT_APPROVERS; |
| 31 | |
| 32 | if (isset($rawValue['approvers']) && is_array($rawValue['approvers'])) { |
| 33 | $filtered = array_filter( |
| 34 | array_map('strval', $rawValue['approvers']), |
| 35 | static fn (string $v): bool => $v !== '' |
| 36 | ); |
| 37 | if (!empty($filtered)) { |
| 38 | $approvers = array_values($filtered); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return [ |
| 43 | 'enabled' => $enabled, |
| 44 | 'approvers' => $approvers, |
| 45 | ]; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Get enabled flag from payload. |
| 50 | */ |
| 51 | public static function isEnabled(mixed $rawValue, bool $default = false): bool { |
| 52 | $normalized = self::normalize($rawValue, $default); |
| 53 | return $normalized['enabled']; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Get approvers from payload. |
| 58 | * |
| 59 | * @return list<string> |
| 60 | */ |
| 61 | public static function getApprovers(mixed $rawValue): array { |
| 62 | $normalized = self::normalize($rawValue); |
| 63 | return $normalized['approvers']; |
| 64 | } |
| 65 | } |