IdentifierTrait.php 2.3 KB

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