Passed
Push — master ( b3cfa1...662ab9 )
by Blizzz
14:36 queued 11s
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\IAccountProperty;
31
use OCP\Accounts\IAccountPropertyCollection;
32
33
class AccountPropertyCollection implements IAccountPropertyCollection {
34
35
	/** @var string */
36
	protected $collectionName = '';
37
38
	/** @var IAccountProperty[] */
39
	protected $properties = [];
40
41
	public function __construct(string $collectionName) {
42
		$this->collectionName = $collectionName;
43
	}
44
45
	public function setProperties(array $properties): IAccountPropertyCollection {
46
		/** @var IAccountProperty $property */
47
		$this->properties = [];
48
		foreach ($properties as $property) {
49
			$this->addProperty($property);
50
		}
51
		return $this;
52
	}
53
54
	public function getProperties(): array {
55
		return $this->properties;
56
	}
57
58
	public function addProperty(IAccountProperty $property): IAccountPropertyCollection {
59
		if ($property->getName() !== $this->collectionName) {
60
			throw new InvalidArgumentException('Provided property does not match collection name');
61
		}
62
		$this->properties[] = $property;
63
		return $this;
64
	}
65
66
	public function removeProperty(IAccountProperty $property): IAccountPropertyCollection {
67
		$ref = array_search($property, $this->properties, true);
68
		if ($ref !== false) {
69
			unset($this->properties[$ref]);
70
		}
71
		return $this;
72
	}
73
74
	public function removePropertyByValue(string $value): IAccountPropertyCollection {
75
		foreach ($this->properties as $i => $property) {
76
			if ($property->getValue() === $value) {
77
				unset($this->properties[$i]);
78
			}
79
		}
80
		return $this;
81
	}
82
83
	public function jsonSerialize() {
84
		return [$this->collectionName => $this->properties];
85
	}
86
87
	public function getName(): string {
88
		return $this->collectionName;
89
	}
90
}
91