Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
SimpleXML
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
1 / 1
 appendElement
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3/**
4 * VuFind SimpleXML enhancement functionality
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2009.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  SimpleXML
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFind;
31
32use SimpleXMLElement;
33
34/**
35 * VuFind SimpleXML enhancement functionality
36 *
37 * @category VuFind
38 * @package  SimpleXML
39 * @author   Demian Katz <demian.katz@villanova.edu>
40 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
41 * @link     https://vufind.org/wiki/development Wiki
42 */
43class SimpleXML
44{
45    /**
46     * Attach $child to $parent.
47     *
48     * @param SimpleXMLElement        $parent Parent element to modify
49     * @param SimpleXMLElement|string $child  Child element (or XML fragment) to
50     * attach
51     *
52     * @return void
53     */
54    public static function appendElement($parent, $child)
55    {
56        $xml = $child instanceof SimpleXMLElement
57            ? $child->asXML() : $child;
58
59        // strip off xml header
60        $mark = strpos($xml, '?' . '>');
61        if ($mark > 0 && $mark < 40) {
62            $xml = substr($xml, $mark + 2);
63        }
64
65        $dom = dom_import_simplexml($parent);
66        $fragment = $dom->ownerDocument->createDocumentFragment();
67        $fragment->appendXML($xml);
68        $dom->appendChild($fragment);
69    }
70}