Completed
Branch dev (35fed3)
by Dennis
02:04
created

WzClassification::buildLabels()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * (c) Dennis Meckel
5
 *
6
 * For the full copyright and license information,
7
 * please view the LICENSE file that was distributed with this source code.
8
 */
9
10
namespace Rayne\wz2008\Graph\Parser;
11
12
use Rayne\wz2008\Graph\WzItem;
13
use Rayne\wz2008\Graph\WzItemInterface;
14
use Rayne\wz2008\Graph\WzItemCollection;
15
use Rayne\wz2008\Graph\WzItemCollectionInterface;
16
use SimpleXMLElement;
17
18
class WzClassification
19
{
20
    /**
21
     * @var WzItemCollectionInterface
22
     */
23
    private $records;
24
25
    /**
26
     * @param SimpleXMLElement $xml
27
     */
28 12
    public function __construct(SimpleXMLElement $xml)
29
    {
30 12
        $this->records = new WzItemCollection;
31
32
        /**
33
         * @var WzItem[]|null[] $levels
34
         */
35
        $levels = [
36 12
            0 => null,
37 8
            WzItem::LEVEL_SECTION => null,
38 8
            WzItem::LEVEL_DIVISION => null,
39 8
            WzItem::LEVEL_GROUP => null,
40 8
            WzItem::LEVEL_CLASS => null,
41 8
            WzItem::LEVEL_SUBCLASS => null,
42 4
        ];
43
44 12
        foreach ($xml->xpath('//Item') as $xmlItem) {
45 12
            $itemId = (string) $xmlItem['id'];
46 12
            $itemLevel = (int) $xmlItem['idLevel'];
47
48 12
            $parent = $levels[$itemLevel - 1];
49 12
            $current = new WzItem(
50 4
                $itemId,
51 12
                $this->buildLabels($xmlItem),
52 4
                $itemLevel,
53 4
                $parent);
54
55 12
            $this->records->add($current);
56 12
            $levels[$itemLevel] = $current;
57 4
        }
58 12
    }
59
60
    /**
61
     * @param SimpleXMLElement $xmlItem
62
     * @return string[]
63
     */
64 12
    private function buildLabels(SimpleXMLElement $xmlItem)
65
    {
66 12
        $labels = [];
67
68 12
        foreach ($xmlItem->xpath('Label[@qualifier="Usual"]/LabelText') as $label) {
69 12
            $labels[strtolower($label['language'])] = (string) $label;
70 4
        }
71
72 12
        return $labels;
73
    }
74
75
    /**
76
     * @return WzItemCollectionInterface|WzItemInterface[] `ID` to `WzItem` map.
77
     */
78 12
    public function getRecords()
79
    {
80 12
        return $this->records;
81
    }
82
}
83