Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.91% covered (success)
90.91%
20 / 22
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
XMLFileBooklet
90.91% covered (success)
90.91%
20 / 22
66.67% covered (warning)
66.67%
2 / 3
12.11
0.00% covered (danger)
0.00%
0 / 1
 crossValidate
80.00% covered (warning)
80.00%
8 / 10
0.00% covered (danger)
0.00%
0 / 1
4.13
 getUnitIds
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getUnitIdFromNode
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2/** @noinspection PhpUnhandledExceptionInspection */
3declare(strict_types=1);
4
5class XMLFileBooklet extends XMLFile {
6  const type = 'Booklet';
7  const canBeRelationSubject = true;
8  const canBeRelationObject = true;
9
10  public function crossValidate(WorkspaceCache $workspaceCache): void {
11    parent::crossValidate($workspaceCache);
12
13
14    foreach ($this->getUnitIds() as $unitId) {
15      $unit = $workspaceCache->getUnit($unitId);
16
17      if ($unit == null) {
18        $this->report('error', "Unit `$unitId` not found");
19        continue;
20      }
21
22      if (!$unit->isValid()) {
23        $this->report('error', "Unit `$unitId` has an error");
24        continue;
25      }
26
27      $this->addRelation(new FileRelation($unit->getType(), $unitId, FileRelationshipType::containsUnit, $unit));
28
29    }
30  }
31
32  // TODO unit-test $useAlias
33  public function getUnitIds(bool $useAlias = false): array {
34    if (!$this->isValid()) {
35      return [];
36    }
37
38    return $this->getUnitIdFromNode($this->getXml()->Units[0], $useAlias);
39  }
40
41  private function getUnitIdFromNode(SimpleXMLElement $node, bool $useAlias = false): array {
42    $unitIds = [];
43    foreach ($node->children() as $element) {
44      if ($element->getName() == 'Unit') {
45        $id = strtoupper((string) $element['id']);
46        $alias = (string) $element['alias'];
47        $unitIds[] = ($useAlias and $alias) ? $alias : $id;
48
49      } else {
50        foreach ($this->getUnitIdFromNode($element, $useAlias) as $id) {
51          $unitIds[] = $id;
52        }
53      }
54    }
55    return $unitIds;
56  }
57}