Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
86.54% |
45 / 52 |
|
75.00% |
12 / 16 |
CRAP | |
0.00% |
0 / 1 |
| ValueCollection | |
86.54% |
45 / 52 |
|
75.00% |
12 / 16 |
32.20 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getUri | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| removeValueByUri | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| addValue | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| extractValueByUri | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
3.14 | |||
| hasDuplicates | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDuplicatedValues | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
7 | |||
| getListUris | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getUris | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getIterator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| jsonSerialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| count | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getTotalCount | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setTotalCount | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| ensureValueProperties | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| createNewValueLabel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2020-2024 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * @author Sergei Mikhailov <sergei.mikhailov@taotesting.com> |
| 21 | */ |
| 22 | |
| 23 | declare(strict_types=1); |
| 24 | |
| 25 | namespace oat\tao\model\Lists\Business\Domain; |
| 26 | |
| 27 | use Countable; |
| 28 | use Traversable; |
| 29 | use JsonSerializable; |
| 30 | use IteratorAggregate; |
| 31 | |
| 32 | class ValueCollection implements IteratorAggregate, JsonSerializable, Countable |
| 33 | { |
| 34 | /** @var string|null */ |
| 35 | private $uri; |
| 36 | |
| 37 | /** @var Value[] */ |
| 38 | private $values = []; |
| 39 | |
| 40 | private $totalCount = 0; |
| 41 | |
| 42 | public function __construct(string $uri = null, Value ...$values) |
| 43 | { |
| 44 | $this->uri = $uri; |
| 45 | |
| 46 | foreach ($values as $value) { |
| 47 | $this->addValue($value); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | public function getUri(): ?string |
| 52 | { |
| 53 | return $this->uri; |
| 54 | } |
| 55 | |
| 56 | public function removeValueByUri(string $uri): self |
| 57 | { |
| 58 | /** |
| 59 | * @var int $key |
| 60 | * @var Value $value |
| 61 | */ |
| 62 | foreach ($this->values as $key => $value) { |
| 63 | if ($value->getUri() === $uri) { |
| 64 | unset($this->values[$key]); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return $this; |
| 69 | } |
| 70 | |
| 71 | public function addValue(Value $value): self |
| 72 | { |
| 73 | $value = $this->ensureValueProperties($value); |
| 74 | $this->values[] = $value; |
| 75 | |
| 76 | return $this; |
| 77 | } |
| 78 | |
| 79 | public function extractValueByUri(string $uri): ?Value |
| 80 | { |
| 81 | foreach ($this->values as $value) { |
| 82 | if ($value->getUri() === $uri) { |
| 83 | return $value; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return null; |
| 88 | } |
| 89 | |
| 90 | public function hasDuplicates(): bool |
| 91 | { |
| 92 | return $this->getDuplicatedValues(1)->count() > 0; |
| 93 | } |
| 94 | |
| 95 | public function getDuplicatedValues(int $limit = null): ValueCollection |
| 96 | { |
| 97 | $duplicates = new ValueCollection(); |
| 98 | $counter = 0; |
| 99 | $visited = []; |
| 100 | |
| 101 | foreach ($this->values as $value) { |
| 102 | if (empty($value->getUri())) { |
| 103 | continue; |
| 104 | } |
| 105 | |
| 106 | if (!isset($visited[$value->getUri()]) || $visited[$value->getUri()] === $value) { |
| 107 | $visited[$value->getUri()] = $value; |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | $duplicates->addValue($value); |
| 112 | |
| 113 | $counter++; |
| 114 | if ($limit !== null && $counter >= $limit) { |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return $duplicates; |
| 120 | } |
| 121 | |
| 122 | public function getListUris(): array |
| 123 | { |
| 124 | $ids = []; |
| 125 | |
| 126 | foreach ($this->values as $value) { |
| 127 | $ids[] = $value->getListUri(); |
| 128 | } |
| 129 | |
| 130 | return $ids; |
| 131 | } |
| 132 | |
| 133 | public function getUris(): array |
| 134 | { |
| 135 | $ids = []; |
| 136 | |
| 137 | foreach ($this->values as $value) { |
| 138 | $ids[] = $value->getUri(); |
| 139 | } |
| 140 | |
| 141 | return $ids; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * @return Value[]|Traversable |
| 146 | */ |
| 147 | public function getIterator(): Traversable |
| 148 | { |
| 149 | yield from array_values($this->values); |
| 150 | } |
| 151 | |
| 152 | public function jsonSerialize(): array |
| 153 | { |
| 154 | return array_values($this->values); |
| 155 | } |
| 156 | |
| 157 | public function count(): int |
| 158 | { |
| 159 | return count($this->values); |
| 160 | } |
| 161 | |
| 162 | public function getTotalCount(): int |
| 163 | { |
| 164 | return $this->totalCount; |
| 165 | } |
| 166 | |
| 167 | public function setTotalCount(int $totalCount): void |
| 168 | { |
| 169 | $this->totalCount = $totalCount; |
| 170 | } |
| 171 | |
| 172 | private function ensureValueProperties(Value $value): Value |
| 173 | { |
| 174 | if ($value->getLabel() !== '') { |
| 175 | return $value; |
| 176 | } |
| 177 | |
| 178 | return new Value( |
| 179 | $value->getId(), |
| 180 | $value->getUri(), |
| 181 | $this->createNewValueLabel(), |
| 182 | $value->getDependencyUri() |
| 183 | ); |
| 184 | } |
| 185 | |
| 186 | private function createNewValueLabel(): string |
| 187 | { |
| 188 | return sprintf('%s %u', __('Element'), count($this) + 1); |
| 189 | } |
| 190 | } |