99
1010namespace OCA \Libresign \Handler ;
1111
12+ use OCP \IL10N ;
13+
1214/**
1315 * @method self setDirection(string $value)
1416 * @method self setLinkToSite(string $value)
3133 */
3234class TemplateVariables {
3335 private array $ variables = [];
36+ private array $ variablesMetadata = [];
3437
35- /**
36- * Allowed template variable names with their expected types
37- */
38- private const ALLOWED_VARIABLES = [
39- 'direction ' => 'string ' ,
40- 'linkToSite ' => 'string ' ,
41- 'qrcode ' => 'string ' ,
42- 'qrcodeSize ' => 'integer ' ,
43- 'signedBy ' => 'string ' ,
44- 'signers ' => 'array ' ,
45- 'uuid ' => 'string ' ,
46- 'validateIn ' => 'string ' ,
47- 'validationSite ' => 'string ' ,
48- ];
38+ public function __construct (
39+ private IL10N $ l10n ,
40+ ) {
41+ $ this ->initializeVariablesMetadata ();
42+ }
43+
44+ private function initializeVariablesMetadata (): void {
45+ $ this ->variablesMetadata = [
46+ 'direction ' => [
47+ 'type ' => 'string ' ,
48+ 'description ' => $ this ->l10n ->t ('Text direction for the footer (ltr or rtl based on language) ' ),
49+ 'example ' => 'ltr ' ,
50+ ],
51+ 'linkToSite ' => [
52+ 'type ' => 'string ' ,
53+ 'description ' => $ this ->l10n ->t ('Link to LibreSign or custom website ' ),
54+ 'example ' => 'https://libresign.coop ' ,
55+ 'default ' => 'https://libresign.coop ' ,
56+ ],
57+ 'qrcode ' => [
58+ 'type ' => 'string ' ,
59+ 'description ' => $ this ->l10n ->t ('QR Code image in base64 format for document validation ' ),
60+ 'example ' => 'iVBORw0KGgoAAAANSUhEUgAA... ' ,
61+ ],
62+ 'qrcodeSize ' => [
63+ 'type ' => 'integer ' ,
64+ 'description ' => $ this ->l10n ->t ('QR Code size in pixels (includes margin) ' ),
65+ 'example ' => 108 ,
66+ ],
67+ 'signedBy ' => [
68+ 'type ' => 'string ' ,
69+ 'description ' => $ this ->l10n ->t ('Message indicating the document was digitally signed ' ),
70+ 'example ' => 'Digitally signed by LibreSign. ' ,
71+ 'default ' => $ this ->l10n ->t ('Digitally signed by LibreSign. ' ),
72+ ],
73+ 'signers ' => [
74+ 'type ' => 'array ' ,
75+ 'description ' => $ this ->l10n ->t ('Array of signers with displayName and signed timestamp ' ),
76+ 'example ' => '[{"displayName": "John Doe", "signed": "2025-01-01T10:00:00Z"}] ' ,
77+ ],
78+ 'uuid ' => [
79+ 'type ' => 'string ' ,
80+ 'description ' => $ this ->l10n ->t ('Document unique identifier (UUID format) ' ),
81+ 'example ' => 'de0a18d4-fe65-4abc-bdd1-84e819700260 ' ,
82+ ],
83+ 'validateIn ' => [
84+ 'type ' => 'string ' ,
85+ 'description ' => $ this ->l10n ->t ('Validation message template with placeholder ' ),
86+ 'example ' => 'Validate in %s. ' ,
87+ 'default ' => $ this ->l10n ->t ('Validate in %s. ' , ['%s ' ]),
88+ ],
89+ 'validationSite ' => [
90+ 'type ' => 'string ' ,
91+ 'description ' => $ this ->l10n ->t ('Complete URL for document validation with UUID ' ),
92+ 'example ' => 'https://example.com/validation/de0a18d4-fe65-4abc-bdd1-84e819700260 ' ,
93+ ],
94+ ];
95+ }
4996
5097 /**
5198 * @throws \InvalidArgumentException if trying to access non-whitelisted variable or wrong type
@@ -70,13 +117,13 @@ public function __call(string $method, array $args): mixed {
70117 }
71118
72119 private function ensureAllowed (string $ key ): void {
73- if (!array_key_exists ($ key , self :: ALLOWED_VARIABLES )) {
120+ if (!array_key_exists ($ key , $ this -> variablesMetadata )) {
74121 throw new \InvalidArgumentException ("Template variable ' {$ key }' is not allowed " );
75122 }
76123 }
77124
78125 private function ensureType (string $ key , mixed $ value ): void {
79- $ expected = self :: ALLOWED_VARIABLES [$ key ];
126+ $ expected = $ this -> variablesMetadata [$ key][ ' type ' ];
80127 $ actual = gettype ($ value );
81128
82129 if ($ actual !== $ expected ) {
@@ -92,6 +139,15 @@ public function toArray(): array {
92139 return $ this ->variables ;
93140 }
94141
142+ /**
143+ * Get metadata for all available template variables
144+ *
145+ * @return array Associative array of variable metadata (name => config)
146+ */
147+ public function getVariablesMetadata (): array {
148+ return $ this ->variablesMetadata ;
149+ }
150+
95151 /**
96152 * Merge additional variables, validating against whitelist and types
97153 *
0 commit comments