IdentifierTrait.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 requiring an identifier (and/or identifier aliases)
  10. * Also supports deprecation to a different object, linking the objects
  11. */
  12. trait IdentifierTrait {
  13. protected string $identifier = '';
  14. /** @var string[] */
  15. protected array $identifierAliases = [];
  16. protected ?IIdentifier $deprecateTo = null;
  17. public function getIdentifier(): string {
  18. return $this->identifier;
  19. }
  20. public function setIdentifier(string $identifier): self {
  21. $this->identifier = $identifier;
  22. $this->identifierAliases[] = $identifier;
  23. return $this;
  24. }
  25. /**
  26. * @return string[]
  27. */
  28. public function getIdentifierAliases(): array {
  29. return $this->identifierAliases;
  30. }
  31. public function addIdentifierAlias(string $alias): self {
  32. $this->identifierAliases[] = $alias;
  33. return $this;
  34. }
  35. public function getDeprecateTo(): ?IIdentifier {
  36. return $this->deprecateTo;
  37. }
  38. public function deprecateTo(IIdentifier $destinationObject): self {
  39. $this->deprecateTo = $destinationObject;
  40. return $this;
  41. }
  42. public function jsonSerializeIdentifier(): array {
  43. $data = [
  44. 'identifier' => $this->identifier,
  45. 'identifierAliases' => $this->identifierAliases,
  46. ];
  47. if ($this->deprecateTo) {
  48. $data['deprecateTo'] = $this->deprecateTo->getIdentifier();
  49. }
  50. return $data;
  51. }
  52. }