Completed
Push — master ( f2fd19...c76b04 )
by Brendan
02:16
created

XmlFormatter::format()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of graze/telnet-client.
4
 *
5
 * Copyright (c) 2018 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/xml-utils/blob/master/LICENSE
11
 * @link https://github.com/graze/xml-utils
12
 */
13
14
namespace Graze\XmlUtils;
15
16
use DOMDocument;
17
use Exception;
18
19
class XmlFormatter
20
{
21
    /**
22
     * Return the XML with correct indentation.
23
     *
24
     * @param string $xml
25
     * @return string
26
     * @throws Exception
27
     */
28
    public function format($xml)
29
    {
30
        $dom = new DOMDocument('1.0');
31
        $dom->preserveWhiteSpace = false;
32
        $dom->formatOutput = true;
33
        $dom->loadXML($xml);
34
        $formatted = $dom->saveXML();
35
36
        if (!$formatted) {
37
            throw new Exception("An error occurred while formatting the XML");
38
        }
39
40
        return $formatted;
41
    }
42
}
43