• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LibreSign / libresign / 19073343708

04 Nov 2025 03:10PM UTC coverage: 39.615%. First build
19073343708

Pull #5731

github

web-flow
Merge 3a7f8a8e9 into e37d1f21b
Pull Request #5731: feat: add multiple ou

34 of 100 new or added lines in 9 files covered. (34.0%)

4570 of 11536 relevant lines covered (39.62%)

2.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.35
/lib/Service/Certificate/ValidateService.php
1
<?php
2

3
declare(strict_types=1);
4
/**
5
 * SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
6
 * SPDX-License-Identifier: AGPL-3.0-or-later
7
 */
8

9
namespace OCA\Libresign\Service\Certificate;
10

11
use InvalidArgumentException;
12
use OCP\IL10N;
13

14
class ValidateService {
15

16
        public function __construct(
17
                protected RulesService $rulesService,
18
                protected IL10N $l10n,
19
        ) {
20

21
        }
32✔
22

23
        public function validate(string $fieldName, string|array $value): void {
24
                $rule = $this->rulesService->getRule($fieldName);
25✔
25
                $expectedType = $rule['type'] ?? 'string';
25✔
26

27
                if ($expectedType === 'array' && !is_array($value)) {
25✔
28
                        throw new InvalidArgumentException(
1✔
29
                                $this->l10n->t("Parameter '%s' is required!", [$fieldName])
1✔
30
                        );
1✔
31
                }
32

33
                if ($expectedType === 'string' && !is_string($value)) {
24✔
34
                        throw new InvalidArgumentException(
1✔
35
                                $this->l10n->t("Parameter '%s' is required!", [$fieldName])
1✔
36
                        );
1✔
37
                }
38

39
                if ($expectedType === 'array') {
23✔
40
                        $this->validateArray($fieldName, $value, $rule);
11✔
41
                } else {
42
                        $this->validateString($fieldName, $value, $rule);
15✔
43
                }
44
        }
45

46
        private function validateString(string $fieldName, string $value, array $rule): void {
47
                $value = trim($value);
20✔
48
                $length = strlen($value);
20✔
49
                if (!$length && isset($rule['required']) && $rule['required']) {
20✔
50
                        throw new InvalidArgumentException(
2✔
51
                                $this->l10n->t("Parameter '%s' is required!", [$fieldName])
2✔
52
                        );
2✔
53
                }
54
                if ($length > $rule['max'] || $length < $rule['min']) {
18✔
55
                        throw new InvalidArgumentException(
7✔
56
                                $this->l10n->t("Parameter '%s' should be betweeen %s and %s.", [$fieldName, $rule['min'], $rule['max']])
7✔
57
                        );
7✔
58
                }
59
        }
60

61
        private function validateArray(string $fieldName, array $values, array $rule): void {
62
                $arrayLength = count($values);
11✔
63
                if (isset($rule['minItems']) && $arrayLength < $rule['minItems']) {
11✔
NEW
64
                        throw new InvalidArgumentException(
×
NEW
65
                                $this->l10n->t("Parameter '%s' should be betweeen %s and %s.", [$fieldName . ' items', $rule['minItems'], $rule['maxItems'] ?? '∞'])
×
NEW
66
                        );
×
67
                }
68
                if (isset($rule['maxItems']) && $arrayLength > $rule['maxItems']) {
11✔
69
                        throw new InvalidArgumentException(
1✔
70
                                $this->l10n->t("Parameter '%s' should be betweeen %s and %s.", [$fieldName . ' items', $rule['minItems'] ?? 0, $rule['maxItems']])
1✔
71
                        );
1✔
72
                }
73

74
                $nonEmptyValues = array_filter($values, fn ($value) => is_string($value) && trim($value) !== '');
10✔
75

76
                if (empty($nonEmptyValues) && isset($rule['required']) && $rule['required']) {
10✔
NEW
77
                        throw new InvalidArgumentException(
×
NEW
78
                                $this->l10n->t("Parameter '%s' is required!", [$fieldName])
×
NEW
79
                        );
×
80
                }
81

82
                foreach ($values as $index => $value) {
10✔
83
                        if (!is_string($value)) {
8✔
NEW
84
                                throw new InvalidArgumentException(
×
NEW
85
                                        $this->l10n->t("Parameter '%s' is required!", [$fieldName])
×
NEW
86
                                );
×
87
                        }
88

89
                        if (trim($value) !== '') {
8✔
90
                                $this->validateString($fieldName, $value, $rule);
7✔
91
                        }
92
                }
93
        }
94

95
        public function validateNames(array $names): void {
96
                foreach ($names as $item) {
7✔
97
                        if (empty($item['id'])) {
7✔
98
                                throw new InvalidArgumentException('Parameter id is required!');
2✔
99
                        }
100

101
                        if (!isset($item['value'])) {
5✔
102
                                throw new InvalidArgumentException("Parameter 'value' is required for field '{$item['id']}'!");
1✔
103
                        }
104

105
                        $this->validate($item['id'], $item['value']);
4✔
106
                }
107
        }
108

109
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc