You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
3.7 KiB

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