xref: /webtrees/app/Report/ReportParserBase.php (revision 8f8787974040d069eb8daff5e2b4af725c6bd747)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Report;
21
22use DomainException;
23use Exception;
24use XMLParser;
25
26use function call_user_func;
27use function fclose;
28use function feof;
29use function fread;
30use function method_exists;
31use function sprintf;
32use function xml_error_string;
33use function xml_get_current_line_number;
34use function xml_get_error_code;
35use function xml_parse;
36use function xml_parser_create;
37use function xml_parser_free;
38use function xml_parser_set_option;
39use function xml_set_character_data_handler;
40use function xml_set_element_handler;
41
42use const XML_OPTION_CASE_FOLDING;
43
44/**
45 * Class ReportParserBase
46 */
47class ReportParserBase
48{
49    /** @var XMLParser (resource before PHP 8.0) The XML parser */
50    protected $xml_parser;
51
52    /** @var string Text contents of tags */
53    protected string $text = '';
54
55    /**
56     * Create a parser for a report
57     *
58     * @param string $report The XML filename
59     *
60     * @throws Exception
61     */
62    public function __construct(string $report)
63    {
64        $this->xml_parser = xml_parser_create();
65
66        xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, false);
67
68        xml_set_element_handler(
69            $this->xml_parser,
70            function ($parser, string $name, array $attrs): void {
71                $this->startElement($parser, $name, $attrs);
72            },
73            function ($parser, string $name): void {
74                $this->endElement($parser, $name);
75            }
76        );
77
78        xml_set_character_data_handler(
79            $this->xml_parser,
80            function ($parser, string $data): void {
81                $this->characterData($parser, $data);
82            }
83        );
84
85        $fp = fopen($report, 'rb');
86
87        while ($data = fread($fp, 4096)) {
88            if (!xml_parse($this->xml_parser, $data, feof($fp))) {
89                throw new DomainException(sprintf(
90                    'XML error: %s at line %d',
91                    xml_error_string(xml_get_error_code($this->xml_parser)),
92                    xml_get_current_line_number($this->xml_parser)
93                ));
94            }
95        }
96
97        fclose($fp);
98
99        xml_parser_free($this->xml_parser);
100    }
101
102    /**
103     * XML handler for an opening (or self-closing) tag.
104     *
105     * @param resource      $parser The resource handler for the xml parser
106     * @param string        $name   The name of the xml element parsed
107     * @param array<string> $attrs  An array of key value pairs for the attributes
108     *
109     * @return void
110     */
111    protected function startElement($parser, string $name, array $attrs): void
112    {
113        $method = $name . 'StartHandler';
114
115        if (method_exists($this, $method)) {
116            call_user_func([$this, $method], $attrs);
117        }
118    }
119
120    /**
121     * XML handler for a closing tag.
122     *
123     * @param resource $parser the resource handler for the xml parser
124     * @param string   $name   the name of the xml element parsed
125     *
126     * @return void
127     */
128    protected function endElement($parser, string $name): void
129    {
130        $method = $name . 'EndHandler';
131
132        if (method_exists($this, $method)) {
133            call_user_func([$this, $method]);
134        }
135    }
136
137    /**
138     * XML handler for character data.
139     *
140     * @param resource $parser The resource handler for the xml parser
141     * @param string   $data   The name of the xml element parsed
142     *
143     * @return void
144     */
145    protected function characterData($parser, string $data): void
146    {
147        $this->text .= $data;
148    }
149}
150