Response.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. use Qii\Exceptions\MethodNotFound;
  81. class Response
  82. {
  83. const VERSION = '1.2';
  84. //成功
  85. const DO_SUCCESS = 0;
  86. //失败
  87. const DO_FAIL = 1;
  88. //数据已经存在
  89. const DOES_EXIST = 100;
  90. //数据不存在
  91. const DOES_NOT_EXIST = 101;
  92. //验证失败
  93. const FAIL_FOR_VALIDATE = 102;
  94. //保存失败
  95. const FAIL_FOR_SAVE = 103;
  96. //更新失败
  97. const FAIL_FOR_UPDATE = 104;
  98. //删除失败
  99. const FAIL_FOR_REMOVE = 105;
  100. //类为定义
  101. const UNDEFINED_CLASS = 106;
  102. //方法为定义
  103. const UNDEFINED_METHOD = 107;
  104. //是否有错误
  105. public $isError = false;
  106. //状态码,对应上边的常量
  107. public $code;
  108. //返回的内容
  109. public $body;
  110. /**
  111. * 实例化response对象
  112. */
  113. public function __construct()
  114. {
  115. $this->code = 0;
  116. $this->body = array();
  117. return $this;
  118. }
  119. /**
  120. * 设置response的信息
  121. */
  122. public function set(array $data)
  123. {
  124. if (!isset($data['code']) || !isset($data['body'])
  125. || !isset($data['body']['operate']) || !isset($data['body']['result'])
  126. ) {
  127. throw new \Exception('Response muse have code , body , body[operate] and body[result] element');
  128. }
  129. foreach ($data AS $key => $val) {
  130. $this->$key = $val;
  131. }
  132. $this->isError = $this->isError();
  133. return $this;
  134. }
  135. /**
  136. * 成功
  137. * @param string $operate 操作类型
  138. * @param mix $result 结果
  139. * @return Qii\Driver\Response
  140. */
  141. public static function Success($operate, $result)
  142. {
  143. return self::Instance(self::DO_SUCCESS, $operate, $result);
  144. }
  145. /**
  146. * 失败
  147. * @param string $operate 操作类型
  148. * @param mix $result 结果
  149. * @return Qii\Driver\Response
  150. */
  151. public static function Fail($operate, $result)
  152. {
  153. return self::Instance(self::DO_FAIL, $operate, $result);
  154. }
  155. /**
  156. * 记录已存在
  157. * @param string $operate 操作类型
  158. * @param mix $result 结果
  159. * @return Qii\Driver\Response
  160. */
  161. public static function Exist($operate, $result)
  162. {
  163. return self::Instance(self::DOES_EXIST, $operate, $result);
  164. }
  165. /**
  166. * 记录不存在
  167. * @param string $operate 操作类型
  168. * @param mix $result 结果
  169. * @return Qii\Driver\Response
  170. */
  171. public static function NotExist($operate, $result)
  172. {
  173. return self::Instance(self::DOES_NOT_EXIST, $operate, $result);
  174. }
  175. /**
  176. * 验证失败
  177. * @param string $operate 操作类型
  178. * @param mix $result 结果
  179. * @return Qii\Driver\Response
  180. */
  181. public static function FailValidate($operate, $result)
  182. {
  183. return self::Instance(self::FAIL_FOR_VALIDATE, $operate, $result);
  184. }
  185. /**
  186. * 保存失败
  187. * @param string $operate 操作类型
  188. * @param mix $result 结果
  189. * @return Qii\Driver\Response
  190. */
  191. public static function FailSave($operate, $result)
  192. {
  193. return self::Instance(self::FAIL_FOR_SAVE, $operate, $result);
  194. }
  195. /**
  196. * 更新失败
  197. * @param string $operate 操作类型
  198. * @param mix $result 结果
  199. * @return Qii\Driver\Response
  200. */
  201. public static function FailUpdate($operate, $result)
  202. {
  203. return self::Instance(self::FAIL_FOR_UPDATE, $operate, $result);
  204. }
  205. /**
  206. * 删除失败
  207. * @param string $operate 操作类型
  208. * @param mix $result 结果
  209. * @return Qii\Driver\Response
  210. */
  211. public static function FailRemove($operate, $result)
  212. {
  213. return self::Instance(self::FAIL_FOR_REMOVE, $operate, $result);
  214. }
  215. /**
  216. * 方法未定义
  217. * @param string $operate 操作类型
  218. * @param mix $result 结果
  219. * @return Qii\Driver\Response
  220. */
  221. public static function UndefinedMethod($operate, $result)
  222. {
  223. return self::Instance(self::UNDEFINED_METHOD, $operate, $result);
  224. }
  225. /**
  226. * 类失败
  227. * @param string $operate 操作类型
  228. * @param mix $result 结果
  229. * @return Qii\Driver\Response
  230. */
  231. public static function UndefinedClass($operate, $result)
  232. {
  233. return self::Instance(self::UNDEFINED_CLASS, $operate, $result);
  234. }
  235. /**
  236. * 直接初始化Qii\Driver\Response 对象
  237. */
  238. public static function Instance($code, $operate, $result)
  239. {
  240. $data = array('code' => $code, 'body' => array('operate' => $operate, 'result' => $result));
  241. return (new \Qii\Driver\Response())->set($data);
  242. }
  243. /**
  244. * 获取操作类型
  245. */
  246. public function getOperate()
  247. {
  248. if (isset($this->body['operate'])) return $this->body['operate'];
  249. throw new \Exception('Call undefined operate');
  250. }
  251. /**
  252. * 返回body中的result,默认返回_result字段
  253. * @param string $key 返回字段
  254. * @return mix
  255. */
  256. public function getResult($key = '_result')
  257. {
  258. if ($key) {
  259. return isset($this->body['result']) && isset($this->body['result'][$key]) ? $this->body['result'][$key] : null;
  260. }
  261. return $this->body['result'];
  262. }
  263. /**
  264. * 返回错误信息
  265. */
  266. public function getMessage()
  267. {
  268. $message = array(
  269. 0 => '成功',
  270. 1 => '失败',
  271. 100 => '数据已经存在',
  272. 101 => '数据不存在',
  273. 102 => '验证失败',
  274. 103 => '保存失败',
  275. 104 => '更新失败',
  276. 105 => '删除失败',
  277. 106 => '类未定义',
  278. 107 => '方法未定义'
  279. );
  280. $code = $this->getCode();
  281. if (isset($message[$code])) return $message[$code] . $this->getResult();
  282. return $this->getResult('message');
  283. }
  284. /**
  285. * 返回错误信息,如果无错误即返回false
  286. * @return array
  287. */
  288. public function getErrors()
  289. {
  290. $code = $this->code;
  291. if ($code == self::DO_SUCCESS) return false;
  292. return $this->getResult();
  293. }
  294. /**
  295. * 是否包含错误信息
  296. */
  297. public function isError()
  298. {
  299. $code = $this->code;
  300. return $code == self::DO_SUCCESS ? false : true;
  301. }
  302. /**
  303. * 当调用get...的时候使用
  304. */
  305. public function __call($method, $args)
  306. {
  307. if (substr($method, 0, 3) == 'get') {
  308. $propertty = strtolower(substr($method, 3));
  309. if (property_exists($this, $propertty)) return $this->$propertty;
  310. }
  311. throw new MethodNotFound(\Qii::i(1101, $method), __LINE__);
  312. }
  313. }