FrontendDefinitionTrait.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 string $text = "";
  31. /** @var array<string, DefinitionParameter> parameters for mechanism */
  32. private array $parameters = [];
  33. /** @var string[] custom JS */
  34. private array $customJs = [];
  35. public function getText(): string {
  36. return $this->text;
  37. }
  38. public function setText(string $text): self {
  39. $this->text = $text;
  40. return $this;
  41. }
  42. public static function lexicalCompare(IFrontendDefinition $a, IFrontendDefinition $b): int {
  43. return strcmp($a->getText(), $b->getText());
  44. }
  45. /**
  46. * @return array<string, DefinitionParameter>
  47. */
  48. public function getParameters(): array {
  49. return $this->parameters;
  50. }
  51. /**
  52. * @param list<DefinitionParameter> $parameters
  53. */
  54. public function addParameters(array $parameters): self {
  55. foreach ($parameters as $parameter) {
  56. $this->addParameter($parameter);
  57. }
  58. return $this;
  59. }
  60. public function addParameter(DefinitionParameter $parameter): self {
  61. $this->parameters[$parameter->getName()] = $parameter;
  62. return $this;
  63. }
  64. /**
  65. * @return string[]
  66. */
  67. public function getCustomJs(): array {
  68. return $this->customJs;
  69. }
  70. /**
  71. * @param string $custom
  72. * @return self
  73. */
  74. public function addCustomJs(string $custom): self {
  75. $this->customJs[] = $custom;
  76. return $this;
  77. }
  78. /**
  79. * Serialize into JSON for client-side JS
  80. */
  81. public function jsonSerializeDefinition(): array {
  82. $configuration = [];
  83. foreach ($this->getParameters() as $parameter) {
  84. $configuration[$parameter->getName()] = $parameter;
  85. }
  86. $data = [
  87. 'name' => $this->getText(),
  88. 'configuration' => $configuration,
  89. 'custom' => $this->getCustomJs(),
  90. ];
  91. return $data;
  92. }
  93. /**
  94. * Check if parameters are satisfied in a StorageConfig
  95. */
  96. public function validateStorageDefinition(StorageConfig $storage): bool {
  97. foreach ($this->getParameters() as $name => $parameter) {
  98. $value = $storage->getBackendOption($name);
  99. if (!is_null($value) || !$parameter->isOptional()) {
  100. if (!$parameter->validateValue($value)) {
  101. return false;
  102. }
  103. $storage->setBackendOption($name, $value);
  104. }
  105. }
  106. return true;
  107. }
  108. }