Account.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Accounts;
  27. use Generator;
  28. use OCP\Accounts\IAccount;
  29. use OCP\Accounts\IAccountProperty;
  30. use OCP\Accounts\IAccountPropertyCollection;
  31. use OCP\Accounts\PropertyDoesNotExistException;
  32. use OCP\IUser;
  33. use RuntimeException;
  34. class Account implements IAccount {
  35. use TAccountsHelper;
  36. /** @var IAccountPropertyCollection[]|IAccountProperty[] */
  37. private array $properties = [];
  38. public function __construct(
  39. private IUser $user,
  40. ) {
  41. }
  42. public function setProperty(string $property, string $value, string $scope, string $verified, string $verificationData = ''): IAccount {
  43. if ($this->isCollection($property)) {
  44. throw new \InvalidArgumentException('setProperty cannot set an IAccountsPropertyCollection');
  45. }
  46. $this->properties[$property] = new AccountProperty($property, $value, $scope, $verified, $verificationData);
  47. return $this;
  48. }
  49. public function getProperty(string $property): IAccountProperty {
  50. if ($this->isCollection($property)) {
  51. throw new \InvalidArgumentException('getProperty cannot retrieve an IAccountsPropertyCollection');
  52. }
  53. if (!array_key_exists($property, $this->properties) || !$this->properties[$property] instanceof IAccountProperty) {
  54. throw new PropertyDoesNotExistException($property);
  55. }
  56. return $this->properties[$property];
  57. }
  58. public function getProperties(): array {
  59. return array_filter($this->properties, function ($obj) {
  60. return $obj instanceof IAccountProperty;
  61. });
  62. }
  63. public function setAllPropertiesFromJson(array $properties): IAccount {
  64. foreach ($properties as $propertyName => $propertyObject) {
  65. if ($this->isCollection($propertyName)) {
  66. $collection = new AccountPropertyCollection($propertyName);
  67. /** @var array<int, IAccountProperty> $collectionProperties */
  68. $collectionProperties = [];
  69. /** @var array<int, array<string, string>> $propertyObject */
  70. foreach ($propertyObject as ['value' => $value, 'scope' => $scope, 'verified' => $verified, 'verificationData' => $verificationData]) {
  71. $collectionProperties[] = new AccountProperty($collection->getName(), $value, $scope, $verified, $verificationData);
  72. }
  73. $collection->setProperties($collectionProperties);
  74. $this->setPropertyCollection($collection);
  75. } else {
  76. /** @var array<string, string> $propertyObject */
  77. ['value' => $value, 'scope' => $scope, 'verified' => $verified, 'verificationData' => $verificationData] = $propertyObject;
  78. $this->setProperty($propertyName, $value, $scope, $verified, $verificationData);
  79. }
  80. }
  81. return $this;
  82. }
  83. public function getAllProperties(): Generator {
  84. foreach ($this->properties as $propertyObject) {
  85. if ($propertyObject instanceof IAccountProperty) {
  86. yield $propertyObject;
  87. } elseif ($propertyObject instanceof IAccountPropertyCollection) {
  88. foreach ($propertyObject->getProperties() as $property) {
  89. yield $property;
  90. }
  91. }
  92. }
  93. }
  94. public function getFilteredProperties(string $scope = null, string $verified = null): array {
  95. $result = $incrementals = [];
  96. /** @var IAccountProperty $obj */
  97. foreach ($this->getAllProperties() as $obj) {
  98. if ($scope !== null && $scope !== $obj->getScope()) {
  99. continue;
  100. }
  101. if ($verified !== null && $verified !== $obj->getVerified()) {
  102. continue;
  103. }
  104. $index = $obj->getName();
  105. if ($this->isCollection($index)) {
  106. $incrementals[$index] = ($incrementals[$index] ?? -1) + 1;
  107. $index .= '#' . $incrementals[$index];
  108. }
  109. $result[$index] = $obj;
  110. }
  111. return $result;
  112. }
  113. /** @return array<string, IAccountProperty|array<int, IAccountProperty>> */
  114. public function jsonSerialize(): array {
  115. $properties = $this->properties;
  116. foreach ($properties as $propertyName => $propertyObject) {
  117. if ($propertyObject instanceof IAccountPropertyCollection) {
  118. // Override collection serialization to discard duplicate name
  119. $properties[$propertyName] = $propertyObject->jsonSerialize()[$propertyName];
  120. }
  121. }
  122. return $properties;
  123. }
  124. public function getUser(): IUser {
  125. return $this->user;
  126. }
  127. public function setPropertyCollection(IAccountPropertyCollection $propertyCollection): IAccount {
  128. $this->properties[$propertyCollection->getName()] = $propertyCollection;
  129. return $this;
  130. }
  131. public function getPropertyCollection(string $propertyCollectionName): IAccountPropertyCollection {
  132. if (!$this->isCollection($propertyCollectionName)) {
  133. throw new PropertyDoesNotExistException($propertyCollectionName);
  134. }
  135. if (!array_key_exists($propertyCollectionName, $this->properties)) {
  136. $this->properties[$propertyCollectionName] = new AccountPropertyCollection($propertyCollectionName);
  137. }
  138. if (!$this->properties[$propertyCollectionName] instanceof IAccountPropertyCollection) {
  139. throw new RuntimeException('Requested collection is not an IAccountPropertyCollection');
  140. }
  141. return $this->properties[$propertyCollectionName];
  142. }
  143. }