FrontendDefinitionTrait.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Lib;
  8. /**
  9. * Trait for objects that have a frontend representation
  10. */
  11. trait FrontendDefinitionTrait {
  12. /** @var string human-readable mechanism name */
  13. private string $text = "";
  14. /** @var array<string, DefinitionParameter> parameters for mechanism */
  15. private array $parameters = [];
  16. /** @var string[] custom JS */
  17. private array $customJs = [];
  18. public function getText(): string {
  19. return $this->text;
  20. }
  21. public function setText(string $text): self {
  22. $this->text = $text;
  23. return $this;
  24. }
  25. public static function lexicalCompare(IFrontendDefinition $a, IFrontendDefinition $b): int {
  26. return strcmp($a->getText(), $b->getText());
  27. }
  28. /**
  29. * @return array<string, DefinitionParameter>
  30. */
  31. public function getParameters(): array {
  32. return $this->parameters;
  33. }
  34. /**
  35. * @param list<DefinitionParameter> $parameters
  36. */
  37. public function addParameters(array $parameters): self {
  38. foreach ($parameters as $parameter) {
  39. $this->addParameter($parameter);
  40. }
  41. return $this;
  42. }
  43. public function addParameter(DefinitionParameter $parameter): self {
  44. $this->parameters[$parameter->getName()] = $parameter;
  45. return $this;
  46. }
  47. /**
  48. * @return string[]
  49. */
  50. public function getCustomJs(): array {
  51. return $this->customJs;
  52. }
  53. /**
  54. * @param string $custom
  55. * @return self
  56. */
  57. public function addCustomJs(string $custom): self {
  58. $this->customJs[] = $custom;
  59. return $this;
  60. }
  61. /**
  62. * Serialize into JSON for client-side JS
  63. */
  64. public function jsonSerializeDefinition(): array {
  65. $configuration = [];
  66. foreach ($this->getParameters() as $parameter) {
  67. $configuration[$parameter->getName()] = $parameter;
  68. }
  69. $data = [
  70. 'name' => $this->getText(),
  71. 'configuration' => $configuration,
  72. 'custom' => $this->getCustomJs(),
  73. ];
  74. return $data;
  75. }
  76. /**
  77. * Check if parameters are satisfied in a StorageConfig
  78. */
  79. public function validateStorageDefinition(StorageConfig $storage): bool {
  80. foreach ($this->getParameters() as $name => $parameter) {
  81. $value = $storage->getBackendOption($name);
  82. if (!is_null($value) || !$parameter->isOptional()) {
  83. if (!$parameter->validateValue($value)) {
  84. return false;
  85. }
  86. $storage->setBackendOption($name, $value);
  87. }
  88. }
  89. return true;
  90. }
  91. }