1
0

Account.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 $properties = [];
  38. /** @var IUser */
  39. private $user;
  40. public function __construct(IUser $user) {
  41. $this->user = $user;
  42. }
  43. public function setProperty(string $property, string $value, string $scope, string $verified, string $verificationData = ''): IAccount {
  44. if ($this->isCollection($property)) {
  45. throw new \InvalidArgumentException('setProperty cannot set an IAccountsPropertyCollection');
  46. }
  47. $this->properties[$property] = new AccountProperty($property, $value, $scope, $verified, $verificationData);
  48. return $this;
  49. }
  50. public function getProperty(string $property): IAccountProperty {
  51. if ($this->isCollection($property)) {
  52. throw new \InvalidArgumentException('getProperty cannot retrieve an IAccountsPropertyCollection');
  53. }
  54. if (!array_key_exists($property, $this->properties) || !$this->properties[$property] instanceof IAccountProperty) {
  55. throw new PropertyDoesNotExistException($property);
  56. }
  57. return $this->properties[$property];
  58. }
  59. public function getProperties(): array {
  60. return array_filter($this->properties, function ($obj) {
  61. return $obj instanceof IAccountProperty;
  62. });
  63. }
  64. public function setAllPropertiesFromJson(array $properties): IAccount {
  65. foreach ($properties as $propertyName => $propertyObject) {
  66. if ($this->isCollection($propertyName)) {
  67. $collection = new AccountPropertyCollection($propertyName);
  68. /** @var array<int, IAccountProperty> $collectionProperties */
  69. $collectionProperties = [];
  70. /** @var array<int, array<string, string>> $propertyObject */
  71. foreach ($propertyObject as ['value' => $value, 'scope' => $scope, 'verified' => $verified, 'verificationData' => $verificationData]) {
  72. $collectionProperties[] = new AccountProperty($collection->getName(), $value, $scope, $verified, $verificationData);
  73. }
  74. $collection->setProperties($collectionProperties);
  75. $this->setPropertyCollection($collection);
  76. } else {
  77. /** @var array<string, string> $propertyObject */
  78. ['value' => $value, 'scope' => $scope, 'verified' => $verified, 'verificationData' => $verificationData] = $propertyObject;
  79. $this->setProperty($propertyName, $value, $scope, $verified, $verificationData);
  80. }
  81. }
  82. return $this;
  83. }
  84. public function getAllProperties(): Generator {
  85. foreach ($this->properties as $propertyObject) {
  86. if ($propertyObject instanceof IAccountProperty) {
  87. yield $propertyObject;
  88. } elseif ($propertyObject instanceof IAccountPropertyCollection) {
  89. foreach ($propertyObject->getProperties() as $property) {
  90. yield $property;
  91. }
  92. }
  93. }
  94. }
  95. public function getFilteredProperties(string $scope = null, string $verified = null): array {
  96. $result = $incrementals = [];
  97. /** @var IAccountProperty $obj */
  98. foreach ($this->getAllProperties() as $obj) {
  99. if ($scope !== null && $scope !== $obj->getScope()) {
  100. continue;
  101. }
  102. if ($verified !== null && $verified !== $obj->getVerified()) {
  103. continue;
  104. }
  105. $index = $obj->getName();
  106. if ($this->isCollection($index)) {
  107. $incrementals[$index] = ($incrementals[$index] ?? -1) + 1;
  108. $index .= '#' . $incrementals[$index];
  109. }
  110. $result[$index] = $obj;
  111. }
  112. return $result;
  113. }
  114. /** @return array<string, IAccountProperty|array<int, IAccountProperty>> */
  115. public function jsonSerialize(): array {
  116. $properties = $this->properties;
  117. foreach ($properties as $propertyName => $propertyObject) {
  118. if ($propertyObject instanceof IAccountPropertyCollection) {
  119. // Override collection serialization to discard duplicate name
  120. $properties[$propertyName] = $propertyObject->jsonSerialize()[$propertyName];
  121. }
  122. }
  123. return $properties;
  124. }
  125. public function getUser(): IUser {
  126. return $this->user;
  127. }
  128. public function setPropertyCollection(IAccountPropertyCollection $propertyCollection): IAccount {
  129. $this->properties[$propertyCollection->getName()] = $propertyCollection;
  130. return $this;
  131. }
  132. public function getPropertyCollection(string $propertyCollectionName): IAccountPropertyCollection {
  133. if (!$this->isCollection($propertyCollectionName)) {
  134. throw new PropertyDoesNotExistException($propertyCollectionName);
  135. }
  136. if (!array_key_exists($propertyCollectionName, $this->properties)) {
  137. $this->properties[$propertyCollectionName] = new AccountPropertyCollection($propertyCollectionName);
  138. }
  139. if (!$this->properties[$propertyCollectionName] instanceof IAccountPropertyCollection) {
  140. throw new RuntimeException('Requested collection is not an IAccountPropertyCollection');
  141. }
  142. return $this->properties[$propertyCollectionName];
  143. }
  144. }