frontenddefinitiontrait.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * @author Robin McCorkell <robin@mccorkell.me.uk>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OCA\Files_External\Lib;
  22. use \OCA\Files_External\Lib\DefinitionParameter;
  23. use \OCA\Files_External\Lib\StorageConfig;
  24. /**
  25. * Trait for objects that have a frontend representation
  26. */
  27. trait FrontendDefinitionTrait {
  28. /** @var string human-readable mechanism name */
  29. private $text;
  30. /** @var DefinitionParameter[] parameters for mechanism */
  31. private $parameters = [];
  32. /** @var string|null custom JS */
  33. private $customJs = null;
  34. /**
  35. * @return string
  36. */
  37. public function getText() {
  38. return $this->text;
  39. }
  40. /**
  41. * @param string $text
  42. * @return self
  43. */
  44. public function setText($text) {
  45. $this->text = $text;
  46. return $this;
  47. }
  48. /**
  49. * @param FrontendDefinitionTrait $a
  50. * @param FrontendDefinitionTrait $b
  51. * @return int
  52. */
  53. public static function lexicalCompare(FrontendDefinitionTrait $a, FrontendDefinitionTrait $b) {
  54. return strcmp($a->getText(), $b->getText());
  55. }
  56. /**
  57. * @return DefinitionParameter[]
  58. */
  59. public function getParameters() {
  60. return $this->parameters;
  61. }
  62. /**
  63. * @param DefinitionParameter[] $parameters
  64. * @return self
  65. */
  66. public function addParameters(array $parameters) {
  67. foreach ($parameters as $parameter) {
  68. $this->addParameter($parameter);
  69. }
  70. return $this;
  71. }
  72. /**
  73. * @param DefinitionParameter $parameter
  74. * @return self
  75. */
  76. public function addParameter(DefinitionParameter $parameter) {
  77. $this->parameters[$parameter->getName()] = $parameter;
  78. return $this;
  79. }
  80. /**
  81. * @return string|null
  82. */
  83. public function getCustomJs() {
  84. return $this->customJs;
  85. }
  86. /**
  87. * @param string $custom
  88. * @return self
  89. */
  90. public function setCustomJs($custom) {
  91. $this->customJs = $custom;
  92. return $this;
  93. }
  94. /**
  95. * Serialize into JSON for client-side JS
  96. *
  97. * @return array
  98. */
  99. public function jsonSerializeDefinition() {
  100. $configuration = [];
  101. foreach ($this->getParameters() as $parameter) {
  102. $configuration[$parameter->getName()] = $parameter;
  103. }
  104. $data = [
  105. 'name' => $this->getText(),
  106. 'configuration' => $configuration,
  107. ];
  108. if (isset($this->customJs)) {
  109. $data['custom'] = $this->getCustomJs();
  110. }
  111. return $data;
  112. }
  113. /**
  114. * Check if parameters are satisfied in a StorageConfig
  115. *
  116. * @param StorageConfig $storage
  117. * @return bool
  118. */
  119. public function validateStorageDefinition(StorageConfig $storage) {
  120. foreach ($this->getParameters() as $name => $parameter) {
  121. $value = $storage->getBackendOption($name);
  122. if (!is_null($value) || !$parameter->isOptional()) {
  123. if (!$parameter->validateValue($value)) {
  124. return false;
  125. }
  126. $storage->setBackendOption($name, $value);
  127. }
  128. }
  129. return true;
  130. }
  131. }