1
0

IdentifierTrait.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Robin McCorkell <robin@mccorkell.me.uk>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\Files_External\Lib;
  24. /**
  25. * Trait for objects requiring an identifier (and/or identifier aliases)
  26. * Also supports deprecation to a different object, linking the objects
  27. */
  28. trait IdentifierTrait {
  29. protected string $identifier = '';
  30. /** @var string[] */
  31. protected array $identifierAliases = [];
  32. protected ?IIdentifier $deprecateTo = null;
  33. public function getIdentifier(): string {
  34. return $this->identifier;
  35. }
  36. public function setIdentifier(string $identifier): self {
  37. $this->identifier = $identifier;
  38. $this->identifierAliases[] = $identifier;
  39. return $this;
  40. }
  41. /**
  42. * @return string[]
  43. */
  44. public function getIdentifierAliases(): array {
  45. return $this->identifierAliases;
  46. }
  47. public function addIdentifierAlias(string $alias): self {
  48. $this->identifierAliases[] = $alias;
  49. return $this;
  50. }
  51. public function getDeprecateTo(): ?IIdentifier {
  52. return $this->deprecateTo;
  53. }
  54. public function deprecateTo(IIdentifier $destinationObject): self {
  55. $this->deprecateTo = $destinationObject;
  56. return $this;
  57. }
  58. public function jsonSerializeIdentifier(): array {
  59. $data = [
  60. 'identifier' => $this->identifier,
  61. 'identifierAliases' => $this->identifierAliases,
  62. ];
  63. if ($this->deprecateTo) {
  64. $data['deprecateTo'] = $this->deprecateTo->getIdentifier();
  65. }
  66. return $data;
  67. }
  68. }