1
0

DefinitionParameter.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Lib;
  8. /**
  9. * Parameter for an external storage definition
  10. */
  11. class DefinitionParameter implements \JsonSerializable {
  12. // placeholder value for password fields, when the client updates a storage configuration
  13. // placeholder values are ignored and the field is left unmodified
  14. public const UNMODIFIED_PLACEHOLDER = '__unmodified__';
  15. /** Value constants */
  16. public const VALUE_TEXT = 0;
  17. public const VALUE_BOOLEAN = 1;
  18. public const VALUE_PASSWORD = 2;
  19. public const VALUE_HIDDEN = 3;
  20. /** Flag constants */
  21. public const FLAG_NONE = 0;
  22. public const FLAG_OPTIONAL = 1;
  23. public const FLAG_USER_PROVIDED = 2;
  24. /** @var string name of parameter */
  25. private string $name;
  26. /** @var string human-readable parameter text */
  27. private string $text;
  28. /** @var string human-readable parameter tooltip */
  29. private string $tooltip = '';
  30. /** @var int value type, see self::VALUE_* constants */
  31. private int $type = self::VALUE_TEXT;
  32. /** @var int flags, see self::FLAG_* constants */
  33. private int $flags = self::FLAG_NONE;
  34. /** @var mixed */
  35. private $defaultValue;
  36. /**
  37. * @param string $name parameter name
  38. * @param string $text parameter description
  39. * @param mixed $defaultValue default value
  40. */
  41. public function __construct(string $name, string $text, $defaultValue = null) {
  42. $this->name = $name;
  43. $this->text = $text;
  44. $this->defaultValue = $defaultValue;
  45. }
  46. /**
  47. * @return string
  48. */
  49. public function getName(): string {
  50. return $this->name;
  51. }
  52. /**
  53. * @return string
  54. */
  55. public function getText(): string {
  56. return $this->text;
  57. }
  58. /**
  59. * Get value type
  60. *
  61. * @return int
  62. */
  63. public function getType(): int {
  64. return $this->type;
  65. }
  66. /**
  67. * Set value type
  68. *
  69. * @param int $type
  70. * @return self
  71. */
  72. public function setType(int $type) {
  73. $this->type = $type;
  74. return $this;
  75. }
  76. /**
  77. * @return mixed default value
  78. */
  79. public function getDefaultValue() {
  80. return $this->defaultValue;
  81. }
  82. /**
  83. * @param mixed $defaultValue default value
  84. * @return self
  85. */
  86. public function setDefaultValue($defaultValue) {
  87. $this->defaultValue = $defaultValue;
  88. return $this;
  89. }
  90. /**
  91. * @return string
  92. */
  93. public function getTypeName(): string {
  94. switch ($this->type) {
  95. case self::VALUE_BOOLEAN:
  96. return 'boolean';
  97. case self::VALUE_TEXT:
  98. return 'text';
  99. case self::VALUE_PASSWORD:
  100. return 'password';
  101. default:
  102. return 'unknown';
  103. }
  104. }
  105. /**
  106. * @return int
  107. */
  108. public function getFlags(): int {
  109. return $this->flags;
  110. }
  111. /**
  112. * @param int $flags
  113. * @return self
  114. */
  115. public function setFlags(int $flags) {
  116. $this->flags = $flags;
  117. return $this;
  118. }
  119. /**
  120. * @param int $flag
  121. * @return self
  122. */
  123. public function setFlag(int $flag) {
  124. $this->flags |= $flag;
  125. return $this;
  126. }
  127. /**
  128. * @param int $flag
  129. * @return bool
  130. */
  131. public function isFlagSet(int $flag): bool {
  132. return (bool)($this->flags & $flag);
  133. }
  134. /**
  135. * @return string
  136. */
  137. public function getTooltip(): string {
  138. return $this->tooltip;
  139. }
  140. /**
  141. * @param string $tooltip
  142. * @return self
  143. */
  144. public function setTooltip(string $tooltip) {
  145. $this->tooltip = $tooltip;
  146. return $this;
  147. }
  148. /**
  149. * Serialize into JSON for client-side JS
  150. */
  151. public function jsonSerialize(): array {
  152. $result = [
  153. 'value' => $this->getText(),
  154. 'flags' => $this->getFlags(),
  155. 'type' => $this->getType(),
  156. 'tooltip' => $this->getTooltip(),
  157. ];
  158. $defaultValue = $this->getDefaultValue();
  159. if ($defaultValue) {
  160. $result['defaultValue'] = $defaultValue;
  161. }
  162. return $result;
  163. }
  164. public function isOptional(): bool {
  165. return $this->isFlagSet(self::FLAG_OPTIONAL) || $this->isFlagSet(self::FLAG_USER_PROVIDED);
  166. }
  167. /**
  168. * Validate a parameter value against this
  169. * Convert type as necessary
  170. *
  171. * @param mixed $value Value to check
  172. * @return bool success
  173. */
  174. public function validateValue(&$value): bool {
  175. switch ($this->getType()) {
  176. case self::VALUE_BOOLEAN:
  177. if (!is_bool($value)) {
  178. switch ($value) {
  179. case 'true':
  180. $value = true;
  181. break;
  182. case 'false':
  183. $value = false;
  184. break;
  185. default:
  186. return false;
  187. }
  188. }
  189. break;
  190. default:
  191. if (!$value && !$this->isOptional()) {
  192. return false;
  193. }
  194. break;
  195. }
  196. return true;
  197. }
  198. }