Response.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /**
  3. * 数据库操作Response类,返回response对象{code:0, body:{}}
  4. * @author Zhu Jinhui<jinhui.zhu@live.cn> 2015-12-31 17:49
  5. * 用法:
  6. * $response = array();
  7. *
  8. * $response[] = new \Qii\Driver\Response(array(
  9. * 'code' => \Qii\Driver\Response::DO_SUCCESS,
  10. * 'body' => array(
  11. * 'operate' => 'save',
  12. * 'result' => 10
  13. * )
  14. * ));
  15. * $response[] = \Qii\Driver\Response::Success('save', 10);
  16. * $response[] = new Response(array(
  17. * 'code' => \Qii\Driver\Response::DO_FAIL,
  18. * 'body' => array(
  19. * 'operate' => 'save',
  20. * 'result' => '操作失败'
  21. * )
  22. * ));
  23. * $response[] = Qii\Driver\Response::Fail('save', '操作失败');
  24. * $response[] = new Response(array(
  25. * 'code' => Qii\Driver\Response::DOES_EXISTS,
  26. * 'body' => array(
  27. * 'operate' => 'save',
  28. * 'result' => array(
  29. * 'uid' => 1,
  30. * 'email' => 'antsnet@163.com'
  31. * )
  32. * )
  33. * ));
  34. * $response[] = \Qii\Driver\Response::Exist('save', array('uid' => 10, 'email' => 'antsnet@163.com'));
  35. * $response[] = new Response(array(
  36. * 'code' => \Qii\Driver\Response::DOES_NOT_EXISTS,
  37. * 'body' => array(
  38. * 'operate' => 'save',
  39. * 'result' => array()
  40. * )
  41. * ));
  42. * $response[] = \Qii\Driver\Response::NotExist('save', 10);
  43. * $response[] = new Response(array(
  44. * 'code' => \Qii\Driver\Response::FAIL_FOR_VALIDATE,
  45. * 'body' => array(
  46. * 'operate' => 'save',
  47. * 'result' => array(
  48. * 'fields' => array(
  49. * array(
  50. * 'name' => 'email',
  51. * 'msg' => '验证失败/格式不正确'
  52. * )
  53. * )
  54. * )
  55. * )
  56. * ));
  57. * $response[] = \Qii\Driver\Response::FailValidate('save', array('fields' => array('name' => 'email', 'msg' => '验证失败/格式不正确')));
  58. *
  59. * $response[] = new Response(array(
  60. * 'code' => \Qii\Driver\Response::FAIL_FOR_SAVE,
  61. * 'body' => array(
  62. * 'operate' => 'save',
  63. * 'result' => '连接错误'
  64. * )
  65. * ));
  66. * $result[] = \Qii\Driver\Response::FailSave('save', '链接错误');
  67. * foreach($response AS $res)
  68. * {
  69. * if($res->isError())
  70. * {
  71. * echo "<pre>". $res->getCode() .'&nbsp;'. print_r($res->getError(), true) ."</pre>";
  72. * }
  73. * else
  74. * {
  75. * echo '操作成功' . print_r($res->getResult(), 1);
  76. * }
  77. * }
  78. */
  79. namespace Qii\Driver;
  80. class Response
  81. {
  82. const VERSION = '1.2';
  83. //成功
  84. const DO_SUCCESS = 0;
  85. //失败
  86. const DO_FAIL = 1;
  87. //数据已经存在
  88. const DOES_EXIST = 100;
  89. //数据不存在
  90. const DOES_NOT_EXIST = 101;
  91. //验证失败
  92. const FAIL_FOR_VALIDATE = 102;
  93. //保存失败
  94. const FAIL_FOR_SAVE = 103;
  95. //更新失败
  96. const FAIL_FOR_UPDATE = 104;
  97. //删除失败
  98. const FAIL_FOR_REMOVE = 105;
  99. //类为定义
  100. const UNDEFINED_CLASS = 106;
  101. //方法为定义
  102. const UNDEFINED_METHOD = 107;
  103. //是否有错误
  104. public $isError = false;
  105. //状态码,对应上边的常量
  106. public $code;
  107. //返回的内容
  108. public $body;
  109. /**
  110. * 实例化response对象
  111. */
  112. public function __construct()
  113. {
  114. $this->code = 0;
  115. $this->body = array();
  116. return $this;
  117. }
  118. /**
  119. * 设置response的信息
  120. */
  121. public function set(array $data)
  122. {
  123. if (!isset($data['code']) || !isset($data['body'])
  124. || !isset($data['body']['operate']) || !isset($data['body']['result'])
  125. ) {
  126. throw new \Exception('Response muse have code , body , body[operate] and body[result] element');
  127. }
  128. foreach ($data AS $key => $val) {
  129. $this->$key = $val;
  130. }
  131. $this->isError = $this->isError();
  132. return $this;
  133. }
  134. /**
  135. * 成功
  136. * @param string $operate 操作类型
  137. * @param mix $result 结果
  138. * @return Qii\Driver\Response
  139. */
  140. public static function Success($operate, $result)
  141. {
  142. return self::Instance(self::DO_SUCCESS, $operate, $result);
  143. }
  144. /**
  145. * 失败
  146. * @param string $operate 操作类型
  147. * @param mix $result 结果
  148. * @return Qii\Driver\Response
  149. */
  150. public static function Fail($operate, $result)
  151. {
  152. return self::Instance(self::DO_FAIL, $operate, $result);
  153. }
  154. /**
  155. * 记录已存在
  156. * @param string $operate 操作类型
  157. * @param mix $result 结果
  158. * @return Qii\Driver\Response
  159. */
  160. public static function Exist($operate, $result)
  161. {
  162. return self::Instance(self::DOES_EXIST, $operate, $result);
  163. }
  164. /**
  165. * 记录不存在
  166. * @param string $operate 操作类型
  167. * @param mix $result 结果
  168. * @return Qii\Driver\Response
  169. */
  170. public static function NotExist($operate, $result)
  171. {
  172. return self::Instance(self::DOES_NOT_EXIST, $operate, $result);
  173. }
  174. /**
  175. * 验证失败
  176. * @param string $operate 操作类型
  177. * @param mix $result 结果
  178. * @return Qii\Driver\Response
  179. */
  180. public static function FailValidate($operate, $result)
  181. {
  182. return self::Instance(self::FAIL_FOR_VALIDATE, $operate, $result);
  183. }
  184. /**
  185. * 保存失败
  186. * @param string $operate 操作类型
  187. * @param mix $result 结果
  188. * @return Qii\Driver\Response
  189. */
  190. public static function FailSave($operate, $result)
  191. {
  192. return self::Instance(self::FAIL_FOR_SAVE, $operate, $result);
  193. }
  194. /**
  195. * 更新失败
  196. * @param string $operate 操作类型
  197. * @param mix $result 结果
  198. * @return Qii\Driver\Response
  199. */
  200. public static function FailUpdate($operate, $result)
  201. {
  202. return self::Instance(self::FAIL_FOR_UPDATE, $operate, $result);
  203. }
  204. /**
  205. * 删除失败
  206. * @param string $operate 操作类型
  207. * @param mix $result 结果
  208. * @return Qii\Driver\Response
  209. */
  210. public static function FailRemove($operate, $result)
  211. {
  212. return self::Instance(self::FAIL_FOR_REMOVE, $operate, $result);
  213. }
  214. /**
  215. * 方法未定义
  216. * @param string $operate 操作类型
  217. * @param mix $result 结果
  218. * @return Qii\Driver\Response
  219. */
  220. public static function UndefinedMethod($operate, $result)
  221. {
  222. return self::Instance(self::UNDEFINED_METHOD, $operate, $result);
  223. }
  224. /**
  225. * 类失败
  226. * @param string $operate 操作类型
  227. * @param mix $result 结果
  228. * @return Qii\Driver\Response
  229. */
  230. public static function UndefinedClass($operate, $result)
  231. {
  232. return self::Instance(self::UNDEFINED_CLASS, $operate, $result);
  233. }
  234. /**
  235. * 直接初始化Qii\Driver\Response 对象
  236. */
  237. public static function Instance($code, $operate, $result)
  238. {
  239. $data = array('code' => $code, 'body' => array('operate' => $operate, 'result' => $result));
  240. return (new \Qii\Driver\Response())->set($data);
  241. }
  242. /**
  243. * 获取操作类型
  244. */
  245. public function getOperate()
  246. {
  247. if (isset($this->body['operate'])) return $this->body['operate'];
  248. throw new \Exception('Call undefined operate');
  249. }
  250. /**
  251. * 返回body中的result,默认返回_result字段
  252. * @param string $key 返回字段
  253. * @return mix
  254. */
  255. public function getResult($key = '_result')
  256. {
  257. if ($key) {
  258. return isset($this->body['result']) && isset($this->body['result'][$key]) ? $this->body['result'][$key] : null;
  259. }
  260. return $this->body['result'];
  261. }
  262. /**
  263. * 返回错误信息
  264. */
  265. public function getMessage()
  266. {
  267. $message = array(
  268. 0 => '成功',
  269. 1 => '失败',
  270. 100 => '数据已经存在',
  271. 101 => '数据不存在',
  272. 102 => '验证失败',
  273. 103 => '保存失败',
  274. 104 => '更新失败',
  275. 105 => '删除失败',
  276. 106 => '类未定义',
  277. 107 => '方法未定义'
  278. );
  279. $code = $this->getCode();
  280. if (isset($message[$code])) return $message[$code] . $this->getResult();
  281. return $this->getResult('message');
  282. }
  283. /**
  284. * 返回错误信息,如果无错误即返回false
  285. * @return array
  286. */
  287. public function getErrors()
  288. {
  289. $code = $this->code;
  290. if ($code == self::DO_SUCCESS) return false;
  291. return $this->getResult();
  292. }
  293. /**
  294. * 是否包含错误信息
  295. */
  296. public function isError()
  297. {
  298. $code = $this->code;
  299. return $code == self::DO_SUCCESS ? false : true;
  300. }
  301. /**
  302. * 当调用get...的时候使用
  303. */
  304. public function __call($method, $args)
  305. {
  306. if (substr($method, 0, 3) == 'get') {
  307. $propertty = strtolower(substr($method, 3));
  308. if (property_exists($this, $propertty)) return $this->$propertty;
  309. }
  310. throw new \Qii\Exceptions\MethodNotFound(\Qii::i(1101, $method), __LINE__);
  311. }
  312. }