BMLT Root Server
PhpJsonXmlArrayStringInterchanger.inc.php
Go to the documentation of this file.
1 <?php
2 define("NODE_SEPERATOR", "#*#");
3 define("LINE_SEPERATOR", "!#=#!");
4 define("ERROR_STYLE", "color:#fff;background-color:#ff0000;font-weight:bold;padding:5px;margin:5px;");
5 
6 /**
7  * This class interchange String, XML, JSON and Array into each other.
8  *
9  * @author Rochak Chauhan
10  * @package PhpJsonXmlArrayStringInterchanger
11  * @version beta
12  */
13 // phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
15 // phpcs:enable PSR1.Classes.ClassDeclaration.MissingNamespace
16 {
17  private $errorLog=array();
18 
19  /**
20  * Function to display last error for debugging purpose
21  *
22  * @access public
23  * @return string
24  */
25  public function displayLastError()
26  {
27  $return="No errors were encountered.";
28  $c=count($this->errorLog);
29  if ($c>0) {
30  $i=$c-1;
31  $return="<div style='".ERROR_STYLE."'>".$this->errorLog[$i]."</div>";
32  }
33  echo $return;
34  }
35 
36  /**
37  * Function to display complete error log for debugging purpose
38  *
39  * @access public
40  * @return string
41  */
42  public function displayErrorLog()
43  {
44  $return="No errors were encountered.";
45  $c=count($this->errorLog);
46  if ($c>0) {
47  $return="";
48  for ($i=0; $i<$c; $i++) {
49  $return.="<div style='".ERROR_STYLE."'>".$this->errorLog[$i]."</div>";
50  }
51  }
52  echo $return;
53  }
54 
55  /**
56  * Function to recursivly parse Xml Content
57  *
58  * @param mixed $ret
59  * @access private
60  * @return array on success and false on failure
61  */
62  private function parseXml($ret)
63  {
64  $return=false;
65  if (is_object($ret)) {
66  $ret=(array)$ret;
67  $this->parseXml($ret);
68  }
69  if (is_array($ret)) {
70  foreach ($ret as $k => $v) {
71  if (is_object($v)) {
72  $return[$k]=$this->parseXml($v);
73  } else {
74  $return[$k]=$v ;
75  }
76  }
77  }
78  return $return;
79  }
80 
81  /**
82  * Function to convert XML into Array
83  *
84  * @param string $xmlContent
85  * @access public
86  * @return array on success and false on failure
87  */
88  public function convertXmlToArray($xmlContent)
89  {
90  $return=false;
91  $ret=simplexml_load_string($xmlContent);
92  if ($ret===false) {
93  $this->errorLog[]="Invalid XML content: $xmlContent in function: ".__FUNCTION__." on line: ".__LINE__." in filename= ".__FILE__;
94  return false;
95  } else {
96  $return=$this->parseXml($ret);
97  if ($return===false) {
98  $this->errorLog[]="Failed to parse XML content in function: ".__FUNCTION__." on line: ".__LINE__." in filename= ".__FILE__;
99  return false;
100  }
101  }
102  return $return;
103  }
104 
105  /**
106  * Function to recursivly parse Array Content
107  *
108  * @param mixed $ret
109  * @access private
110  * @return string(xml) on success and false on failure
111  */
112  private function parseArray($array)
113  {
114  if (is_array($array)) {
115  foreach ($array as $k => $v) {
116  if (trim($k)=="") {
117  $this->errorLog[]="Array needs to be associative as parameter in function: ".__FUNCTION__." on line: ".__LINE__." in filename= ".__FILE__;
118  return false;
119  } else {
120  if (is_numeric($k)) {
121  $k="nodeValue$k";
122  }
123  if (is_array($v)) {
124  $return.="<$k>".$this->parseArray($v)."</$k>";
125  } else {
126  $return.="<$k>$v</$k>";
127  }
128  }
129  }
130  } else {
131  $this->errorLog[]="Invalid array in function: ".__FUNCTION__." on line: ".__LINE__." in filename= ".__FILE__;
132  return false;
133  }
134  return $return;
135  }
136 
137  /**
138  * Function to convert an associative array into XML
139  *
140  * @param string $array
141  * @access public
142  * @return string(xml) on success and false on failure
143  */
144  public function convertArrayToXML($array)
145  {
146  $return="<?xml version='1.0' encoding='ISO-8859-1'?><PhpJsonXmlArrayStringInterchanger>";
147  $return.=$this->parseArray($array);
148  $return.="</PhpJsonXmlArrayStringInterchanger>";
149  return $return;
150  }
151 
152  /**
153  * Function to convert an JSON into XML
154  *
155  * @param string $json
156  * @access public
157  * @return string(xml) on success and false on failure
158  */
159  public function convertJsonToXML($json)
160  {
161  if (!is_string($json)) {
162  $this->errorLog[]="The first parameter should to be string in function: ".__FUNCTION__." on line: ".__LINE__." in filename= ".__FILE__;
163  return false;
164  }
165  $array=json_decode($json, true);
166  if ($array===false) {
167  $this->errorLog[]="Failed to decode JSON in function: ".__FUNCTION__." on line: ".__LINE__." in filename= ".__FILE__;
168  return false;
169  } else {
170  return $this->convertArrayToXML($array);
171  }
172  }
173 
174  /**
175  * Function to convert an JSON into array
176  *
177  * @param string $json
178  * @access public
179  * @return array on success and false on failure
180  */
181  public function convertJsonToArray($json)
182  {
183  if (!is_string($json)) {
184  $this->errorLog[]="The first parameter should to be string in function: ".__FUNCTION__." on line: ".__LINE__." in filename= ".__FILE__;
185  return false;
186  }
187  $json=str_replace("\\'", "'", $json);
188  $json=str_replace('\\"', '"', $json);
189  $array=json_decode($json, true);
190  if ($array===false) {
191  $this->errorLog[]="Failed to decode JSON in function: ".__FUNCTION__." on line: ".__LINE__." in filename= ".__FILE__;
192  return false;
193  } else {
194  return $array;
195  }
196  }
197 
198 
199  /**
200  * Function to parse String and convert it into array
201  *
202  * @param array $array
203  * @access public
204  * @return array on success and false on failure
205  * @todo refactor the code from line 205-222 (automate it)
206  */
207  public function convertStringToArray($string, &$myarray = "")
208  {
209  $lines = explode(LINE_SEPERATOR, $string);
210  foreach ($lines as $value) {
211  $items = explode(NODE_SEPERATOR, $value);
212  if (sizeof($items) == 2) {
213  $myarray[$items[0]] = $items[1];
214  } elseif (sizeof($items) == 3) {
215  $myarray[$items[0]][$items[1]] = $items[2];
216  } elseif (sizeof($items) == 4) {
217  $myarray[$items[0]][$items[1]] [$items[2]] = $items[3];
218  } elseif (sizeof($items) == 5) {
219  $myarray[$items[0]][$items[1]] [$items[2]][$items[3]] = $items[4];
220  } elseif (sizeof($items) == 6) {
221  $myarray[$items[0]][$items[1]] [$items[2]][$items[3]][$items[4]] = $items[5];
222  } elseif (sizeof($items) == 7) {
223  $myarray[$items[0]][$items[1]] [$items[2]][$items[3]][$items[4]][$items[5]] = $items[6];
224  }
225  }
226  return $myarray;
227  }
228 
229  /**
230  * Function to parse Array and convert it into string
231  *
232  * @param array $array
233  * @access private
234  * @return string on success and false on failure
235  */
236  private function convertArrayToString($myarray, &$output = "", &$parentkey = "")
237  {
238  if (is_array($myarray)) {
239  if (trim($parentkey)=="") {
240  $parentkey=LINE_SEPERATOR;
241  }
242  foreach ($myarray as $key => $value) {
243  if (is_array($value)) {
244  $parentkey .= $key.NODE_SEPERATOR;
245  $this->convertArrayToString($value, $output, $parentkey);
246  $parentkey = "";
247  } else {
248  $output .= $parentkey.$key.NODE_SEPERATOR.$value.LINE_SEPERATOR;
249  }
250  }
251  } else {
252  $this->errorLog[]="Invalid array in function: ".__FUNCTION__." on line: ".__LINE__." in filename= ".__FILE__;
253  return false;
254  }
255  return $output;
256  }
257 
258  /**
259  * Function to convert XML into string
260  *
261  * @param string $xml
262  * @return string on success and false on failure
263  */
264  public function convertXmltoString($xml)
265  {
266  $array=$this->convertXmlToArray($xml);
267  if ($array===false) {
268  return false;
269  } else {
270  return $this->convertArrayToString($array);
271  }
272  }
273 }
convertArrayToString($myarray, &$output="", &$parentkey="")
$ret
Definition: contact.php:226