DefinitionParameter.php 4.3 KB

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