thtroyer /
noaa-cap-alerts
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace NoaaCapAlerts\Parser; |
||||||
| 4 | |||||||
| 5 | use NoaaCapAlerts\Exceptions\XmlParseException; |
||||||
| 6 | |||||||
| 7 | class XmlParser |
||||||
| 8 | { |
||||||
| 9 | |||||||
| 10 | protected array $output; |
||||||
| 11 | |||||||
| 12 | 7 | public function __construct() |
|||||
| 13 | { |
||||||
| 14 | 7 | $this->output = array(); |
|||||
| 15 | 7 | } |
|||||
| 16 | |||||||
| 17 | 7 | /** |
|||||
| 18 | * @throws XmlParseException |
||||||
| 19 | 7 | */ |
|||||
| 20 | 7 | public function getArrayFromXml($xml): array |
|||||
| 21 | 7 | { |
|||||
| 22 | 7 | $xmlParser = xml_parser_create(); |
|||||
| 23 | xml_set_object($xmlParser, $this); |
||||||
| 24 | 7 | xml_set_element_handler($xmlParser, "tagOpen", "tagClosed"); |
|||||
| 25 | xml_set_character_data_handler($xmlParser, "tagData"); |
||||||
| 26 | 7 | ||||||
| 27 | 2 | $successfulParse = xml_parse($xmlParser, $xml, true); |
|||||
| 28 | 2 | ||||||
| 29 | if ($successfulParse === 0) { |
||||||
| 30 | 2 | $errorString = xml_error_string(xml_get_error_code($xmlParser)); |
|||||
| 31 | $errorLine = xml_get_current_line_number($xmlParser); |
||||||
| 32 | |||||||
| 33 | 5 | throw new XmlParseException("Error parsing XML: {$errorString} at line {$errorLine}."); |
|||||
| 34 | } |
||||||
| 35 | 5 | ||||||
| 36 | xml_parser_free($xmlParser); |
||||||
| 37 | |||||||
| 38 | 5 | return $this->output; |
|||||
| 39 | } |
||||||
| 40 | |||||||
| 41 | 5 | protected function tagOpen($parser, $name, $attrs) |
|||||
|
0 ignored issues
–
show
|
|||||||
| 42 | 5 | { |
|||||
| 43 | $tag = array( |
||||||
| 44 | "name" => $name, |
||||||
| 45 | 5 | "attrs" => $attrs |
|||||
| 46 | 5 | ); |
|||||
| 47 | |||||||
| 48 | 5 | $this->output[] = $tag; |
|||||
| 49 | } |
||||||
| 50 | 5 | ||||||
| 51 | protected function tagClosed($parser, $name) |
||||||
|
0 ignored issues
–
show
The parameter
$name is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
The parameter
$parser is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||
| 52 | 5 | { |
|||||
| 53 | 5 | $this->output[count($this->output) - 2]['children'][] = $this->output[count($this->output) - 1]; |
|||||
| 54 | |||||||
| 55 | 5 | array_pop($this->output); |
|||||
| 56 | } |
||||||
| 57 | 5 | ||||||
| 58 | protected function tagData($parser, $tagData) |
||||||
|
0 ignored issues
–
show
The parameter
$parser is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||||
| 59 | 5 | { |
|||||
| 60 | 5 | $notWhitespace = !empty(trim($tagData)); |
|||||
| 61 | |||||||
| 62 | 5 | if ($notWhitespace) { |
|||||
| 63 | $this->output[count($this->output) - 1]['tagData'] = $tagData; |
||||||
| 64 | } |
||||||
| 65 | } |
||||||
| 66 | } |
||||||
| 67 | |||||||
| 68 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.