XML   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 2
dl 0
loc 67
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 4 1
A stringify() 0 10 2
A load() 0 6 2
A save() 0 6 2
1
<?php
2
3
/**
4
 * @package Cadmium\Framework\XML
5
 * @author Anton Romanov
6
 * @copyright Copyright (c) 2015-2017, Anton Romanov
7
 * @link http://cadmium-cms.com
8
 */
9
10
namespace {
11
12
	abstract class XML {
13
14
		/**
15
		 * Parse a string as XML
16
		 *
17
		 * @return SimpleXMLElement|false : the XML object or false on failure
18
		 */
19
20
		public static function parse(string $string) {
21
22
			return simplexml_load_string($string);
23
		}
24
25
		/**
26
		 * Convert an XML object to a string
27
		 *
28
		 * @return string|false : the string or false on failure
29
		 */
30
31
		public static function stringify(SimpleXMLElement $xml) {
32
33
			if (false === ($xml = dom_import_simplexml($xml))) return false;
34
35
			$dom = $xml->ownerDocument; $dom->formatOutput = true;
36
37
			# ------------------------
38
39
			return $dom->saveXML();
40
		}
41
42
		/**
43
		 * Load an XML object from a file
44
		 *
45
		 * @return SimpleXMLElement|false : the XML object or false on failure
46
		 */
47
48
		public static function load(string $file_name) {
49
50
			if (false === ($contents = Explorer::getContents($file_name))) return false;
51
52
			return self::parse($contents);
53
		}
54
55
		/**
56
		 * Save an XML object into a file
57
		 *
58
		 * @return int|false : the number of bytes that were written to the file or false on failure
59
		 */
60
61
		public static function save(string $file_name, SimpleXMLElement $xml) {
62
63
			if (false === ($xml = self::stringify($xml))) return false;
64
65
			return Explorer::putContents($file_name, $xml);
66
		}
67
68
		/**
69
		 * Output XML
70
		 */
71
72
		public static function output(SimpleXMLElement $xml) {
73
74
			Headers::sendNoCache(); Headers::sendStatus(STATUS_CODE_200); Headers::sendContent(MIME_TYPE_XML);
75
76
			echo self::stringify($xml);
77
		}
78
	}
79
}
80