AccountProperty.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Accounts;
  8. use InvalidArgumentException;
  9. use OCP\Accounts\IAccountManager;
  10. use OCP\Accounts\IAccountProperty;
  11. class AccountProperty implements IAccountProperty {
  12. /**
  13. * @var IAccountManager::SCOPE_*
  14. */
  15. private string $scope;
  16. private string $locallyVerified = IAccountManager::NOT_VERIFIED;
  17. public function __construct(
  18. private string $name,
  19. private string $value,
  20. string $scope,
  21. private string $verified,
  22. private string $verificationData,
  23. ) {
  24. $this->setScope($scope);
  25. }
  26. public function jsonSerialize(): array {
  27. return [
  28. 'name' => $this->getName(),
  29. 'value' => $this->getValue(),
  30. 'scope' => $this->getScope(),
  31. 'verified' => $this->getVerified(),
  32. 'verificationData' => $this->getVerificationData(),
  33. ];
  34. }
  35. /**
  36. * Set the value of a property
  37. *
  38. * @since 15.0.0
  39. */
  40. public function setValue(string $value): IAccountProperty {
  41. $this->value = $value;
  42. return $this;
  43. }
  44. /**
  45. * Set the scope of a property
  46. *
  47. * @since 15.0.0
  48. */
  49. public function setScope(string $scope): IAccountProperty {
  50. $newScope = $this->mapScopeToV2($scope);
  51. if (!in_array($newScope, [
  52. IAccountManager::SCOPE_LOCAL,
  53. IAccountManager::SCOPE_FEDERATED,
  54. IAccountManager::SCOPE_PRIVATE,
  55. IAccountManager::SCOPE_PUBLISHED
  56. ])) {
  57. throw new InvalidArgumentException('Invalid scope');
  58. }
  59. $this->scope = $newScope;
  60. return $this;
  61. }
  62. /**
  63. * Set the verification status of a property
  64. *
  65. * @since 15.0.0
  66. */
  67. public function setVerified(string $verified): IAccountProperty {
  68. $this->verified = $verified;
  69. return $this;
  70. }
  71. /**
  72. * Get the name of a property
  73. *
  74. * @since 15.0.0
  75. */
  76. public function getName(): string {
  77. return $this->name;
  78. }
  79. /**
  80. * Get the value of a property
  81. *
  82. * @since 15.0.0
  83. */
  84. public function getValue(): string {
  85. return $this->value;
  86. }
  87. /**
  88. * Get the scope of a property
  89. *
  90. * @since 15.0.0
  91. */
  92. public function getScope(): string {
  93. return $this->scope;
  94. }
  95. public static function mapScopeToV2(string $scope): string {
  96. if (str_starts_with($scope, 'v2-')) {
  97. return $scope;
  98. }
  99. return match ($scope) {
  100. IAccountManager::VISIBILITY_PRIVATE, '' => IAccountManager::SCOPE_LOCAL,
  101. IAccountManager::VISIBILITY_CONTACTS_ONLY => IAccountManager::SCOPE_FEDERATED,
  102. IAccountManager::VISIBILITY_PUBLIC => IAccountManager::SCOPE_PUBLISHED,
  103. default => $scope,
  104. };
  105. }
  106. /**
  107. * Get the verification status of a property
  108. *
  109. * @since 15.0.0
  110. */
  111. public function getVerified(): string {
  112. return $this->verified;
  113. }
  114. public function setVerificationData(string $verificationData): IAccountProperty {
  115. $this->verificationData = $verificationData;
  116. return $this;
  117. }
  118. public function getVerificationData(): string {
  119. return $this->verificationData;
  120. }
  121. public function setLocallyVerified(string $verified): IAccountProperty {
  122. if (!in_array($verified, [
  123. IAccountManager::NOT_VERIFIED,
  124. IAccountManager::VERIFICATION_IN_PROGRESS,
  125. IAccountManager::VERIFIED,
  126. ])) {
  127. throw new InvalidArgumentException('Provided verification value is invalid');
  128. }
  129. $this->locallyVerified = $verified;
  130. return $this;
  131. }
  132. public function getLocallyVerified(): string {
  133. return $this->locallyVerified;
  134. }
  135. }