XmlMapper   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 15
cp 0
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A toXml() 0 15 2
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Minotaur
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2017 Appertly
19
 * @license   Apache-2.0
20
 */
21
namespace Minotaur\Net;
22
23
/**
24
 * Turns strings into XML
25
 */
26
class XmlMapper
27
{
28
    /**
29
     * Converts a string to a SimpleXMLElement.
30
     *
31
     * @param string $xml The string to convert, or `null`
32
     * @return \SimpleXmlElement The XML version
33
     * @throws \Minotaur\Net\Exception\Illegible If the string is not XML
34
     */
35
    public function toXml(?string $xml): \SimpleXMLElement
36
    {
37
        try {
38
            libxml_use_internal_errors(true);
39
            $xml = simplexml_load_string($xml);
40
            if ($xml === false) {
41
                $errors = libxml_get_errors();
42
                throw new Exception\Illegible($xml, "Invalid XML. " . implode(". ", $errors));
43
            }
44
            return $xml;
45
        } finally {
46
            libxml_clear_errors();
47
            libxml_use_internal_errors(true);
48
        }
49
    }
50
}
51