DefinitionParameter.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Robin McCorkell <robin@mccorkell.me.uk>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Files_External\Lib;
  25. /**
  26. * Parameter for an external storage definition
  27. */
  28. class DefinitionParameter implements \JsonSerializable {
  29. // placeholder value for password fields, when the client updates a storage configuration
  30. // placeholder values are ignored and the field is left unmodified
  31. public const UNMODIFIED_PLACEHOLDER = '__unmodified__';
  32. /** Value constants */
  33. public const VALUE_TEXT = 0;
  34. public const VALUE_BOOLEAN = 1;
  35. public const VALUE_PASSWORD = 2;
  36. public const VALUE_HIDDEN = 3;
  37. /** Flag constants */
  38. public const FLAG_NONE = 0;
  39. public const FLAG_OPTIONAL = 1;
  40. public const FLAG_USER_PROVIDED = 2;
  41. /** @var string name of parameter */
  42. private string $name;
  43. /** @var string human-readable parameter text */
  44. private string $text;
  45. /** @var string human-readable parameter tooltip */
  46. private string $tooltip = '';
  47. /** @var int value type, see self::VALUE_* constants */
  48. private int $type = self::VALUE_TEXT;
  49. /** @var int flags, see self::FLAG_* constants */
  50. private int $flags = self::FLAG_NONE;
  51. /** @var mixed */
  52. private $defaultValue;
  53. /**
  54. * @param string $name parameter name
  55. * @param string $text parameter description
  56. * @param mixed $defaultValue default value
  57. */
  58. public function __construct(string $name, string $text, $defaultValue = null) {
  59. $this->name = $name;
  60. $this->text = $text;
  61. $this->defaultValue = $defaultValue;
  62. }
  63. /**
  64. * @return string
  65. */
  66. public function getName(): string {
  67. return $this->name;
  68. }
  69. /**
  70. * @return string
  71. */
  72. public function getText(): string {
  73. return $this->text;
  74. }
  75. /**
  76. * Get value type
  77. *
  78. * @return int
  79. */
  80. public function getType(): int {
  81. return $this->type;
  82. }
  83. /**
  84. * Set value type
  85. *
  86. * @param int $type
  87. * @return self
  88. */
  89. public function setType(int $type) {
  90. $this->type = $type;
  91. return $this;
  92. }
  93. /**
  94. * @return mixed default value
  95. */
  96. public function getDefaultValue() {
  97. return $this->defaultValue;
  98. }
  99. /**
  100. * @param mixed $defaultValue default value
  101. * @return self
  102. */
  103. public function setDefaultValue($defaultValue) {
  104. $this->defaultValue = $defaultValue;
  105. return $this;
  106. }
  107. /**
  108. * @return string
  109. */
  110. public function getTypeName(): string {
  111. switch ($this->type) {
  112. case self::VALUE_BOOLEAN:
  113. return 'boolean';
  114. case self::VALUE_TEXT:
  115. return 'text';
  116. case self::VALUE_PASSWORD:
  117. return 'password';
  118. default:
  119. return 'unknown';
  120. }
  121. }
  122. /**
  123. * @return int
  124. */
  125. public function getFlags(): int {
  126. return $this->flags;
  127. }
  128. /**
  129. * @param int $flags
  130. * @return self
  131. */
  132. public function setFlags(int $flags) {
  133. $this->flags = $flags;
  134. return $this;
  135. }
  136. /**
  137. * @param int $flag
  138. * @return self
  139. */
  140. public function setFlag(int $flag) {
  141. $this->flags |= $flag;
  142. return $this;
  143. }
  144. /**
  145. * @param int $flag
  146. * @return bool
  147. */
  148. public function isFlagSet(int $flag): bool {
  149. return (bool)($this->flags & $flag);
  150. }
  151. /**
  152. * @return string
  153. */
  154. public function getTooltip(): string {
  155. return $this->tooltip;
  156. }
  157. /**
  158. * @param string $tooltip
  159. * @return self
  160. */
  161. public function setTooltip(string $tooltip) {
  162. $this->tooltip = $tooltip;
  163. return $this;
  164. }
  165. /**
  166. * Serialize into JSON for client-side JS
  167. */
  168. public function jsonSerialize(): array {
  169. $result = [
  170. 'value' => $this->getText(),
  171. 'flags' => $this->getFlags(),
  172. 'type' => $this->getType(),
  173. 'tooltip' => $this->getTooltip(),
  174. ];
  175. $defaultValue = $this->getDefaultValue();
  176. if ($defaultValue) {
  177. $result['defaultValue'] = $defaultValue;
  178. }
  179. return $result;
  180. }
  181. public function isOptional(): bool {
  182. return $this->isFlagSet(self::FLAG_OPTIONAL) || $this->isFlagSet(self::FLAG_USER_PROVIDED);
  183. }
  184. /**
  185. * Validate a parameter value against this
  186. * Convert type as necessary
  187. *
  188. * @param mixed $value Value to check
  189. * @return bool success
  190. */
  191. public function validateValue(&$value): bool {
  192. switch ($this->getType()) {
  193. case self::VALUE_BOOLEAN:
  194. if (!is_bool($value)) {
  195. switch ($value) {
  196. case 'true':
  197. $value = true;
  198. break;
  199. case 'false':
  200. $value = false;
  201. break;
  202. default:
  203. return false;
  204. }
  205. }
  206. break;
  207. default:
  208. if (!$value && !$this->isOptional()) {
  209. return false;
  210. }
  211. break;
  212. }
  213. return true;
  214. }
  215. }