From 2d9d0702f00410c5868482c0017fc93a06eaef90 Mon Sep 17 00:00:00 2001 From: Christopher Ng Date: Tue, 8 Mar 2022 00:49:00 +0000 Subject: Add method to set all account properties from json Signed-off-by: Christopher Ng --- lib/private/Accounts/Account.php | 22 ++++++++++++++++++++++ lib/public/Accounts/IAccount.php | 9 +++++++++ 2 files changed, 31 insertions(+) (limited to 'lib') diff --git a/lib/private/Accounts/Account.php b/lib/private/Accounts/Account.php index 7d36af561ce..d3287b219d0 100644 --- a/lib/private/Accounts/Account.php +++ b/lib/private/Accounts/Account.php @@ -72,6 +72,28 @@ class Account implements IAccount { }); } + public function setAllPropertiesFromJson(array $properties): IAccount { + foreach ($properties as $propertyName => $propertyObject) { + if ($this->isCollection($propertyName)) { + $collection = new AccountPropertyCollection($propertyName); + /** @var array $collectionProperties */ + $collectionProperties = []; + /** @var array> $propertyObject */ + foreach ($propertyObject as ['value' => $value, 'scope' => $scope, 'verified' => $verified, 'verificationData' => $verificationData]) { + $collectionProperties[] = new AccountProperty($collection->getName(), $value, $scope, $verified, $verificationData); + } + $collection->setProperties($collectionProperties); + $this->setPropertyCollection($collection); + } else { + /** @var array $propertyObject */ + ['value' => $value, 'scope' => $scope, 'verified' => $verified, 'verificationData' => $verificationData] = $propertyObject; + $this->setProperty($propertyName, $value, $scope, $verified, $verificationData); + } + } + + return $this; + } + public function getAllProperties(): Generator { foreach ($this->properties as $propertyObject) { if ($propertyObject instanceof IAccountProperty) { diff --git a/lib/public/Accounts/IAccount.php b/lib/public/Accounts/IAccount.php index 8d4d8b5c023..dcec17f9363 100644 --- a/lib/public/Accounts/IAccount.php +++ b/lib/public/Accounts/IAccount.php @@ -71,6 +71,15 @@ interface IAccount extends \JsonSerializable { */ public function getProperties(): array; + /** + * Set all properties of an account + * + * @param array>|array>> $properties + * + * @since 24.0.0 + */ + public function setAllPropertiesFromJson(array $properties): IAccount; + /** * Get all properties of an account. Array indices are numeric. To get * the property name, call getName() against the value. -- cgit v1.2.3