DefinitionParameter.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 human-readable parameter tooltip */
  25. private string $tooltip = '';
  26. /** @var int value type, see self::VALUE_* constants */
  27. private int $type = self::VALUE_TEXT;
  28. /** @var int flags, see self::FLAG_* constants */
  29. private int $flags = self::FLAG_NONE;
  30. /**
  31. * @param string $name parameter name
  32. * @param string $text parameter description
  33. * @param mixed $defaultValue default value
  34. */
  35. public function __construct(
  36. private string $name,
  37. private string $text,
  38. private $defaultValue = null,
  39. ) {
  40. }
  41. /**
  42. * @return string
  43. */
  44. public function getName(): string {
  45. return $this->name;
  46. }
  47. /**
  48. * @return string
  49. */
  50. public function getText(): string {
  51. return $this->text;
  52. }
  53. /**
  54. * Get value type
  55. *
  56. * @return int
  57. */
  58. public function getType(): int {
  59. return $this->type;
  60. }
  61. /**
  62. * Set value type
  63. *
  64. * @param int $type
  65. * @return self
  66. */
  67. public function setType(int $type) {
  68. $this->type = $type;
  69. return $this;
  70. }
  71. /**
  72. * @return mixed default value
  73. */
  74. public function getDefaultValue() {
  75. return $this->defaultValue;
  76. }
  77. /**
  78. * @param mixed $defaultValue default value
  79. * @return self
  80. */
  81. public function setDefaultValue($defaultValue) {
  82. $this->defaultValue = $defaultValue;
  83. return $this;
  84. }
  85. /**
  86. * @return string
  87. */
  88. public function getTypeName(): string {
  89. switch ($this->type) {
  90. case self::VALUE_BOOLEAN:
  91. return 'boolean';
  92. case self::VALUE_TEXT:
  93. return 'text';
  94. case self::VALUE_PASSWORD:
  95. return 'password';
  96. default:
  97. return 'unknown';
  98. }
  99. }
  100. /**
  101. * @return int
  102. */
  103. public function getFlags(): int {
  104. return $this->flags;
  105. }
  106. /**
  107. * @param int $flags
  108. * @return self
  109. */
  110. public function setFlags(int $flags) {
  111. $this->flags = $flags;
  112. return $this;
  113. }
  114. /**
  115. * @param int $flag
  116. * @return self
  117. */
  118. public function setFlag(int $flag) {
  119. $this->flags |= $flag;
  120. return $this;
  121. }
  122. /**
  123. * @param int $flag
  124. * @return bool
  125. */
  126. public function isFlagSet(int $flag): bool {
  127. return (bool)($this->flags & $flag);
  128. }
  129. /**
  130. * @return string
  131. */
  132. public function getTooltip(): string {
  133. return $this->tooltip;
  134. }
  135. /**
  136. * @param string $tooltip
  137. * @return self
  138. */
  139. public function setTooltip(string $tooltip) {
  140. $this->tooltip = $tooltip;
  141. return $this;
  142. }
  143. /**
  144. * Serialize into JSON for client-side JS
  145. */
  146. public function jsonSerialize(): array {
  147. $result = [
  148. 'value' => $this->getText(),
  149. 'flags' => $this->getFlags(),
  150. 'type' => $this->getType(),
  151. 'tooltip' => $this->getTooltip(),
  152. ];
  153. $defaultValue = $this->getDefaultValue();
  154. if ($defaultValue) {
  155. $result['defaultValue'] = $defaultValue;
  156. }
  157. return $result;
  158. }
  159. public function isOptional(): bool {
  160. return $this->isFlagSet(self::FLAG_OPTIONAL) || $this->isFlagSet(self::FLAG_USER_PROVIDED);
  161. }
  162. /**
  163. * Validate a parameter value against this
  164. * Convert type as necessary
  165. *
  166. * @param mixed $value Value to check
  167. * @return bool success
  168. */
  169. public function validateValue(&$value): bool {
  170. switch ($this->getType()) {
  171. case self::VALUE_BOOLEAN:
  172. if (!is_bool($value)) {
  173. switch ($value) {
  174. case 'true':
  175. $value = true;
  176. break;
  177. case 'false':
  178. $value = false;
  179. break;
  180. default:
  181. return false;
  182. }
  183. }
  184. break;
  185. default:
  186. if (!$value && !$this->isOptional()) {
  187. return false;
  188. }
  189. break;
  190. }
  191. return true;
  192. }
  193. }