PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Format.php

https://github.com/Sa-ryong/Stadioom-php
PHP | 263 lines | 163 code | 48 blank | 52 comment | 17 complexity | 9c665ad1b7eedc918f542144d6a0110b MD5 | raw file
  1. <?php
  2. /**
  3. * Format class
  4. *
  5. * Help convert between various formats such as XML, JSON, CSV, etc.
  6. *
  7. * @author Phil Sturgeon
  8. * @license http://philsturgeon.co.uk/code/dbad-license
  9. */
  10. class Format {
  11. // Array to convert
  12. protected $_data = array();
  13. // View filename
  14. protected $_from_type = null;
  15. /**
  16. * Returns an instance of the Format object.
  17. *
  18. * echo $this->format->factory(array('foo' => 'bar'))->to_xml();
  19. *
  20. * @param mixed general date to be converted
  21. * @param string data format the file was provided in
  22. * @return Factory
  23. */
  24. public function factory($data, $from_type = null)
  25. {
  26. // Stupid stuff to emulate the "new static()" stuff in this libraries PHP 5.3 equivilent
  27. $class = __CLASS__;
  28. return new $class($data, $from_type);
  29. }
  30. /**
  31. * Do not use this directly, call factory()
  32. */
  33. public function __construct($data = null, $from_type = null)
  34. {
  35. get_instance()->load->helper('inflector');
  36. // If the provided data is already formatted we should probably convert it to an array
  37. if ($from_type !== null)
  38. {
  39. if (method_exists($this, '_from_' . $from_type))
  40. {
  41. $data = call_user_func(array($this, '_from_' . $from_type), $data);
  42. }
  43. else
  44. {
  45. throw new Exception('Format class does not support conversion from "' . $from_type . '".');
  46. }
  47. }
  48. $this->_data = $data;
  49. }
  50. // FORMATING OUTPUT ---------------------------------------------------------
  51. public function to_array($data = null)
  52. {
  53. // If not just null, but nopthing is provided
  54. if ($data === null and ! func_num_args())
  55. {
  56. $data = $this->_data;
  57. }
  58. $array = array();
  59. foreach ((array) $data as $key => $value)
  60. {
  61. if (is_object($value) or is_array($value))
  62. {
  63. $array[$key] = $this->to_array($value);
  64. }
  65. else
  66. {
  67. $array[$key] = $value;
  68. }
  69. }
  70. return $array;
  71. }
  72. // Format XML for output
  73. public function to_xml($data = null, $structure = null, $basenode = 'xml')
  74. {
  75. if ($data === null and ! func_num_args())
  76. {
  77. $data = $this->_data;
  78. }
  79. // turn off compatibility mode as simple xml throws a wobbly if you don't.
  80. if (ini_get('zend.ze1_compatibility_mode') == 1)
  81. {
  82. ini_set('zend.ze1_compatibility_mode', 0);
  83. }
  84. if ($structure === null)
  85. {
  86. $structure = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$basenode />");
  87. }
  88. // Force it to be something useful
  89. if ( ! is_array($data) AND ! is_object($data))
  90. {
  91. $data = (array) $data;
  92. }
  93. foreach ($data as $key => $value)
  94. {
  95. // no numeric keys in our xml please!
  96. if (is_numeric($key))
  97. {
  98. // make string key...
  99. $key = (singular($basenode) != $basenode) ? singular($basenode) : 'item';
  100. }
  101. // replace anything not alpha numeric
  102. $key = preg_replace('/[^a-z_\-0-9]/i', '', $key);
  103. // if there is another array found recrusively call this function
  104. if (is_array($value) || is_object($value))
  105. {
  106. $node = $structure->addChild($key);
  107. // recrusive call.
  108. $this->to_xml($value, $node, $key);
  109. }
  110. else
  111. {
  112. // add single node.
  113. $value = htmlspecialchars(html_entity_decode($value, ENT_QUOTES, 'UTF-8'), ENT_QUOTES, "UTF-8");
  114. $structure->addChild($key, $value);
  115. }
  116. }
  117. return $structure->asXML();
  118. }
  119. // Format HTML for output
  120. public function to_html()
  121. {
  122. $data = $this->_data;
  123. // Multi-dimentional array
  124. if (isset($data[0]))
  125. {
  126. $headings = array_keys($data[0]);
  127. }
  128. // Single array
  129. else
  130. {
  131. $headings = array_keys($data);
  132. $data = array($data);
  133. }
  134. $ci = get_instance();
  135. $ci->load->library('table');
  136. $ci->table->set_heading($headings);
  137. foreach ($data as &$row)
  138. {
  139. $ci->table->add_row($row);
  140. }
  141. return $ci->table->generate();
  142. }
  143. // Format HTML for output
  144. public function to_csv()
  145. {
  146. $data = $this->_data;
  147. // Multi-dimentional array
  148. if (isset($data[0]))
  149. {
  150. $headings = array_keys($data[0]);
  151. }
  152. // Single array
  153. else
  154. {
  155. $headings = array_keys($data);
  156. $data = array($data);
  157. }
  158. $output = implode(',', $headings).PHP_EOL;
  159. foreach ($data as &$row)
  160. {
  161. $output .= '"'.implode('","', $row).'"'.PHP_EOL;
  162. }
  163. return $output;
  164. }
  165. // Encode as JSON
  166. public function to_json()
  167. {
  168. return json_encode($this->_data);
  169. }
  170. // Encode as Serialized array
  171. public function to_serialized()
  172. {
  173. return serialize($this->_data);
  174. }
  175. // Output as a string representing the PHP structure
  176. public function to_php()
  177. {
  178. return var_export($this->_data, TRUE);
  179. }
  180. // Format XML for output
  181. protected function _from_xml($string)
  182. {
  183. return $string ? (array) simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA) : array();
  184. }
  185. // Format HTML for output
  186. // This function is DODGY! Not perfect CSV support but works with my REST_Controller
  187. protected function _from_csv($string)
  188. {
  189. $data = array();
  190. // Splits
  191. $rows = explode("\n", trim($string));
  192. $headings = explode(',', array_shift($rows));
  193. foreach ($rows as $row)
  194. {
  195. // The substr removes " from start and end
  196. $data_fields = explode('","', trim(substr($row, 1, -1)));
  197. if (count($data_fields) == count($headings))
  198. {
  199. $data[] = array_combine($headings, $data_fields);
  200. }
  201. }
  202. return $data;
  203. }
  204. // Encode as JSON
  205. private function _from_json($string)
  206. {
  207. return json_decode(trim($string));
  208. }
  209. // Encode as Serialized array
  210. private function _from_serialize($string)
  211. {
  212. return unserialize(trim($string));
  213. }
  214. }
  215. /* End of file format.php */