Skip to content

Commit fa40426

Browse files
committed
Add method to set all account properties from json
Signed-off-by: Christopher Ng <chrng8@gmail.com>
1 parent 9fb419d commit fa40426

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

lib/private/Accounts/Account.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,28 @@ public function getProperties(): array {
7272
});
7373
}
7474

75+
public function setJsonProperties(array $properties): IAccount {
76+
foreach ($properties as $propertyName => $propertyObject) {
77+
if ($this->isCollection($propertyName)) {
78+
$collection = new AccountPropertyCollection($propertyName);
79+
/** @var array<int, IAccountProperty> $collectionProperties */
80+
$collectionProperties = [];
81+
/** @var array<int, array<string, string>> $propertyObject */
82+
foreach ($propertyObject as ['value' => $value, 'scope' => $scope, 'verified' => $verified, 'verificationData' => $verificationData]) {
83+
$collectionProperties[] = new AccountProperty($collection->getName(), $value, $scope, $verified, $verificationData);
84+
}
85+
$collection->setProperties($collectionProperties);
86+
$this->setPropertyCollection($collection);
87+
} else {
88+
/** @var array<string, string> $propertyObject */
89+
['value' => $value, 'scope' => $scope, 'verified' => $verified, 'verificationData' => $verificationData] = $propertyObject;
90+
$this->setProperty($propertyName, $value, $scope, $verified, $verificationData);
91+
}
92+
}
93+
94+
return $this;
95+
}
96+
7597
public function getAllProperties(): Generator {
7698
foreach ($this->properties as $propertyObject) {
7799
if ($propertyObject instanceof IAccountProperty) {

lib/public/Accounts/IAccount.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ public function getProperty(string $property): IAccountProperty;
7171
*/
7272
public function getProperties(): array;
7373

74+
/**
75+
* Set all properties of an account
76+
*
77+
* @param array<string, array<string, string>>|array<string, array<int, array<string, string>>> $properties
78+
*
79+
* @since 24.0.0
80+
*/
81+
public function setJsonProperties(array $properties): IAccount;
82+
7483
/**
7584
* Get all properties of an account. Array indices are numeric. To get
7685
* the property name, call getName() against the value.

tests/lib/Accounts/AccountTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,31 @@ public function testGetAndGetAllProperties() {
7070
$this->assertEquals(array_values($properties), \iterator_to_array($account->getAllProperties()));
7171
}
7272

73+
public function testSetJsonProperties() {
74+
$user = $this->createMock(IUser::class);
75+
$properties = [
76+
IAccountManager::PROPERTY_DISPLAYNAME => new AccountProperty(IAccountManager::PROPERTY_DISPLAYNAME, 'Steve', IAccountManager::SCOPE_FEDERATED, IAccountManager::NOT_VERIFIED, ''),
77+
IAccountManager::PROPERTY_ADDRESS => new AccountProperty(IAccountManager::PROPERTY_ADDRESS, '123 Acorn Avenue', IAccountManager::SCOPE_FEDERATED, IAccountManager::NOT_VERIFIED, ''),
78+
IAccountManager::PROPERTY_WEBSITE => new AccountProperty(IAccountManager::PROPERTY_WEBSITE, 'https://www.example.org', IAccountManager::SCOPE_FEDERATED, IAccountManager::VERIFIED, ''),
79+
IAccountManager::PROPERTY_EMAIL => new AccountProperty(IAccountManager::PROPERTY_EMAIL, 'steve@earth.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFICATION_IN_PROGRESS, ''),
80+
IAccountManager::PROPERTY_AVATAR => new AccountProperty(IAccountManager::PROPERTY_AVATAR, '', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, ''),
81+
IAccountManager::PROPERTY_PHONE => new AccountProperty(IAccountManager::PROPERTY_PHONE, '+358407991028', IAccountManager::SCOPE_LOCAL, IAccountManager::NOT_VERIFIED, ''),
82+
IAccountManager::PROPERTY_TWITTER => new AccountProperty(IAccountManager::PROPERTY_TWITTER, 'therealsteve', IAccountManager::SCOPE_PRIVATE, IAccountManager::NOT_VERIFIED, ''),
83+
IAccountManager::PROPERTY_ORGANISATION => new AccountProperty(IAccountManager::PROPERTY_ORGANISATION, 'Steve Incorporated', IAccountManager::SCOPE_FEDERATED, IAccountManager::NOT_VERIFIED, ''),
84+
IAccountManager::PROPERTY_ROLE => new AccountProperty(IAccountManager::PROPERTY_ROLE, 'Founder', IAccountManager::SCOPE_FEDERATED, IAccountManager::NOT_VERIFIED, ''),
85+
IAccountManager::PROPERTY_HEADLINE => new AccountProperty(IAccountManager::PROPERTY_HEADLINE, 'I am Steve', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, ''),
86+
IAccountManager::PROPERTY_BIOGRAPHY => new AccountProperty(IAccountManager::PROPERTY_BIOGRAPHY, 'Steve is the best', IAccountManager::SCOPE_LOCAL, IAccountManager::NOT_VERIFIED, ''),
87+
IAccountManager::PROPERTY_PROFILE_ENABLED => new AccountProperty(IAccountManager::PROPERTY_PROFILE_ENABLED, '1', IAccountManager::SCOPE_FEDERATED, IAccountManager::NOT_VERIFIED, ''),
88+
IAccountManager::COLLECTION_EMAIL => [
89+
new AccountProperty(IAccountManager::COLLECTION_EMAIL, 'steve@mars.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, ''),
90+
new AccountProperty(IAccountManager::COLLECTION_EMAIL, 'steve@neptune.com', IAccountManager::SCOPE_FEDERATED, IAccountManager::NOT_VERIFIED, ''),
91+
],
92+
];
93+
$account = new Account($user);
94+
$account->setJsonProperties(json_decode(json_encode($properties), true));
95+
$this->assertEquals($properties, $account->jsonSerialize());
96+
}
97+
7398
public function testGetFilteredProperties() {
7499
$user = $this->createMock(IUser::class);
75100
$properties = [

0 commit comments

Comments
 (0)