BMLT Root Server
Public Member Functions | Private Member Functions | Private Attributes | List of all members
PhpJsonXmlArrayStringInterchanger Class Reference

Public Member Functions

 displayLastError ()
 
 displayErrorLog ()
 
 convertXmlToArray ($xmlContent)
 
 convertArrayToXML ($array)
 
 convertJsonToXML ($json)
 
 convertJsonToArray ($json)
 
 convertStringToArray ($string, &$myarray="")
 
 convertXmltoString ($xml)
 

Private Member Functions

 parseXml ($ret)
 
 parseArray ($array)
 
 convertArrayToString ($myarray, &$output="", &$parentkey="")
 

Private Attributes

 $errorLog =array()
 

Detailed Description

Definition at line 14 of file PhpJsonXmlArrayStringInterchanger.inc.php.

Member Function Documentation

convertArrayToString (   $myarray,
$output = "",
$parentkey = "" 
)
private

Function to parse Array and convert it into string

Parameters
array$arrayprivate
Returns
string on success and false on failure

Definition at line 236 of file PhpJsonXmlArrayStringInterchanger.inc.php.

References LINE_SEPERATOR.

Referenced by convertXmltoString().

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  }
convertArrayToString($myarray, &$output="", &$parentkey="")
convertArrayToXML (   $array)

Function to convert an associative array into XML

Parameters
string$arraypublic
Returns
string(xml) on success and false on failure

Definition at line 144 of file PhpJsonXmlArrayStringInterchanger.inc.php.

References parseArray().

Referenced by convertJsonToXML().

145  {
146  $return="<?xml version='1.0' encoding='ISO-8859-1'?><PhpJsonXmlArrayStringInterchanger>";
147  $return.=$this->parseArray($array);
148  $return.="</PhpJsonXmlArrayStringInterchanger>";
149  return $return;
150  }
convertJsonToArray (   $json)

Function to convert an JSON into array

Parameters
string$jsonpublic
Returns
array on success and false on failure

Definition at line 181 of file PhpJsonXmlArrayStringInterchanger.inc.php.

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  }
convertJsonToXML (   $json)

Function to convert an JSON into XML

Parameters
string$jsonpublic
Returns
string(xml) on success and false on failure

Definition at line 159 of file PhpJsonXmlArrayStringInterchanger.inc.php.

References convertArrayToXML().

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  }
convertStringToArray (   $string,
$myarray = "" 
)

Function to parse String and convert it into array

Parameters
array$arraypublic
Returns
array on success and false on failure

Definition at line 207 of file PhpJsonXmlArrayStringInterchanger.inc.php.

References LINE_SEPERATOR, and NODE_SEPERATOR.

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  }
convertXmlToArray (   $xmlContent)

Function to convert XML into Array

Parameters
string$xmlContentpublic
Returns
array on success and false on failure

Definition at line 88 of file PhpJsonXmlArrayStringInterchanger.inc.php.

References $ret, and parseXml().

Referenced by convertXmltoString().

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  }
$ret
Definition: contact.php:226
convertXmltoString (   $xml)

Function to convert XML into string

Parameters
string$xml
Returns
string on success and false on failure

Definition at line 264 of file PhpJsonXmlArrayStringInterchanger.inc.php.

References convertArrayToString(), and convertXmlToArray().

265  {
266  $array=$this->convertXmlToArray($xml);
267  if ($array===false) {
268  return false;
269  } else {
270  return $this->convertArrayToString($array);
271  }
272  }
convertArrayToString($myarray, &$output="", &$parentkey="")
displayErrorLog ( )

Function to display complete error log for debugging purpose

public

Returns
string

Definition at line 42 of file PhpJsonXmlArrayStringInterchanger.inc.php.

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  }
displayLastError ( )

Function to display last error for debugging purpose

public

Returns
string

Definition at line 25 of file PhpJsonXmlArrayStringInterchanger.inc.php.

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  }
parseArray (   $array)
private

Function to recursivly parse Array Content

Parameters
mixed$retprivate
Returns
string(xml) on success and false on failure

Definition at line 112 of file PhpJsonXmlArrayStringInterchanger.inc.php.

Referenced by convertArrayToXML().

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  }
parseXml (   $ret)
private

Function to recursivly parse Xml Content

Parameters
mixed$retprivate
Returns
array on success and false on failure

Definition at line 62 of file PhpJsonXmlArrayStringInterchanger.inc.php.

References $ret.

Referenced by convertXmlToArray().

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  }
$ret
Definition: contact.php:226

Member Data Documentation

$errorLog =array()
private

Definition at line 17 of file PhpJsonXmlArrayStringInterchanger.inc.php.


The documentation for this class was generated from the following file: