diff options
author | Arthur Schiwon <blizzz@arthur-schiwon.de> | 2021-06-03 19:53:18 +0200 |
---|---|---|
committer | Arthur Schiwon <blizzz@arthur-schiwon.de> | 2021-06-03 20:49:49 +0200 |
commit | 44827e37c0a2e9f69feae6a741a223bf19e49685 (patch) | |
tree | e9992e8ae8d85c5a0ad344207bc9d80fa0bd5539 /lib/private/Accounts | |
parent | 0bade2747902b22002203c31e7e13f36256f42bc (diff) | |
download | nextcloud-server-44827e37c0a2e9f69feae6a741a223bf19e49685.tar.gz nextcloud-server-44827e37c0a2e9f69feae6a741a223bf19e49685.zip |
allow interacting with IAccountPropertyCollections
- in fact the API could be done in a nicer way and it might be possible to
work without IAccountPropertyCollection, but only with the
IAccountProperties.
- To keep it simple at first and not overengineer the blunt attempt is
followed
- If necessary helpful in the further cause of development adjustements or
extensions can be done quickly with this base
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
Diffstat (limited to 'lib/private/Accounts')
-rw-r--r-- | lib/private/Accounts/Account.php | 58 | ||||
-rw-r--r-- | lib/private/Accounts/AccountManager.php | 9 | ||||
-rw-r--r-- | lib/private/Accounts/AccountPropertyCollection.php | 4 | ||||
-rw-r--r-- | lib/private/Accounts/TAccountsHelper.php | 41 |
4 files changed, 98 insertions, 14 deletions
diff --git a/lib/private/Accounts/Account.php b/lib/private/Accounts/Account.php index 109691641fd..ff9b78dae40 100644 --- a/lib/private/Accounts/Account.php +++ b/lib/private/Accounts/Account.php @@ -27,12 +27,15 @@ declare(strict_types=1); namespace OC\Accounts; +use Generator; use OCP\Accounts\IAccount; use OCP\Accounts\IAccountProperty; +use OCP\Accounts\IAccountPropertyCollection; use OCP\Accounts\PropertyDoesNotExistException; use OCP\IUser; class Account implements IAccount { + use TAccountsHelper; /** @var IAccountProperty[] */ private $properties = []; @@ -45,11 +48,17 @@ class Account implements IAccount { } public function setProperty(string $property, string $value, string $scope, string $verified, string $verificationData = ''): IAccount { + if ($this->isCollection($property)) { + throw new \InvalidArgumentException('setProperty cannot set an IAccountsPropertyCollection'); + } $this->properties[$property] = new AccountProperty($property, $value, $scope, $verified, $verificationData); return $this; } public function getProperty(string $property): IAccountProperty { + if ($this->isCollection($property)) { + throw new \InvalidArgumentException('getProperty cannot retrieve an IAccountsPropertyCollection'); + } if (!array_key_exists($property, $this->properties)) { throw new PropertyDoesNotExistException($property); } @@ -57,19 +66,41 @@ class Account implements IAccount { } public function getProperties(): array { - return $this->properties; + return array_filter($this->properties, function ($obj) { + return $obj instanceof IAccountProperty; + }); + } + + public function getAllProperties(): Generator { + foreach ($this->properties as $propertyObject) { + if ($propertyObject instanceof IAccountProperty) { + yield $propertyObject; + } else if ($propertyObject instanceof IAccountPropertyCollection) { + foreach ($propertyObject->getProperties() as $property) { + yield $property; + } + } + } } public function getFilteredProperties(string $scope = null, string $verified = null): array { - return \array_filter($this->properties, function (IAccountProperty $obj) use ($scope, $verified) { + $result = $incrementals = []; + /** @var IAccountProperty $obj */ + foreach ($this->getAllProperties() as $obj) { if ($scope !== null && $scope !== $obj->getScope()) { - return false; + continue; } if ($verified !== null && $verified !== $obj->getVerified()) { - return false; + continue; } - return true; - }); + $index = $obj->getName(); + if ($this->isCollection($index)) { + $incrementals[$index] = ($incrementals[$index] ?? -1) + 1; + $index .= '#' . $incrementals[$index]; + } + $result[$index] = $obj; + } + return $result; } public function jsonSerialize() { @@ -79,4 +110,19 @@ class Account implements IAccount { public function getUser(): IUser { return $this->user; } + + public function setPropertyCollection(IAccountPropertyCollection $propertyCollection): IAccount { + $this->properties[$propertyCollection->getName()] = $propertyCollection; + return $this; + } + + public function getPropertyCollection(string $propertyCollection): IAccountPropertyCollection { + if (!array_key_exists($propertyCollection, $this->properties)) { + throw new PropertyDoesNotExistException($propertyCollection); + } + if (!$this->properties[$propertyCollection] instanceof IAccountPropertyCollection) { + throw new \RuntimeException('Requested collection is not an IAccountPropertyCollection'); + } + return $this->properties[$propertyCollection]; + } } diff --git a/lib/private/Accounts/AccountManager.php b/lib/private/Accounts/AccountManager.php index 3241ad27c6a..3b3117d8be1 100644 --- a/lib/private/Accounts/AccountManager.php +++ b/lib/private/Accounts/AccountManager.php @@ -58,6 +58,7 @@ use function json_last_error; * @package OC\Accounts */ class AccountManager implements IAccountManager { + use TAccountsHelper; /** @var IDBConnection database connection */ private $connection; @@ -607,12 +608,4 @@ class AccountManager implements IAccountManager { $this->updateUser($account->getUser(), $data, true); } - - protected function isCollection(string $propertyName): bool { - return in_array($propertyName, - [ - IAccountManager::COLLECTION_EMAIL, - ] - ); - } } diff --git a/lib/private/Accounts/AccountPropertyCollection.php b/lib/private/Accounts/AccountPropertyCollection.php index 8e74d45d52d..84e10e6a507 100644 --- a/lib/private/Accounts/AccountPropertyCollection.php +++ b/lib/private/Accounts/AccountPropertyCollection.php @@ -83,4 +83,8 @@ class AccountPropertyCollection implements IAccountPropertyCollection { public function jsonSerialize() { return [$this->collectionName => $this->properties]; } + + public function getName(): string { + return $this->collectionName; + } } diff --git a/lib/private/Accounts/TAccountsHelper.php b/lib/private/Accounts/TAccountsHelper.php new file mode 100644 index 00000000000..09cc44c87c2 --- /dev/null +++ b/lib/private/Accounts/TAccountsHelper.php @@ -0,0 +1,41 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (c) 2021 Arthur Schiwon <blizzz@arthur-schiwon.de> + * + * @author Arthur Schiwon <blizzz@arthur-schiwon.de> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + * + */ + +namespace OC\Accounts; + + +use OCP\Accounts\IAccountManager; + +trait TAccountsHelper { + protected function isCollection(string $propertyName): bool { + return in_array($propertyName, + [ + IAccountManager::COLLECTION_EMAIL, + ], + true + ); + } +} |