/application/datamapper/json.php

https://github.com/busaway/FoOlSlide · PHP · 220 lines · 148 code · 15 blank · 57 comment · 18 complexity · 900316682217d090fd1410bec9ac7495 MD5 · raw file

  1. <?php
  2. /**
  3. * Json Extension for DataMapper classes.
  4. *
  5. * Quickly convert DataMapper models to-and-from JSON syntax.
  6. *
  7. * @license MIT License
  8. * @package DMZ-Included-Extensions
  9. * @category DMZ
  10. * @author Phil DeJarnett
  11. * @link http://www.overzealous.com/dmz/pages/extensions/json.html
  12. * @version 1.1
  13. */
  14. // --------------------------------------------------------------------------
  15. /**
  16. * DMZ_Json Class
  17. *
  18. * @package DMZ-Included-Extensions
  19. */
  20. class DMZ_Json {
  21. /**
  22. * Convert a DataMapper model into JSON code.
  23. *
  24. * @param DataMapper $object The DataMapper Object to convert
  25. * @param array $fields Array of fields to include. If empty, includes all database columns.
  26. * @param boolean $pretty_print Format the JSON code for legibility.
  27. * @return string A JSON formatted String, or FALSE if an error occurs.
  28. */
  29. public function to_json($object, $fields = '', $pretty_print = FALSE)
  30. {
  31. if(empty($fields))
  32. {
  33. $fields = $object->fields;
  34. }
  35. $result = array();
  36. foreach($fields as $f)
  37. {
  38. $result[$f] = $object->{$f};
  39. }
  40. $json = json_encode($result);
  41. if($json === FALSE)
  42. {
  43. return FALSE;
  44. }
  45. if($pretty_print)
  46. {
  47. $json = $this->_json_format($json);
  48. }
  49. return $json;
  50. }
  51. /**
  52. * Convert the entire $object->all array result set into JSON code.
  53. *
  54. * @param DataMapper $object The DataMapper Object to convert
  55. * @param array $fields Array of fields to include. If empty, includes all database columns.
  56. * @param boolean $pretty_print Format the JSON code for legibility.
  57. * @return string A JSON formatted String, or FALSE if an error occurs.
  58. */
  59. public function all_to_json($object, $fields = '', $pretty_print = FALSE)
  60. {
  61. if(empty($fields))
  62. {
  63. $fields = $object->fields;
  64. }
  65. $result = array();
  66. foreach($object as $o)
  67. {
  68. $temp = array();
  69. foreach($fields as $f)
  70. {
  71. $temp[$f] = $o->{$f};
  72. }
  73. $result[] = $temp;
  74. }
  75. $json = json_encode($result);
  76. if($json === FALSE)
  77. {
  78. return FALSE;
  79. }
  80. if($pretty_print)
  81. {
  82. $json = $this->_json_format($json);
  83. }
  84. return $json;
  85. }
  86. /**
  87. * Convert a JSON object back into a DataMapper model.
  88. *
  89. * @param DataMapper $object The DataMapper Object to save to.
  90. * @param string $json_code A string that contains JSON code.
  91. * @param array $fields Array of 'safe' fields. If empty, only include the database columns.
  92. * @return bool TRUE or FALSE on success or failure of converting the JSON string.
  93. */
  94. public function from_json($object, $json_code, $fields = '')
  95. {
  96. if(empty($fields))
  97. {
  98. $fields = $object->fields;
  99. }
  100. $data = json_decode($json_code);
  101. if($data === FALSE)
  102. {
  103. return FALSE;
  104. }
  105. foreach($data as $k => $v) {
  106. if(in_array($k, $fields))
  107. {
  108. $object->{$k} = $v;
  109. }
  110. }
  111. return TRUE;
  112. }
  113. /**
  114. * Sets the HTTP Content-Type header to application/json
  115. *
  116. * @param DataMapper $object
  117. */
  118. public function set_json_content_type($object)
  119. {
  120. $CI =& get_instance();
  121. $CI->output->set_header('Content-Type: application/json');
  122. }
  123. /**
  124. * Formats a JSON string for readability.
  125. *
  126. * From @link http://php.net/manual/en/function.json-encode.php
  127. *
  128. * @param string $json Unformatted JSON
  129. * @return string Formatted JSON
  130. */
  131. private function _json_format($json)
  132. {
  133. $tab = " ";
  134. $new_json = "";
  135. $indent_level = 0;
  136. $in_string = false;
  137. $json_obj = json_decode($json);
  138. if($json_obj === false)
  139. return false;
  140. $json = json_encode($json_obj);
  141. $len = strlen($json);
  142. for($c = 0; $c < $len; $c++)
  143. {
  144. $char = $json[$c];
  145. switch($char)
  146. {
  147. case '{':
  148. case '[':
  149. if(!$in_string)
  150. {
  151. $new_json .= $char . "\n" . str_repeat($tab, $indent_level+1);
  152. $indent_level++;
  153. }
  154. else
  155. {
  156. $new_json .= $char;
  157. }
  158. break;
  159. case '}':
  160. case ']':
  161. if(!$in_string)
  162. {
  163. $indent_level--;
  164. $new_json .= "\n" . str_repeat($tab, $indent_level) . $char;
  165. }
  166. else
  167. {
  168. $new_json .= $char;
  169. }
  170. break;
  171. case ',':
  172. if(!$in_string)
  173. {
  174. $new_json .= ",\n" . str_repeat($tab, $indent_level);
  175. }
  176. else
  177. {
  178. $new_json .= $char;
  179. }
  180. break;
  181. case ':':
  182. if(!$in_string)
  183. {
  184. $new_json .= ": ";
  185. }
  186. else
  187. {
  188. $new_json .= $char;
  189. }
  190. break;
  191. case '"':
  192. if($c > 0 && $json[$c-1] != '\\')
  193. {
  194. $in_string = !$in_string;
  195. }
  196. default:
  197. $new_json .= $char;
  198. break;
  199. }
  200. }
  201. return $new_json;
  202. }
  203. }
  204. /* End of file json.php */
  205. /* Location: ./application/datamapper/json.php */