1
0

AccountPropertyCollection.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 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. use OCP\Accounts\IAccountPropertyCollection;
  12. class AccountPropertyCollection implements IAccountPropertyCollection {
  13. /** @var IAccountProperty[] */
  14. protected array $properties = [];
  15. public function __construct(
  16. protected string $collectionName,
  17. ) {
  18. }
  19. public function setProperties(array $properties): IAccountPropertyCollection {
  20. /** @var IAccountProperty $property */
  21. $this->properties = [];
  22. foreach ($properties as $property) {
  23. $this->addProperty($property);
  24. }
  25. return $this;
  26. }
  27. public function getProperties(): array {
  28. return $this->properties;
  29. }
  30. public function addProperty(IAccountProperty $property): IAccountPropertyCollection {
  31. if ($property->getName() !== $this->collectionName) {
  32. throw new InvalidArgumentException('Provided property does not match collection name');
  33. }
  34. $this->properties[] = $property;
  35. return $this;
  36. }
  37. public function addPropertyWithDefaults(string $value): IAccountPropertyCollection {
  38. $property = new AccountProperty(
  39. $this->collectionName,
  40. $value,
  41. IAccountManager::SCOPE_LOCAL,
  42. IAccountManager::NOT_VERIFIED,
  43. ''
  44. );
  45. $this->addProperty($property);
  46. return $this;
  47. }
  48. public function removeProperty(IAccountProperty $property): IAccountPropertyCollection {
  49. $ref = array_search($property, $this->properties, true);
  50. if ($ref !== false) {
  51. unset($this->properties[$ref]);
  52. }
  53. return $this;
  54. }
  55. public function getPropertyByValue(string $value): ?IAccountProperty {
  56. foreach ($this->properties as $i => $property) {
  57. if ($property->getValue() === $value) {
  58. return $property;
  59. }
  60. }
  61. return null;
  62. }
  63. public function removePropertyByValue(string $value): IAccountPropertyCollection {
  64. foreach ($this->properties as $i => $property) {
  65. if ($property->getValue() === $value) {
  66. unset($this->properties[$i]);
  67. }
  68. }
  69. return $this;
  70. }
  71. public function jsonSerialize(): array {
  72. return [$this->collectionName => $this->properties];
  73. }
  74. public function getName(): string {
  75. return $this->collectionName;
  76. }
  77. }