|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @copyright Copyright (c) 2018 Julius Härtl <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @author Arthur Schiwon <[email protected]> |
|
9
|
|
|
* @author Christoph Wurst <[email protected]> |
|
10
|
|
|
* @author Julius Härtl <[email protected]> |
|
11
|
|
|
* |
|
12
|
|
|
* @license GNU AGPL version 3 or any later version |
|
13
|
|
|
* |
|
14
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
15
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
16
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
17
|
|
|
* License, or (at your option) any later version. |
|
18
|
|
|
* |
|
19
|
|
|
* This program is distributed in the hope that it will be useful, |
|
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
22
|
|
|
* GNU Affero General Public License for more details. |
|
23
|
|
|
* |
|
24
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
25
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
26
|
|
|
* |
|
27
|
|
|
*/ |
|
28
|
|
|
namespace OC\Accounts; |
|
29
|
|
|
|
|
30
|
|
|
use Generator; |
|
31
|
|
|
use OCP\Accounts\IAccount; |
|
32
|
|
|
use OCP\Accounts\IAccountProperty; |
|
33
|
|
|
use OCP\Accounts\IAccountPropertyCollection; |
|
34
|
|
|
use OCP\Accounts\PropertyDoesNotExistException; |
|
35
|
|
|
use OCP\IUser; |
|
36
|
|
|
|
|
37
|
|
|
class Account implements IAccount { |
|
38
|
|
|
use TAccountsHelper; |
|
39
|
|
|
|
|
40
|
|
|
/** @var IAccountPropertyCollection[]|IAccountProperty[] */ |
|
41
|
|
|
private $properties = []; |
|
42
|
|
|
|
|
43
|
|
|
/** @var IUser */ |
|
44
|
|
|
private $user; |
|
45
|
|
|
|
|
46
|
|
|
public function __construct(IUser $user) { |
|
47
|
|
|
$this->user = $user; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function setProperty(string $property, string $value, string $scope, string $verified, string $verificationData = ''): IAccount { |
|
51
|
|
|
if ($this->isCollection($property)) { |
|
52
|
|
|
throw new \InvalidArgumentException('setProperty cannot set an IAccountsPropertyCollection'); |
|
53
|
|
|
} |
|
54
|
|
|
$this->properties[$property] = new AccountProperty($property, $value, $scope, $verified, $verificationData); |
|
55
|
|
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getProperty(string $property): IAccountProperty { |
|
59
|
|
|
if ($this->isCollection($property)) { |
|
60
|
|
|
throw new \InvalidArgumentException('getProperty cannot retrieve an IAccountsPropertyCollection'); |
|
61
|
|
|
} |
|
62
|
|
|
if (!array_key_exists($property, $this->properties) || !$this->properties[$property] instanceof IAccountProperty) { |
|
63
|
|
|
throw new PropertyDoesNotExistException($property); |
|
64
|
|
|
} |
|
65
|
|
|
return $this->properties[$property]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getProperties(): array { |
|
69
|
|
|
return array_filter($this->properties, function ($obj) { |
|
70
|
|
|
return $obj instanceof IAccountProperty; |
|
71
|
|
|
}); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function getAllProperties(): Generator { |
|
75
|
|
|
foreach ($this->properties as $propertyObject) { |
|
76
|
|
|
if ($propertyObject instanceof IAccountProperty) { |
|
77
|
|
|
yield $propertyObject; |
|
78
|
|
|
} elseif ($propertyObject instanceof IAccountPropertyCollection) { |
|
79
|
|
|
foreach ($propertyObject->getProperties() as $property) { |
|
80
|
|
|
yield $property; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getFilteredProperties(string $scope = null, string $verified = null): array { |
|
87
|
|
|
$result = $incrementals = []; |
|
88
|
|
|
/** @var IAccountProperty $obj */ |
|
89
|
|
|
foreach ($this->getAllProperties() as $obj) { |
|
90
|
|
|
if ($scope !== null && $scope !== $obj->getScope()) { |
|
91
|
|
|
continue; |
|
92
|
|
|
} |
|
93
|
|
|
if ($verified !== null && $verified !== $obj->getVerified()) { |
|
94
|
|
|
continue; |
|
95
|
|
|
} |
|
96
|
|
|
$index = $obj->getName(); |
|
97
|
|
|
if ($this->isCollection($index)) { |
|
98
|
|
|
$incrementals[$index] = ($incrementals[$index] ?? -1) + 1; |
|
99
|
|
|
$index .= '#' . $incrementals[$index]; |
|
100
|
|
|
} |
|
101
|
|
|
$result[$index] = $obj; |
|
102
|
|
|
} |
|
103
|
|
|
return $result; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function jsonSerialize() { |
|
107
|
|
|
return $this->properties; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function getUser(): IUser { |
|
111
|
|
|
return $this->user; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function setPropertyCollection(IAccountPropertyCollection $propertyCollection): IAccount { |
|
115
|
|
|
$this->properties[$propertyCollection->getName()] = $propertyCollection; |
|
116
|
|
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function getPropertyCollection(string $propertyCollection): IAccountPropertyCollection { |
|
120
|
|
|
if (!array_key_exists($propertyCollection, $this->properties)) { |
|
121
|
|
|
throw new PropertyDoesNotExistException($propertyCollection); |
|
122
|
|
|
} |
|
123
|
|
|
if (!$this->properties[$propertyCollection] instanceof IAccountPropertyCollection) { |
|
124
|
|
|
throw new \RuntimeException('Requested collection is not an IAccountPropertyCollection'); |
|
125
|
|
|
} |
|
126
|
|
|
return $this->properties[$propertyCollection]; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|