proto.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace App\Library\Protocol;
  3. abstract class _Payload {
  4. public function __construct(array $data = null) {
  5. if ($data != null) {
  6. $this->set($data);
  7. }
  8. }
  9. public function set($data) {
  10. //TODO: recursive set data
  11. if ($data === null) {
  12. return false;
  13. } elseif (is_array($data)) {
  14. foreach ($data as $k => $v) {
  15. if (property_exists(get_class($this), $k)) {
  16. $t = sprintf("_%s", $k);
  17. if (property_exists(get_class($this), $t)) {
  18. $tp = $this->{$t};
  19. $type = $tp['type'];
  20. $this->{$k} = $this->cast($v, $type);
  21. }
  22. }
  23. }
  24. } elseif (is_object($data) && $data instanceof $this) {
  25. foreach ($data as $k => $v) {
  26. $t = sprintf("_%s", $k);
  27. if (property_exists(get_class($this), $t)) {
  28. $this->{$k} = $v;
  29. }
  30. }
  31. } else {
  32. return false;
  33. }
  34. return true;
  35. }
  36. public function cast($value, $type) {
  37. switch ($type) {
  38. case 'integer':
  39. $ret = (int) $value;
  40. break;
  41. case 'float':
  42. $ret = (double) $value;
  43. break;
  44. case 'string':
  45. $ret = (string) $value;
  46. break;
  47. case 'boolean':
  48. $ret = (boolean) $value;
  49. break;
  50. default:
  51. if (preg_match('/^(array\[([A-Za-z_]+)\]|dict\[([A-Za-z_]+)\]([A-Za-z_]+))$/', $type, $matches)) {
  52. $ret = [];
  53. if (substr($matches[0], 0, 4) === 'dict') {
  54. foreach ($value as $k => $v) {
  55. $key = $this->cast($k, $matches[3]);
  56. $value = $this->cast($v, $matches[4]);
  57. $ret[$key] = $value;
  58. }
  59. } else {
  60. foreach ($value as $k => $v) {
  61. $ret[] = $this->cast($v, $matches[2]);
  62. }
  63. }
  64. } elseif (is_object($value)) {
  65. $klass = sprintf("%s\\%s", __NAMESPACE__, $type);
  66. if ($value instanceof $klass) {
  67. $ret = $this->iterate($value);
  68. } else {
  69. $ret = $value;
  70. }
  71. } else {
  72. $ret = $value;
  73. }
  74. break;
  75. }
  76. return $ret;
  77. }
  78. public function iterate($object = null) {
  79. $ret = [];
  80. if ($object === null) {
  81. $object = $this;
  82. }
  83. foreach ($object as $k => $v) {
  84. $t = sprintf("_%s", $k);
  85. if (property_exists(get_class($object), $t)) {
  86. $tp = $this->{$t};
  87. $type = $tp['type'];
  88. $ret[$k] = $this->cast($v, $type);
  89. }
  90. }
  91. return $ret;
  92. }
  93. }
  94. /*
  95. #token结构
  96. */
  97. final class token_info extends _Payload {
  98. public $token = "";
  99. protected $_token = [
  100. 'type' => 'string',
  101. 'required' => true,
  102. ];
  103. }
  104. /*
  105. #可用服务信息结构
  106. */
  107. final class service_info extends _Payload {
  108. public $name = "";
  109. protected $_name = [
  110. 'type' => 'string',
  111. 'required' => true,
  112. ];
  113. public $auth = 0;
  114. protected $_auth = [
  115. 'type' => 'integer',
  116. 'required' => true,
  117. ];
  118. }
  119. /*
  120. #appid-appkey结构
  121. */
  122. final class app_info extends _Payload {
  123. public $appid = "";
  124. protected $_appid = [
  125. 'type' => 'string',
  126. 'required' => true,
  127. ];
  128. }
  129. /*
  130. #单id结构
  131. */
  132. final class struct_id extends _Payload {
  133. public $id = 0;
  134. protected $_id = [
  135. 'type' => 'integer',
  136. 'required' => true,
  137. ];
  138. }
  139. /*
  140. #可用服务列表
  141. */
  142. final class service_list extends _Payload {
  143. public $service = [];
  144. protected $_service = [
  145. 'type' => 'array[service_info]',
  146. 'required' => true,
  147. ];
  148. }
  149. /*
  150. #请求token参数
  151. */
  152. final class token_req_info extends _Payload {
  153. public $server_id = "";
  154. protected $_server_id = [
  155. 'type' => 'string',
  156. 'required' => true,
  157. ];
  158. public $user_id = 0;
  159. protected $_user_id = [
  160. 'type' => 'integer',
  161. 'required' => true,
  162. ];
  163. }