blob: 65f46eb4dbca61cd00bcda04e4442a238afa082c (
plain)
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
|
<?php
/**
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Files_External\Lib;
/**
* Trait for objects that have a frontend representation
*/
trait FrontendDefinitionTrait {
/** @var string human-readable mechanism name */
private string $text = "";
/** @var array<string, DefinitionParameter> parameters for mechanism */
private array $parameters = [];
/** @var string[] custom JS */
private array $customJs = [];
public function getText(): string {
return $this->text;
}
public function setText(string $text): self {
$this->text = $text;
return $this;
}
public static function lexicalCompare(IFrontendDefinition $a, IFrontendDefinition $b): int {
return strcmp($a->getText(), $b->getText());
}
/**
* @return array<string, DefinitionParameter>
*/
public function getParameters(): array {
return $this->parameters;
}
/**
* @param list<DefinitionParameter> $parameters
*/
public function addParameters(array $parameters): self {
foreach ($parameters as $parameter) {
$this->addParameter($parameter);
}
return $this;
}
public function addParameter(DefinitionParameter $parameter): self {
$this->parameters[$parameter->getName()] = $parameter;
return $this;
}
/**
* @return string[]
*/
public function getCustomJs(): array {
return $this->customJs;
}
/**
* @param string $custom
* @return self
*/
public function addCustomJs(string $custom): self {
$this->customJs[] = $custom;
return $this;
}
/**
* Serialize into JSON for client-side JS
*/
public function jsonSerializeDefinition(): array {
$configuration = [];
foreach ($this->getParameters() as $parameter) {
$configuration[$parameter->getName()] = $parameter;
}
$data = [
'name' => $this->getText(),
'configuration' => $configuration,
'custom' => $this->getCustomJs(),
];
return $data;
}
/**
* Check if parameters are satisfied in a StorageConfig
*/
public function validateStorageDefinition(StorageConfig $storage): bool {
foreach ($this->getParameters() as $name => $parameter) {
$value = $storage->getBackendOption($name);
if (!is_null($value) || !$parameter->isOptional()) {
if (!$parameter->validateValue($value)) {
return false;
}
$storage->setBackendOption($name, $value);
}
}
return true;
}
}
|