Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
RulesService
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRule
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getHelperText
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4/**
5 * SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
6 * SPDX-License-Identifier: AGPL-3.0-or-later
7 */
8
9namespace OCA\Libresign\Service\Certificate;
10
11use OCP\IL10N;
12
13class RulesService {
14
15    private array $rules = [
16        'CN' => [
17            'type' => 'string',
18            'required' => true,
19            'min' => 1,
20            'max' => 64,
21        ],
22        'C' => [
23            'type' => 'string',
24            'min' => 2,
25            'max' => 2,
26        ],
27        'ST' => [
28            'type' => 'string',
29            'min' => 1,
30            'max' => 128,
31        ],
32        'L' => [
33            'type' => 'string',
34            'min' => 1,
35            'max' => 128,
36        ],
37        'O' => [
38            'type' => 'string',
39            'min' => 1,
40            'max' => 64,
41        ],
42        'OU' => [
43            'type' => 'array',
44            'required' => false,
45            'min' => 1,
46            'max' => 64,
47            'minItems' => 0,
48            'maxItems' => 10,
49        ],
50    ];
51
52    public function __construct(
53        protected IL10N $l10n,
54    ) {
55
56    }
57
58    public function getRule(string $fieldName): array {
59        if (!isset($this->rules[$fieldName]['helperText'])) {
60            $this->rules[$fieldName]['helperText'] = $this->getHelperText($fieldName);
61            if (empty($this->rules[$fieldName]['helperText'])) {
62                unset($this->rules[$fieldName]['helperText']);
63            }
64        }
65        return $this->rules[$fieldName];
66    }
67
68    public function getHelperText(string $fieldName): ?string {
69        return match ($fieldName) {
70            // TRANSLATORS Label for the X.509 certificate subject Common Name (CN) field shown when generating a signing certificate.
71            'CN' => $this->l10n->t('Common Name (CN)'),
72            // TRANSLATORS Help text for the X.509 certificate subject country (C) field; value must be a two-letter ISO 3166 code.
73            'C' => $this->l10n->t('Two-letter ISO 3166 country code'),
74            // TRANSLATORS Help text for the X.509 certificate subject state or province (ST) field.
75            'ST' => $this->l10n->t('Full name of states or provinces'),
76            // TRANSLATORS Help text for the X.509 certificate subject locality (L) field such as city or region.
77            'L' => $this->l10n->t('Name of a locality or place, such as a city, county, or other geographic region'),
78            // TRANSLATORS Help text for the X.509 certificate subject organization (O) field.
79            'O' => $this->l10n->t('Name of an organization'),
80            // TRANSLATORS Help text for the X.509 certificate subject organizational unit (OU) field.
81            'OU' => $this->l10n->t('Name of an organizational unit'),
82            default => null,
83        };
84    }
85}