| Code Coverage | ||||||||||
| Lines | Functions and Methods | Classes and Traits | ||||||||
| Total |  | 100.00% | 12 / 12 |  | 100.00% | 7 / 7 | CRAP |  | 100.00% | 1 / 1 | 
| ResourceRelationCollection |  | 100.00% | 12 / 12 |  | 100.00% | 7 / 7 | 9 |  | 100.00% | 1 / 1 | 
| __construct |  | 100.00% | 2 / 2 |  | 100.00% | 1 / 1 | 2 | |||
| getIterator |  | 100.00% | 1 / 1 |  | 100.00% | 1 / 1 | 1 | |||
| add |  | 100.00% | 2 / 2 |  | 100.00% | 1 / 1 | 1 | |||
| filterNewSourceIds |  | 100.00% | 1 / 1 |  | 100.00% | 1 / 1 | 1 | |||
| filterRemovedSourceIds |  | 100.00% | 1 / 1 |  | 100.00% | 1 / 1 | 1 | |||
| jsonSerialize |  | 100.00% | 1 / 1 |  | 100.00% | 1 / 1 | 1 | |||
| getSourceIds |  | 100.00% | 4 / 4 |  | 100.00% | 1 / 1 | 2 | |||
| 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 (original work) Open Assessment Technologies SA; | 
| 19 | */ | 
| 20 | |
| 21 | declare(strict_types=1); | 
| 22 | |
| 23 | namespace oat\tao\model\resources\relation; | 
| 24 | |
| 25 | use ArrayIterator; | 
| 26 | use IteratorAggregate; | 
| 27 | use JsonSerializable; | 
| 28 | |
| 29 | class ResourceRelationCollection implements IteratorAggregate, JsonSerializable | 
| 30 | { | 
| 31 | /** @var ResourceRelation[] */ | 
| 32 | private $relations = []; | 
| 33 | |
| 34 | public function __construct(ResourceRelation ...$relations) | 
| 35 | { | 
| 36 | foreach ($relations as $relation) { | 
| 37 | $this->add($relation); | 
| 38 | } | 
| 39 | } | 
| 40 | |
| 41 | public function getIterator(): ArrayIterator | 
| 42 | { | 
| 43 | return new ArrayIterator($this->relations); | 
| 44 | } | 
| 45 | |
| 46 | public function add(ResourceRelation $relation): self | 
| 47 | { | 
| 48 | $this->relations[] = $relation; | 
| 49 | |
| 50 | return $this; | 
| 51 | } | 
| 52 | |
| 53 | public function filterNewSourceIds(array $currentSourceIds): array | 
| 54 | { | 
| 55 | return array_diff($currentSourceIds, $this->getSourceIds()); | 
| 56 | } | 
| 57 | |
| 58 | public function filterRemovedSourceIds(array $currentSourceIds): array | 
| 59 | { | 
| 60 | return array_diff($this->getSourceIds(), $currentSourceIds); | 
| 61 | } | 
| 62 | |
| 63 | public function jsonSerialize(): array | 
| 64 | { | 
| 65 | return $this->relations; | 
| 66 | } | 
| 67 | |
| 68 | private function getSourceIds(): array | 
| 69 | { | 
| 70 | $sourceIds = []; | 
| 71 | |
| 72 | foreach ($this->relations as $relation) { | 
| 73 | $sourceIds[] = $relation->getSourceId(); | 
| 74 | } | 
| 75 | |
| 76 | return $sourceIds; | 
| 77 | } | 
| 78 | } |