|
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\Factory; |
|
11
|
|
|
|
|
12
|
|
|
use InvalidArgumentException; |
|
13
|
|
|
use Rayne\wz2008\Graph\WzClassification; |
|
14
|
|
|
use Rayne\wz2008\Graph\WzClassificationInterface; |
|
15
|
|
|
use Rayne\wz2008\Graph\WzItem; |
|
16
|
|
|
use Rayne\wz2008\Graph\WzItemInterface; |
|
17
|
|
|
use SimpleXMLElement; |
|
18
|
|
|
|
|
19
|
|
|
class WzClassificationFactory |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @return WzClassificationInterface The classification based upon the included `/assets/WZ2008*xml` file. |
|
23
|
|
|
* @throws InvalidArgumentException Unreadable or invalid file. |
|
24
|
|
|
*/ |
|
25
|
15 |
|
public static function build() |
|
26
|
|
|
{ |
|
27
|
15 |
|
return self::buildFromFile( |
|
28
|
15 |
|
__DIR__ . '/../../assets/WZ2008-2016-07-29-Classification_(complete).xml'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $xmlFile |
|
33
|
|
|
* @return WzClassificationInterface |
|
34
|
|
|
* @throws InvalidArgumentException Unreadable or invalid file. |
|
35
|
|
|
*/ |
|
36
|
24 |
|
public static function buildFromFile($xmlFile) |
|
37
|
|
|
{ |
|
38
|
24 |
|
$xml = @simplexml_load_file($xmlFile); |
|
39
|
|
|
|
|
40
|
24 |
|
if (!$xml) { |
|
41
|
9 |
|
throw new InvalidArgumentException( |
|
42
|
9 |
|
sprintf("Invalid XML file `%s`.", $xmlFile)); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
15 |
|
return self::buildFromXml($xml); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param SimpleXMLElement $xml |
|
50
|
|
|
* @return WzClassificationInterface |
|
51
|
|
|
*/ |
|
52
|
15 |
|
public static function buildFromXml(SimpleXMLElement $xml) |
|
53
|
|
|
{ |
|
54
|
15 |
|
return new WzClassification(self::buildItems($xml)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param SimpleXMLElement $xml |
|
59
|
|
|
* @return WzItemInterface[] |
|
60
|
|
|
*/ |
|
61
|
15 |
|
private static function buildItems(SimpleXMLElement $xml) |
|
62
|
|
|
{ |
|
63
|
15 |
|
$items = []; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var WzItemInterface[]|null[] $levels |
|
67
|
|
|
*/ |
|
68
|
|
|
$levels = [ |
|
69
|
15 |
|
0 => null, |
|
70
|
10 |
|
WzItemInterface::LEVEL_SECTION => null, |
|
71
|
10 |
|
WzItemInterface::LEVEL_DIVISION => null, |
|
72
|
10 |
|
WzItemInterface::LEVEL_GROUP => null, |
|
73
|
10 |
|
WzItemInterface::LEVEL_CLASS => null, |
|
74
|
10 |
|
WzItemInterface::LEVEL_SUBCLASS => null, |
|
75
|
5 |
|
]; |
|
76
|
|
|
|
|
77
|
15 |
|
foreach ($xml->xpath('//Item') as $xmlItem) { |
|
78
|
15 |
|
$itemId = (string)$xmlItem['id']; |
|
79
|
15 |
|
$itemLevel = (int)$xmlItem['idLevel']; |
|
80
|
|
|
|
|
81
|
15 |
|
$parent = $levels[$itemLevel - 1]; |
|
82
|
15 |
|
$current = new WzItem( |
|
83
|
5 |
|
$itemId, |
|
84
|
15 |
|
self::buildLabels($xmlItem), |
|
85
|
5 |
|
$itemLevel, |
|
86
|
5 |
|
$parent); |
|
87
|
|
|
|
|
88
|
15 |
|
$items[] = $current; |
|
89
|
15 |
|
$levels[$itemLevel] = $current; |
|
90
|
5 |
|
} |
|
91
|
|
|
|
|
92
|
15 |
|
return $items; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param SimpleXMLElement $xmlItem |
|
97
|
|
|
* @return string[] |
|
98
|
|
|
*/ |
|
99
|
15 |
|
private static function buildLabels(SimpleXMLElement $xmlItem) |
|
100
|
|
|
{ |
|
101
|
15 |
|
$labels = []; |
|
102
|
|
|
|
|
103
|
15 |
|
foreach ($xmlItem->xpath('Label[@qualifier="Usual"]/LabelText') as $label) { |
|
104
|
15 |
|
$labels[strtolower($label['language'])] = (string)$label; |
|
105
|
5 |
|
} |
|
106
|
|
|
|
|
107
|
15 |
|
return $labels; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|