FrontendDefinitionTrait.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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. * Trait for objects that have a frontend representation
  27. */
  28. trait FrontendDefinitionTrait {
  29. /** @var string human-readable mechanism name */
  30. private $text;
  31. /** @var DefinitionParameter[] parameters for mechanism */
  32. private $parameters = [];
  33. /** @var string[] custom JS */
  34. private $customJs = [];
  35. /**
  36. * @return string
  37. */
  38. public function getText() {
  39. return $this->text;
  40. }
  41. /**
  42. * @param string $text
  43. * @return $this
  44. */
  45. public function setText($text) {
  46. $this->text = $text;
  47. return $this;
  48. }
  49. /**
  50. * @param FrontendDefinitionTrait $a
  51. * @param FrontendDefinitionTrait $b
  52. * @return int
  53. */
  54. public static function lexicalCompare(FrontendDefinitionTrait $a, FrontendDefinitionTrait $b) {
  55. return strcmp($a->getText(), $b->getText());
  56. }
  57. /**
  58. * @return DefinitionParameter[]
  59. */
  60. public function getParameters() {
  61. return $this->parameters;
  62. }
  63. /**
  64. * @param DefinitionParameter[] $parameters
  65. * @return self
  66. */
  67. public function addParameters(array $parameters) {
  68. foreach ($parameters as $parameter) {
  69. $this->addParameter($parameter);
  70. }
  71. return $this;
  72. }
  73. /**
  74. * @param DefinitionParameter $parameter
  75. * @return self
  76. */
  77. public function addParameter(DefinitionParameter $parameter) {
  78. $this->parameters[$parameter->getName()] = $parameter;
  79. return $this;
  80. }
  81. /**
  82. * @return string[]
  83. */
  84. public function getCustomJs() {
  85. return $this->customJs;
  86. }
  87. /**
  88. * @param string $custom
  89. * @return self
  90. */
  91. public function addCustomJs($custom) {
  92. $this->customJs[] = $custom;
  93. return $this;
  94. }
  95. /**
  96. * @param string $custom
  97. * @return self
  98. * @deprecated 9.1.0, use addCustomJs() instead
  99. */
  100. public function setCustomJs($custom) {
  101. $this->customJs = [$custom];
  102. return $this;
  103. }
  104. /**
  105. * Serialize into JSON for client-side JS
  106. *
  107. * @return array
  108. */
  109. public function jsonSerializeDefinition() {
  110. $configuration = [];
  111. foreach ($this->getParameters() as $parameter) {
  112. $configuration[$parameter->getName()] = $parameter;
  113. }
  114. $data = [
  115. 'name' => $this->getText(),
  116. 'configuration' => $configuration,
  117. 'custom' => $this->getCustomJs(),
  118. ];
  119. return $data;
  120. }
  121. /**
  122. * Check if parameters are satisfied in a StorageConfig
  123. *
  124. * @param StorageConfig $storage
  125. * @return bool
  126. */
  127. public function validateStorageDefinition(StorageConfig $storage) {
  128. foreach ($this->getParameters() as $name => $parameter) {
  129. $value = $storage->getBackendOption($name);
  130. if (!is_null($value) || !$parameter->isOptional()) {
  131. if (!$parameter->validateValue($value)) {
  132. return false;
  133. }
  134. $storage->setBackendOption($name, $value);
  135. }
  136. }
  137. return true;
  138. }
  139. }