Passed
Push — master ( c790c5...9a63b6 )
by Tom
01:42
created

IndexParser::parseElementItems()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace NoaaCapAlerts\Parser;
4
5
class IndexParser
6
{
7
    protected $xmlParser;
8
9 3
    function __construct(XmlParser $xmlParser = null)
10
    {
11 3
        if ($xmlParser === null) {
12 2
            $this->xmlParser = new XmlParser();
13
        } else {
14 1
            $this->xmlParser = $xmlParser;
15
        }
16 3
    }
17
18 3
    public function parse(string $xml): array
19
    {
20
        // parse XML into an array of alerts
21 3
        $rawDataArray = $this->xmlParser->getArrayFromXml($xml);
22 2
        $alertDataArray = $rawDataArray[0]['children'];
23
24
        // Process each alert ("ENTRY")
25 2
        $resultArray = array();
26
27 2
        foreach ($alertDataArray as $alert) {
28 2
            if (!$this->isAlert($alert)) {
29 2
                continue;
30
            }
31
32 2
            $parsedAlert = $this->parseAlert($alert);
33
34 2
            $resultArray[] = $parsedAlert;
35
        }
36
37 2
        return $resultArray;
38
    }
39
40 2
    protected function isAlert(array $alert): bool
41
    {
42 2
        return isset($alert['name']) && $alert['name'] == 'ENTRY';
43
    }
44
45 2
    protected function parseAlert(array $alert): array
46
    {
47 2
        $parsedAlert = $this->getDefaultAlertValues();
48
49
        // Loop through attributes and set values
50 2
        foreach ($alert['children'] as $element) {
51 2
            list($elementName, $elementAttrs, $elementData) = $this->parseElementItems($element);
52
53
            switch ($elementName) {
54 2
                case 'ID':
55 2
                    $parsedAlert['idString'] = $elementData;
56 2
                    break;
57 2
                case 'UPDATED':
58 2
                    $parsedAlert['updatedDateTime'] = new \DateTime($elementData);
59 2
                    $parsedAlert['updatedTime'] = $parsedAlert['updatedDateTime']->format('Y-m-d H:i:s');
60 2
                    break;
61 2
                case 'PUBLISHED':
62 2
                    $parsedAlert['publishedDateTime'] = new \DateTime($elementData);
63 2
                    $parsedAlert['publishedTime'] = $parsedAlert['publishedDateTime']->format('Y-m-d H:i:s');
64 2
                    break;
65 2
                case 'AUTHOR':
66 2
                    $parsedAlert['authorName'] = $element['children'][0]['tagData'];
67 2
                    break;
68 2
                case 'TITLE':
69 2
                    $parsedAlert['title'] = $elementData;
70 2
                    break;
71 2
                case 'LINK':
72 2
                    $parsedAlert['link'] = $elementAttrs['HREF'];
73 2
                    break;
74 2
                case 'SUMMARY':
75 2
                    $parsedAlert['summary'] = $elementData;
76 2
                    break;
77 2
                case 'CAP:EVENT':
78 2
                    $parsedAlert['capEvent'] = $elementData;
79 2
                    break;
80 2
                case 'CAP:EFFECTIVE':
81 2
                    $effectiveDateTime = new \DateTime($elementData);
82 2
                    $parsedAlert['capEffectiveTime'] = $effectiveDateTime->format('Y-m-d H:i:s');
83 2
                    $parsedAlert['capEffectiveDateTime'] = $effectiveDateTime;
84 2
                    break;
85 2
                case 'CAP:EXPIRES':
86 2
                    $expiresDateTime = new \DateTime($elementData);
87 2
                    $parsedAlert['capExpiresTime'] = $expiresDateTime->format('Y-m-d H:i:s');
88 2
                    $parsedAlert['capExpiresDateTime'] = $expiresDateTime;
89 2
                    break;
90 2
                case 'CAP:STATUS':
91 2
                    $parsedAlert['capStatus'] = $elementData;
92 2
                    break;
93 2
                case 'CAP:MSGTYPE':
94 2
                    $parsedAlert['capMsgType'] = $elementData;
95 2
                    break;
96 2
                case 'CAP:CATEGORY':
97 2
                    $parsedAlert['capCategory'] = $elementData;
98 2
                    break;
99 2
                case 'CAP:URGENCY':
100 2
                    $parsedAlert['capUrgencyExpected'] = $elementData;
101 2
                    break;
102 2
                case 'CAP:SEVERITY':
103 2
                    $parsedAlert['capSeverity'] = $elementData;
104 2
                    break;
105 2
                case 'CAP:CERTAINTY':
106 2
                    $parsedAlert['capCertainty'] = $elementData;
107 2
                    break;
108 2
                case 'CAP:AREADESC':
109 2
                    $parsedAlert['capAreaDesc'] = $elementData;
110 2
                    break;
111 2
                case 'CAP:POLYGON':
112 2
                    $capPolygonString = $elementData;
113 2
                    $parsedAlert['capPolygon'] = explode(' ', $capPolygonString);
114 2
                    break;
115 2
                case 'CAP:GEOCODE':
116 2
                    $geoArray = $this->parseGeoArray($element);
117 2
                    $cleanGeoArray = $this->formatGeoArray($geoArray);
118 2
                    $parsedAlert['capGeoString'] = implode(', ', $geoArray);
119 2
                    $parsedAlert['capGeo'] = $cleanGeoArray;
120 2
                    break;
121 2
                case 'CAP:PARAMETER':
122 2
                    $vtec = $this->parseVtec($element);
123 2
                    $parsedAlert['vtec'] = $vtec;
124 2
                    break;
125
            }
126
127 2
            $parsedAlert['idKey'] = $this->generateIdKey($parsedAlert['idString']);
128
129
        }
130
131 2
        return $parsedAlert;
132
    }
133
134 2
    protected function formatGeoArray(array $geoArray): array
135
    {
136
        // organize array by format type
137
        $locationFormatTypes = array(
138 2
            'FIPS6',
139
            'UGC',
140
        );
141
142 2
        $currentLocationKey = 'null';
143 2
        $geoLocArray = array();
144
145 2
        foreach ($geoArray as $geoLoc) {
146 2
            if (in_array($geoLoc, $locationFormatTypes)) {
147 2
                $currentLocationKey = $geoLoc;
148 2
                $geoLocArray[$geoLoc] = array();
149
            } else {
150 2
                $geoLocArray[$currentLocationKey] = explode(' ', $geoLoc);
151
            }
152
        }
153
154 2
        return $geoLocArray;
155
    }
156
157 2
    protected function generateIdKey(string $idString): string
158
    {
159
        // idString contains important data in it as well.
160
        // Use it to generate a unique key for the alert.
161
        //
162
        // Example:
163
        //    alerts.weather.gov/cap/wwacapget.php?x=AK12539092A414.WinterWeatherAdvisory.125390A09AB0AK.AFGWSWNSB.a59f94b5da45867f6f45272a36df61cc
164
        //
165
        // The pieces of idString appears to be
166
        // 0. State abrev + some strange timestamp format
167
        // 1. Type
168
        // 2. Another timestamp with state abrev.
169
        // 3. ??
170
        // 4. Hash of some data (32 bit)
171
        //
172
        // Since 0,1,2, and 3 aren't unique on their own, but it looks like 4 is, I'll plan on using 0 and 4 just to be sure.
173
174 2
        $idParts = explode('=', $idString);
175 2
        $idSplit = explode('.', $idParts[1]);
176 2
        $idKey = $idSplit[0] . '.' . $idSplit[4];
177
178 2
        return $idKey;
179
    }
180
181
    /**
182
     * @return array
183
     */
184 2
    protected function getDefaultAlertValues(): array
185
    {
186
        $parsedAlert = array(
187 2
            'idString' => '',
188
            'idKey' => '',
189
            'updatedDateTime' => null,
190
            'publishedDateTime' => null,
191
            'updatedTime' => '',
192
            'publishedTime' => '',
193
            'authorName' => '',
194
            'title' => '',
195
            'link' => '',
196
            'summary' => '',
197
            'capEvent' => '',
198
            'capEffectiveTime' => '',
199
            'capExpiresTime' => '',
200
            'capEffectiveDateTime' => null,
201
            'capExpiresDateTime' => null,
202
            'capStatus' => '',
203
            'capMsgType' => '',
204
            'capCategory' => '',
205
            'capUrgencyExpected' => '',
206
            'capSeverity' => '',
207
            'capCertainty' => '',
208
            'capAreaDesc' => '',
209
            'capPolygon' => '',
210
            'capGeo' => array(),
211
            'capGeoString' => '',
212
            'vtec' => '',
213
        );
214 2
        return $parsedAlert;
215
    }
216
217 2
    protected function parseGeoArray(array $element): array
218
    {
219 2
        $geoArray = array();
220
221 2
        foreach ($element['children'] as $geo) {
222 2
            if (isset($geo['tagData'])) {
223 2
                $geoArray[] = $geo['tagData'];
224
            }
225
        }
226
227 2
        return $geoArray;
228
    }
229
230 2
    protected function parseVtec(array $element): string
231
    {
232 2
        if (isset($element['children'][1]['tagData'])) {
233 2
            return $element['children'][1]['tagData'];
234
        }
235
236 1
        return "";
237
    }
238
239 2
    protected function parseElementItems($element): array
240
    {
241 2
        $elementName = $element['name'];
242 2
        $elementAttrs = $element['attrs'];
243 2
        if (isset($element['tagData'])) {
244 2
            $elementData = $element['tagData'];
245
        } else {
246 2
            $elementData = '';
247
        }
248 2
        return array($elementName, $elementAttrs, $elementData);
249
    }
250
251
}
252