Another set of PHPdoc corrections
[civicrm-core.git] / CRM / Utils / XML.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 * @package CRM
ca5cec67 14 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035 15 */
6a488035
TO
16class CRM_Utils_XML {
17
18 /**
19 * Read a well-formed XML file
20 *
fa3fdebc 21 * @param string $file
f4aaa82a 22 *
a6c01b45
CW
23 * @return array
24 * (0 => SimpleXMLElement|FALSE, 1 => errorMessage|FALSE)
6a488035
TO
25 */
26 public static function parseFile($file) {
6714d8d2
SL
27 // SimpleXMLElement
28 $xml = FALSE;
29 // string
30 $error = FALSE;
6a488035 31
353ffa53 32 if (!file_exists($file)) {
6a488035 33 $error = 'File ' . $file . ' does not exist.';
0db6c3e1
TO
34 }
35 else {
6a488035
TO
36 $oldLibXMLErrors = libxml_use_internal_errors();
37 libxml_use_internal_errors(TRUE);
38
6498f612 39 // Note that under obscure circumstances calling simplexml_load_file
40 // hit https://bugs.php.net/bug.php?id=62577
41 $string = file_get_contents($file);
42 $xml = simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA);
6a488035
TO
43 if ($xml === FALSE) {
44 $error = self::formatErrors(libxml_get_errors());
45 }
46
47 libxml_use_internal_errors($oldLibXMLErrors);
48 }
49
be2fb01f 50 return [$xml, $error];
6a488035
TO
51 }
52
53 /**
54 * Read a well-formed XML file
55 *
f4aaa82a
EM
56 * @param $string
57 *
a6c01b45
CW
58 * @return array
59 * (0 => SimpleXMLElement|FALSE, 1 => errorMessage|FALSE)
6a488035
TO
60 */
61 public static function parseString($string) {
6714d8d2
SL
62 // SimpleXMLElement
63 $xml = FALSE;
64 // string
65 $error = FALSE;
6a488035
TO
66
67 $oldLibXMLErrors = libxml_use_internal_errors();
68 libxml_use_internal_errors(TRUE);
69
70 $xml = simplexml_load_string($string,
71 'SimpleXMLElement', LIBXML_NOCDATA
72 );
73 if ($xml === FALSE) {
74 $error = self::formatErrors(libxml_get_errors());
75 }
76
77 libxml_use_internal_errors($oldLibXMLErrors);
78
be2fb01f 79 return [$xml, $error];
6a488035
TO
80 }
81
5bc392e6
EM
82 /**
83 * @param $errors
84 *
85 * @return string
86 */
6a488035 87 protected static function formatErrors($errors) {
be2fb01f 88 $messages = [];
6a488035
TO
89
90 foreach ($errors as $error) {
91 if ($error->level != LIBXML_ERR_ERROR && $error->level != LIBXML_ERR_FATAL) {
92 continue;
93 }
94
be2fb01f 95 $parts = [];
6a488035 96 if ($error->file) {
e7292422 97 $parts[] = "File=$error->file";
6a488035
TO
98 }
99 $parts[] = "Line=$error->line";
100 $parts[] = "Column=$error->column";
101 $parts[] = "Code=$error->code";
102
103 $messages[] = implode(" ", $parts) . ": " . trim($error->message);
104 }
105
106 return implode("\n", $messages);
107 }
108
109 /**
fe482240 110 * Convert an XML element to an array.
6a488035 111 *
77855840
TO
112 * @param $obj
113 * SimpleXMLElement.
f4aaa82a 114 *
6a488035
TO
115 * @return array
116 */
117 public static function xmlObjToArray($obj) {
be2fb01f 118 $arr = [];
6a488035
TO
119 if (is_object($obj)) {
120 $obj = get_object_vars($obj);
121 }
122 if (is_array($obj)) {
123 foreach ($obj as $i => $v) {
124 if (is_object($v) || is_array($v)) {
125 $v = self::xmlObjToArray($v);
126 }
127 if (empty($v)) {
128 $arr[$i] = NULL;
129 }
130 else {
131 $arr[$i] = $v;
132 }
133 }
134 }
135 return $arr;
136 }
96025800 137
6a488035 138}