AccountPropertyCollection.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Arthur Schiwon <blizzz@arthur-schiwon.de>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Accounts;
  25. use InvalidArgumentException;
  26. use OCP\Accounts\IAccountManager;
  27. use OCP\Accounts\IAccountProperty;
  28. use OCP\Accounts\IAccountPropertyCollection;
  29. class AccountPropertyCollection implements IAccountPropertyCollection {
  30. /** @var IAccountProperty[] */
  31. protected array $properties = [];
  32. public function __construct(
  33. protected string $collectionName,
  34. ) {
  35. }
  36. public function setProperties(array $properties): IAccountPropertyCollection {
  37. /** @var IAccountProperty $property */
  38. $this->properties = [];
  39. foreach ($properties as $property) {
  40. $this->addProperty($property);
  41. }
  42. return $this;
  43. }
  44. public function getProperties(): array {
  45. return $this->properties;
  46. }
  47. public function addProperty(IAccountProperty $property): IAccountPropertyCollection {
  48. if ($property->getName() !== $this->collectionName) {
  49. throw new InvalidArgumentException('Provided property does not match collection name');
  50. }
  51. $this->properties[] = $property;
  52. return $this;
  53. }
  54. public function addPropertyWithDefaults(string $value): IAccountPropertyCollection {
  55. $property = new AccountProperty(
  56. $this->collectionName,
  57. $value,
  58. IAccountManager::SCOPE_LOCAL,
  59. IAccountManager::NOT_VERIFIED,
  60. ''
  61. );
  62. $this->addProperty($property);
  63. return $this;
  64. }
  65. public function removeProperty(IAccountProperty $property): IAccountPropertyCollection {
  66. $ref = array_search($property, $this->properties, true);
  67. if ($ref !== false) {
  68. unset($this->properties[$ref]);
  69. }
  70. return $this;
  71. }
  72. public function getPropertyByValue(string $value): ?IAccountProperty {
  73. foreach ($this->properties as $i => $property) {
  74. if ($property->getValue() === $value) {
  75. return $property;
  76. }
  77. }
  78. return null;
  79. }
  80. public function removePropertyByValue(string $value): IAccountPropertyCollection {
  81. foreach ($this->properties as $i => $property) {
  82. if ($property->getValue() === $value) {
  83. unset($this->properties[$i]);
  84. }
  85. }
  86. return $this;
  87. }
  88. public function jsonSerialize(): array {
  89. return [$this->collectionName => $this->properties];
  90. }
  91. public function getName(): string {
  92. return $this->collectionName;
  93. }
  94. }