-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathValidateService.php
More file actions
48 lines (39 loc) · 1.16 KB
/
ValidateService.php
File metadata and controls
48 lines (39 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Libresign\Service\Certificate;
use InvalidArgumentException;
use OCP\IL10N;
class ValidateService {
public function __construct(
protected RulesService $rulesService,
protected IL10N $l10n,
) {
}
public function validate(string $fieldName, string $value):void {
$rule = $this->rulesService->getRule($fieldName);
$value = trim($value);
$length = strlen($value);
if (!$length && isset($rule['required']) && $rule['required']) {
throw new InvalidArgumentException(
$this->l10n->t("Parameter '%s' is required!", [$fieldName])
);
}
if ($length > $rule['max'] || $length < $rule['min']) {
throw new InvalidArgumentException(
$this->l10n->t("Parameter '%s' should be betweeen %s and %s.", [$fieldName, $rule['min'], $rule['max']])
);
}
}
public function validateNames(array $names) {
foreach ($names as $item) {
if (empty($item['id'])) {
throw new InvalidArgumentException('Parameter id is required!');
}
$this->validate($item['id'], $item['value']);
}
}
}