Cleanup phpdoc comments
[civicrm-core.git] / CRM / Utils / XML.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26*/
27
28/**
29 * @package CRM
06b69b18 30 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
31 * $Id$
32 */
33
34class CRM_Utils_XML {
35
36 /**
37 * Read a well-formed XML file
38 *
f4aaa82a
EM
39 * @param $file
40 *
6a488035
TO
41 * @return array (0 => SimpleXMLElement|FALSE, 1 => errorMessage|FALSE)
42 */
43 public static function parseFile($file) {
44 $xml = FALSE; // SimpleXMLElement
45 $error = FALSE; // string
46
47 if (! file_exists($file)) {
48 $error = 'File ' . $file . ' does not exist.';
49 } else {
50 $oldLibXMLErrors = libxml_use_internal_errors();
51 libxml_use_internal_errors(TRUE);
52
53 $xml = simplexml_load_file($file,
54 'SimpleXMLElement', LIBXML_NOCDATA
55 );
56 if ($xml === FALSE) {
57 $error = self::formatErrors(libxml_get_errors());
58 }
59
60 libxml_use_internal_errors($oldLibXMLErrors);
61 }
62
63 return array($xml, $error);
64 }
65
66 /**
67 * Read a well-formed XML file
68 *
f4aaa82a
EM
69 * @param $string
70 *
6a488035
TO
71 * @return array (0 => SimpleXMLElement|FALSE, 1 => errorMessage|FALSE)
72 */
73 public static function parseString($string) {
74 $xml = FALSE; // SimpleXMLElement
75 $error = FALSE; // string
76
77 $oldLibXMLErrors = libxml_use_internal_errors();
78 libxml_use_internal_errors(TRUE);
79
80 $xml = simplexml_load_string($string,
81 'SimpleXMLElement', LIBXML_NOCDATA
82 );
83 if ($xml === FALSE) {
84 $error = self::formatErrors(libxml_get_errors());
85 }
86
87 libxml_use_internal_errors($oldLibXMLErrors);
88
89 return array($xml, $error);
90 }
91
5bc392e6
EM
92 /**
93 * @param $errors
94 *
95 * @return string
96 */
6a488035
TO
97 protected static function formatErrors($errors) {
98 $messages = array();
99
100 foreach ($errors as $error) {
101 if ($error->level != LIBXML_ERR_ERROR && $error->level != LIBXML_ERR_FATAL) {
102 continue;
103 }
104
105 $parts = array();
106 if ($error->file) {
107 $parts[] = "File=$error->file";
108 }
109 $parts[] = "Line=$error->line";
110 $parts[] = "Column=$error->column";
111 $parts[] = "Code=$error->code";
112
113 $messages[] = implode(" ", $parts) . ": " . trim($error->message);
114 }
115
116 return implode("\n", $messages);
117 }
118
119 /**
120 * Convert an XML element to an array
121 *
c490a46a 122 * @param $obj SimpleXMLElement
f4aaa82a 123 *
6a488035
TO
124 * @return array
125 */
126 public static function xmlObjToArray($obj) {
127 $arr = array();
128 if (is_object($obj)) {
129 $obj = get_object_vars($obj);
130 }
131 if (is_array($obj)) {
132 foreach ($obj as $i => $v) {
133 if (is_object($v) || is_array($v)) {
134 $v = self::xmlObjToArray($v);
135 }
136 if (empty($v)) {
137 $arr[$i] = NULL;
138 }
139 else {
140 $arr[$i] = $v;
141 }
142 }
143 }
144 return $arr;
145 }
146}