AccountPropertyCollection.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 string */
  31. protected $collectionName = '';
  32. /** @var IAccountProperty[] */
  33. protected $properties = [];
  34. public function __construct(string $collectionName) {
  35. $this->collectionName = $collectionName;
  36. }
  37. public function setProperties(array $properties): IAccountPropertyCollection {
  38. /** @var IAccountProperty $property */
  39. $this->properties = [];
  40. foreach ($properties as $property) {
  41. $this->addProperty($property);
  42. }
  43. return $this;
  44. }
  45. public function getProperties(): array {
  46. return $this->properties;
  47. }
  48. public function addProperty(IAccountProperty $property): IAccountPropertyCollection {
  49. if ($property->getName() !== $this->collectionName) {
  50. throw new InvalidArgumentException('Provided property does not match collection name');
  51. }
  52. $this->properties[] = $property;
  53. return $this;
  54. }
  55. public function addPropertyWithDefaults(string $value): IAccountPropertyCollection {
  56. $property = new AccountProperty(
  57. $this->collectionName,
  58. $value,
  59. IAccountManager::SCOPE_LOCAL,
  60. IAccountManager::NOT_VERIFIED,
  61. ''
  62. );
  63. $this->addProperty($property);
  64. return $this;
  65. }
  66. public function removeProperty(IAccountProperty $property): IAccountPropertyCollection {
  67. $ref = array_search($property, $this->properties, true);
  68. if ($ref !== false) {
  69. unset($this->properties[$ref]);
  70. }
  71. return $this;
  72. }
  73. public function getPropertyByValue(string $value): ?IAccountProperty {
  74. foreach ($this->properties as $i => $property) {
  75. if ($property->getValue() === $value) {
  76. return $property;
  77. }
  78. }
  79. return null;
  80. }
  81. public function removePropertyByValue(string $value): IAccountPropertyCollection {
  82. foreach ($this->properties as $i => $property) {
  83. if ($property->getValue() === $value) {
  84. unset($this->properties[$i]);
  85. }
  86. }
  87. return $this;
  88. }
  89. public function jsonSerialize(): array {
  90. return [$this->collectionName => $this->properties];
  91. }
  92. public function getName(): string {
  93. return $this->collectionName;
  94. }
  95. }