Account.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 Generator;
  9. use OCP\Accounts\IAccount;
  10. use OCP\Accounts\IAccountProperty;
  11. use OCP\Accounts\IAccountPropertyCollection;
  12. use OCP\Accounts\PropertyDoesNotExistException;
  13. use OCP\IUser;
  14. use RuntimeException;
  15. class Account implements IAccount {
  16. use TAccountsHelper;
  17. /** @var IAccountPropertyCollection[]|IAccountProperty[] */
  18. private array $properties = [];
  19. public function __construct(
  20. private IUser $user,
  21. ) {
  22. }
  23. public function setProperty(string $property, string $value, string $scope, string $verified, string $verificationData = ''): IAccount {
  24. if ($this->isCollection($property)) {
  25. throw new \InvalidArgumentException('setProperty cannot set an IAccountsPropertyCollection');
  26. }
  27. $this->properties[$property] = new AccountProperty($property, $value, $scope, $verified, $verificationData);
  28. return $this;
  29. }
  30. public function getProperty(string $property): IAccountProperty {
  31. if ($this->isCollection($property)) {
  32. throw new \InvalidArgumentException('getProperty cannot retrieve an IAccountsPropertyCollection');
  33. }
  34. if (!array_key_exists($property, $this->properties) || !$this->properties[$property] instanceof IAccountProperty) {
  35. throw new PropertyDoesNotExistException($property);
  36. }
  37. return $this->properties[$property];
  38. }
  39. public function getProperties(): array {
  40. return array_filter($this->properties, function ($obj) {
  41. return $obj instanceof IAccountProperty;
  42. });
  43. }
  44. public function setAllPropertiesFromJson(array $properties): IAccount {
  45. foreach ($properties as $propertyName => $propertyObject) {
  46. if ($this->isCollection($propertyName)) {
  47. $collection = new AccountPropertyCollection($propertyName);
  48. /** @var array<int, IAccountProperty> $collectionProperties */
  49. $collectionProperties = [];
  50. /** @var array<int, array<string, string>> $propertyObject */
  51. foreach ($propertyObject as ['value' => $value, 'scope' => $scope, 'verified' => $verified, 'verificationData' => $verificationData]) {
  52. $collectionProperties[] = new AccountProperty($collection->getName(), $value, $scope, $verified, $verificationData);
  53. }
  54. $collection->setProperties($collectionProperties);
  55. $this->setPropertyCollection($collection);
  56. } else {
  57. /** @var array<string, string> $propertyObject */
  58. ['value' => $value, 'scope' => $scope, 'verified' => $verified, 'verificationData' => $verificationData] = $propertyObject;
  59. $this->setProperty($propertyName, $value, $scope, $verified, $verificationData);
  60. }
  61. }
  62. return $this;
  63. }
  64. public function getAllProperties(): Generator {
  65. foreach ($this->properties as $propertyObject) {
  66. if ($propertyObject instanceof IAccountProperty) {
  67. yield $propertyObject;
  68. } elseif ($propertyObject instanceof IAccountPropertyCollection) {
  69. foreach ($propertyObject->getProperties() as $property) {
  70. yield $property;
  71. }
  72. }
  73. }
  74. }
  75. public function getFilteredProperties(?string $scope = null, ?string $verified = null): array {
  76. $result = $incrementals = [];
  77. /** @var IAccountProperty $obj */
  78. foreach ($this->getAllProperties() as $obj) {
  79. if ($scope !== null && $scope !== $obj->getScope()) {
  80. continue;
  81. }
  82. if ($verified !== null && $verified !== $obj->getVerified()) {
  83. continue;
  84. }
  85. $index = $obj->getName();
  86. if ($this->isCollection($index)) {
  87. $incrementals[$index] = ($incrementals[$index] ?? -1) + 1;
  88. $index .= '#' . $incrementals[$index];
  89. }
  90. $result[$index] = $obj;
  91. }
  92. return $result;
  93. }
  94. /** @return array<string, IAccountProperty|array<int, IAccountProperty>> */
  95. public function jsonSerialize(): array {
  96. $properties = $this->properties;
  97. foreach ($properties as $propertyName => $propertyObject) {
  98. if ($propertyObject instanceof IAccountPropertyCollection) {
  99. // Override collection serialization to discard duplicate name
  100. $properties[$propertyName] = $propertyObject->jsonSerialize()[$propertyName];
  101. }
  102. }
  103. return $properties;
  104. }
  105. public function getUser(): IUser {
  106. return $this->user;
  107. }
  108. public function setPropertyCollection(IAccountPropertyCollection $propertyCollection): IAccount {
  109. $this->properties[$propertyCollection->getName()] = $propertyCollection;
  110. return $this;
  111. }
  112. public function getPropertyCollection(string $propertyCollectionName): IAccountPropertyCollection {
  113. if (!$this->isCollection($propertyCollectionName)) {
  114. throw new PropertyDoesNotExistException($propertyCollectionName);
  115. }
  116. if (!array_key_exists($propertyCollectionName, $this->properties)) {
  117. $this->properties[$propertyCollectionName] = new AccountPropertyCollection($propertyCollectionName);
  118. }
  119. if (!$this->properties[$propertyCollectionName] instanceof IAccountPropertyCollection) {
  120. throw new RuntimeException('Requested collection is not an IAccountPropertyCollection');
  121. }
  122. return $this->properties[$propertyCollectionName];
  123. }
  124. }