-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathTemplateVariables.php
More file actions
164 lines (146 loc) · 4.91 KB
/
TemplateVariables.php
File metadata and controls
164 lines (146 loc) · 4.91 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Libresign\Handler;
use OCP\IL10N;
/**
* @method self setDirection(string $value)
* @method self setLinkToSite(string $value)
* @method self setQrcode(string $value)
* @method self setQrcodeSize(int $value)
* @method self setSignedBy(string $value)
* @method self setSigners(array $value)
* @method self setUuid(string $value)
* @method self setValidateIn(string $value)
* @method self setValidationSite(string $value)
* @method string|null getDirection()
* @method string|null getLinkToSite()
* @method string|null getQrcode()
* @method int|null getQrcodeSize()
* @method string|null getSignedBy()
* @method array|null getSigners()
* @method string|null getUuid()
* @method string|null getValidateIn()
* @method string|null getValidationSite()
*/
class TemplateVariables {
private array $variables = [];
private array $variablesMetadata = [];
public function __construct(
private IL10N $l10n,
) {
$this->initializeVariablesMetadata();
}
private function initializeVariablesMetadata(): void {
$this->variablesMetadata = [
'direction' => [
'type' => 'string',
'description' => $this->l10n->t('Text direction for the footer (ltr or rtl based on language)'),
'example' => 'ltr',
],
'linkToSite' => [
'type' => 'string',
'description' => $this->l10n->t('Link to LibreSign or custom website'),
'example' => 'https://libresign.coop',
'default' => 'https://libresign.coop',
],
'qrcode' => [
'type' => 'string',
'description' => $this->l10n->t('QR Code image in base64 format for document validation'),
'example' => 'iVBORw0KGgoAAAANSUhEUgAA...',
],
'qrcodeSize' => [
'type' => 'integer',
'description' => $this->l10n->t('QR Code size in pixels (includes margin)'),
'example' => 108,
],
'signedBy' => [
'type' => 'string',
'description' => $this->l10n->t('Message indicating the document was digitally signed'),
'example' => 'Digitally signed by LibreSign.',
'default' => $this->l10n->t('Digitally signed by LibreSign.'),
],
'signers' => [
'type' => 'array',
'description' => $this->l10n->t('Array of signers with "displayName" and "signed" timestamp'),
'example' => '[{"displayName": "John Doe", "signed": "2025-01-01T10:00:00Z"}]',
],
'uuid' => [
'type' => 'string',
'description' => $this->l10n->t('Document unique identifier (UUID format)'),
'example' => 'de0a18d4-fe65-4abc-bdd1-84e819700260',
],
'validateIn' => [
'type' => 'string',
'description' => $this->l10n->t('Validation message template with placeholder'),
'example' => 'Validate in %s.',
'default' => $this->l10n->t('Validate in %s.', ['%s']),
],
'validationSite' => [
'type' => 'string',
'description' => $this->l10n->t('Complete URL for document validation with UUID'),
'example' => 'https://example.com/validation/de0a18d4-fe65-4abc-bdd1-84e819700260',
],
];
}
/**
* @throws \InvalidArgumentException if trying to access non-whitelisted variable or wrong type
*/
public function __call(string $method, array $args): mixed {
if (str_starts_with($method, 'set')) {
$key = lcfirst(substr($method, 3));
$this->ensureAllowed($key);
$this->ensureType($key, $args[0]);
$this->variables[$key] = $args[0];
return $this;
}
if (str_starts_with($method, 'get')) {
$key = lcfirst(substr($method, 3));
$this->ensureAllowed($key);
return $this->variables[$key] ?? null;
}
throw new \BadMethodCallException("Method {$method} does not exist");
}
private function ensureAllowed(string $key): void {
if (!array_key_exists($key, $this->variablesMetadata)) {
throw new \InvalidArgumentException("Template variable '{$key}' is not allowed");
}
}
private function ensureType(string $key, mixed $value): void {
$expected = $this->variablesMetadata[$key]['type'];
$actual = gettype($value);
if ($actual !== $expected) {
throw new \InvalidArgumentException("Template variable '{$key}' must be of type {$expected}, got {$actual}");
}
}
public function has(string $key): bool {
return isset($this->variables[$key]);
}
public function toArray(): array {
return $this->variables;
}
/**
* Get metadata for all available template variables
*
* @return array Associative array of variable metadata (name => config)
*/
public function getVariablesMetadata(): array {
return $this->variablesMetadata;
}
/**
* Merge additional variables, validating against whitelist and types
*
* @throws \InvalidArgumentException if trying to merge non-whitelisted variable or wrong type
*/
public function merge(array $variables): self {
foreach ($variables as $key => $value) {
$this->ensureAllowed($key);
$this->ensureType($key, $value);
}
$this->variables = array_merge($this->variables, $variables);
return $this;
}
}