PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/classes/VarExportor.php

http://github.com/iwind/rockmongo
PHP | 241 lines | 197 code | 15 blank | 29 comment | 25 complexity | 020d61f901e7af6127998b4580f7da25 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. define("MONGO_EXPORT_PHP", "array");
  3. define("MONGO_EXPORT_JSON", "json");
  4. class VarExportor {
  5. private $_db;
  6. private $_var;
  7. private $_phpParams = array();
  8. private $_jsonParams = array();
  9. private $_paramIndex = 0;
  10. /**
  11. * construct exportor
  12. *
  13. * @param MongoDB $db current db you are operating
  14. * @param mixed $var variable
  15. */
  16. function __construct(MongoDB $db, $var) {
  17. $this->_db = $db;
  18. $this->_var = $var;
  19. }
  20. /**
  21. * Export the variable to a string
  22. *
  23. * @param string $type variable type (array or json)
  24. * @param boolean $fieldLabel if add label to fields
  25. * @return string
  26. */
  27. function export($type = MONGO_EXPORT_PHP, $fieldLabel = false) {
  28. if ($fieldLabel) {
  29. $this->_var = $this->_addLabelToArray($this->_var);
  30. }
  31. if ($type == MONGO_EXPORT_PHP) {
  32. return $this->_exportPHP();
  33. }
  34. return $this->_exportJSON();
  35. }
  36. /**
  37. * Export the variable to PHP
  38. *
  39. * @return string
  40. */
  41. private function _exportPHP() {
  42. $var = $this->_formatVar($this->_var);
  43. $string = var_export($var, true);
  44. $params = array();
  45. foreach ($this->_phpParams as $index => $value) {
  46. $params["'" . $this->_param($index) . "'"] = $value;
  47. }
  48. return strtr($string, $params);
  49. }
  50. /**
  51. * Substr utf-8 version
  52. *
  53. * @param unknown_type $str
  54. * @param unknown_type $from
  55. * @param unknown_type $len
  56. * @return unknown
  57. * @author sajjad at sajjad dot biz (copied from PHP manual)
  58. */
  59. private function _utf8_substr($str,$from,$len) {
  60. return function_exists('mb_substr') ?
  61. mb_substr($str, $from, $len, 'UTF-8') :
  62. preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'. $from .'}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'. $len .'}).*#s','$1', $str);
  63. }
  64. private function _addLabelToArray($array, $prev = "") {
  65. $ret = array();
  66. $cutLength = 150;
  67. foreach ($array as $key => $value) {
  68. if (is_string($key)) {
  69. $newKey = $prev . ($prev === ""?"":".") . "rockfield." . $key;
  70. if (is_string($value) && strlen($value) > $cutLength) {
  71. $value = $this->_utf8_substr($value, 0, $cutLength);
  72. $value = $value . " __rockmore.{$newKey}.rockmore__";
  73. }
  74. $ret[$newKey . ".rockfield"] = $value;
  75. if (is_array($value)) {
  76. $ret[$newKey . ".rockfield"] = $this->_addLabelToArray($value, $newKey);
  77. }
  78. }
  79. else {
  80. $ret[$key] = $value;
  81. }
  82. }
  83. return $ret;
  84. }
  85. private function _exportJSON() {
  86. if (function_exists("json_encode")) {
  87. $service = "json_encode";
  88. } else {
  89. import("classes.Services_JSON");
  90. $json = new Services_JSON();
  91. $service = array(&$json, 'encode');
  92. }
  93. $var = $this->_formatVarAsJSON($this->_var, $service);
  94. $string = call_user_func($service, $var);
  95. //Remove "\/" escape
  96. $string = str_replace('\/', "/", $string);
  97. $params = array();
  98. foreach ($this->_jsonParams as $index => $value) {
  99. $params['"' . $this->_param($index) . '"'] = $value;
  100. }
  101. return json_unicode_to_utf8(json_format(strtr($string, $params)));
  102. }
  103. private function _param($index) {
  104. return "%{MONGO_PARAM_{$index}}";
  105. }
  106. private function _formatVar($var) {
  107. if (is_scalar($var) || is_null($var)) {
  108. switch (gettype($var)) {
  109. case "integer":
  110. if (class_exists("MongoInt32")) {
  111. // producing MongoInt32 to keep type unchanged (Kyryl Bilokurov <kyryl.bilokurov@gmail.com>)
  112. $this->_paramIndex ++;
  113. $this->_phpParams[$this->_paramIndex] = 'new MongoInt32(' . $var . ')';
  114. return $this->_param($this->_paramIndex);
  115. }
  116. default:
  117. return $var;
  118. }
  119. }
  120. if (is_array($var)) {
  121. foreach ($var as $index => $value) {
  122. $var[$index] = $this->_formatVar($value);
  123. }
  124. return $var;
  125. }
  126. if (is_object($var)) {
  127. $this->_paramIndex ++;
  128. switch (get_class($var)) {
  129. case "stdClass":
  130. $this->_phpParams[$this->_paramIndex] = array();
  131. return $this->_param($this->_paramIndex);
  132. case "MongoId":
  133. $this->_phpParams[$this->_paramIndex] = 'new MongoId("' . $var->__toString() . '")';
  134. return $this->_param($this->_paramIndex);
  135. case "MongoInt32":
  136. $this->_phpParams[$this->_paramIndex] = 'new MongoInt32(' . $var->__toString() . ')';
  137. return $this->_param($this->_paramIndex);
  138. case "MongoInt64":
  139. $this->_phpParams[$this->_paramIndex] = 'new MongoInt64(' . $var->__toString() . ')';
  140. return $this->_param($this->_paramIndex);
  141. case "MongoDate":
  142. $this->_phpParams[$this->_paramIndex] = 'new MongoDate(' . $var->sec . ', ' . $var->usec . ')';
  143. return $this->_param($this->_paramIndex);
  144. case "MongoRegex":
  145. $this->_phpParams[$this->_paramIndex] = 'new MongoRegex(\'/' . $var->regex . '/' . $var->flags . '\')';
  146. return $this->_param($this->_paramIndex);
  147. case "MongoTimestamp":
  148. $this->_phpParams[$this->_paramIndex] = 'new MongoTimestamp(' . $var->sec . ', ' . $var->inc . ')';
  149. return $this->_param($this->_paramIndex);
  150. case "MongoMinKey":
  151. $this->_phpParams[$this->_paramIndex] = 'new MongoMinKey()';
  152. return $this->_param($this->_paramIndex);
  153. case "MongoMaxKey":
  154. $this->_phpParams[$this->_paramIndex] = 'new MongoMaxKey()';
  155. return $this->_param($this->_paramIndex);
  156. case "MongoCode":
  157. $this->_phpParams[$this->_paramIndex] = 'new MongoCode("' . addcslashes($var->code, '"') . '", ' . var_export($var->scope, true) . ')';
  158. return $this->_param($this->_paramIndex);
  159. default:
  160. if (method_exists($var, "__toString")) {
  161. return $var->__toString();
  162. }
  163. }
  164. }
  165. return $var;
  166. }
  167. private function _formatVarAsJSON($var, $jsonService) {
  168. if (is_scalar($var) || is_null($var)) {
  169. switch (gettype($var)) {
  170. case "integer":
  171. $this->_paramIndex ++;
  172. $this->_jsonParams[$this->_paramIndex] = 'NumberInt(' . $var . ')';
  173. return $this->_param($this->_paramIndex);
  174. default:
  175. return $var;
  176. }
  177. }
  178. if (is_array($var)) {
  179. foreach ($var as $index => $value) {
  180. $var[$index] = $this->_formatVarAsJSON($value, $jsonService);
  181. }
  182. return $var;
  183. }
  184. if (is_object($var)) {
  185. $this->_paramIndex ++;
  186. switch (get_class($var)) {
  187. case "MongoId":
  188. $this->_jsonParams[$this->_paramIndex] = 'ObjectId("' . $var->__toString() . '")';
  189. return $this->_param($this->_paramIndex);
  190. case "MongoInt32":
  191. $this->_jsonParams[$this->_paramIndex] = 'NumberInt(' . $var->__toString() . ')';
  192. return $this->_param($this->_paramIndex);
  193. case "MongoInt64":
  194. $this->_jsonParams[$this->_paramIndex] = 'NumberLong(' . $var->__toString() . ')';
  195. return $this->_param($this->_paramIndex);
  196. case "MongoDate":
  197. $timezone = @date_default_timezone_get();
  198. date_default_timezone_set("UTC");
  199. $this->_jsonParams[$this->_paramIndex] = "ISODate(\"" . date("Y-m-d", $var->sec) . "T" . date("H:i:s.", $var->sec) . ($var->usec/1000) . "Z\")";
  200. date_default_timezone_set($timezone);
  201. return $this->_param($this->_paramIndex);
  202. case "MongoTimestamp":
  203. $this->_jsonParams[$this->_paramIndex] = call_user_func($jsonService, array(
  204. "t" => $var->inc * 1000,
  205. "i" => $var->sec
  206. ));
  207. return $this->_param($this->_paramIndex);
  208. case "MongoMinKey":
  209. $this->_jsonParams[$this->_paramIndex] = call_user_func($jsonService, array( '$minKey' => 1 ));
  210. return $this->_param($this->_paramIndex);
  211. case "MongoMaxKey":
  212. $this->_jsonParams[$this->_paramIndex] = call_user_func($jsonService, array( '$minKey' => 1 ));
  213. return $this->_param($this->_paramIndex);
  214. case "MongoCode":
  215. $this->_jsonParams[$this->_paramIndex] = $var->__toString();
  216. return $this->_param($this->_paramIndex);
  217. default:
  218. if (method_exists($var, "__toString")) {
  219. return $var->__toString();
  220. }
  221. return '<unknown type>';
  222. }
  223. }
  224. }
  225. }
  226. ?>