You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IdentifierTrait.php 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. /**
  23. * Trait for objects requiring an identifier (and/or identifier aliases)
  24. * Also supports deprecation to a different object, linking the objects
  25. */
  26. trait IdentifierTrait {
  27. /** @var string */
  28. protected $identifier;
  29. /** @var string[] */
  30. protected $identifierAliases = [];
  31. /** @var IdentifierTrait */
  32. protected $deprecateTo = null;
  33. /**
  34. * @return string
  35. */
  36. public function getIdentifier() {
  37. return $this->identifier;
  38. }
  39. /**
  40. * @param string $identifier
  41. * @return self
  42. */
  43. public function setIdentifier($identifier) {
  44. $this->identifier = $identifier;
  45. $this->identifierAliases[] = $identifier;
  46. return $this;
  47. }
  48. /**
  49. * @return string[]
  50. */
  51. public function getIdentifierAliases() {
  52. return $this->identifierAliases;
  53. }
  54. /**
  55. * @param string $alias
  56. * @return self
  57. */
  58. public function addIdentifierAlias($alias) {
  59. $this->identifierAliases[] = $alias;
  60. return $this;
  61. }
  62. /**
  63. * @return object|null
  64. */
  65. public function getDeprecateTo() {
  66. return $this->deprecateTo;
  67. }
  68. /**
  69. * @param object $destinationObject
  70. * @return self
  71. */
  72. public function deprecateTo($destinationObject) {
  73. $this->deprecateTo = $destinationObject;
  74. return $this;
  75. }
  76. /**
  77. * @return array
  78. */
  79. public function jsonSerializeIdentifier() {
  80. $data = [
  81. 'identifier' => $this->identifier,
  82. 'identifierAliases' => $this->identifierAliases,
  83. ];
  84. if ($this->deprecateTo) {
  85. $data['deprecateTo'] = $this->deprecateTo->getIdentifier();
  86. }
  87. return $data;
  88. }
  89. }