Passed
Push — master ( 62ff4f...9124c6 )
by John
74:39 queued 12s
created

AccountPropertyCollection::getProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2021 Arthur Schiwon <[email protected]>
7
 *
8
 * @author Arthur Schiwon <[email protected]>
9
 *
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OC\Accounts;
28
29
use InvalidArgumentException;
30
use OCP\Accounts\IAccountManager;
31
use OCP\Accounts\IAccountProperty;
32
use OCP\Accounts\IAccountPropertyCollection;
33
34
class AccountPropertyCollection implements IAccountPropertyCollection {
35
36
	/** @var string */
37
	protected $collectionName = '';
38
39
	/** @var IAccountProperty[] */
40
	protected $properties = [];
41
42
	public function __construct(string $collectionName) {
43
		$this->collectionName = $collectionName;
44
	}
45
46
	public function setProperties(array $properties): IAccountPropertyCollection {
47
		/** @var IAccountProperty $property */
48
		$this->properties = [];
49
		foreach ($properties as $property) {
50
			$this->addProperty($property);
51
		}
52
		return $this;
53
	}
54
55
	public function getProperties(): array {
56
		return $this->properties;
57
	}
58
59
	public function addProperty(IAccountProperty $property): IAccountPropertyCollection {
60
		if ($property->getName() !== $this->collectionName) {
61
			throw new InvalidArgumentException('Provided property does not match collection name');
62
		}
63
		$this->properties[] = $property;
64
		return $this;
65
	}
66
67
	public function addPropertyWithDefaults(string $value): IAccountPropertyCollection {
68
		$property = new AccountProperty(
69
			$this->collectionName,
70
			$value,
71
			IAccountManager::SCOPE_LOCAL,
72
			IAccountManager::NOT_VERIFIED,
73
			''
74
		);
75
		$this->addProperty($property);
76
		return $this;
77
	}
78
79
	public function removeProperty(IAccountProperty $property): IAccountPropertyCollection {
80
		$ref = array_search($property, $this->properties, true);
81
		if ($ref !== false) {
82
			unset($this->properties[$ref]);
83
		}
84
		return $this;
85
	}
86
87
	public function removePropertyByValue(string $value): IAccountPropertyCollection {
88
		foreach ($this->properties as $i => $property) {
89
			if ($property->getValue() === $value) {
90
				unset($this->properties[$i]);
91
			}
92
		}
93
		return $this;
94
	}
95
96
	public function jsonSerialize() {
97
		return [$this->collectionName => $this->properties];
98
	}
99
100
	public function getName(): string {
101
		return $this->collectionName;
102
	}
103
}
104