123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- namespace App\Library\Protocol;
- abstract class _Payload {
- public function __construct(array $data = null) {
- if ($data != null) {
- $this->set($data);
- }
- }
- public function set($data) {
- //TODO: recursive set data
- if ($data === null) {
- return false;
- } elseif (is_array($data)) {
- foreach ($data as $k => $v) {
- if (property_exists(get_class($this), $k)) {
- $t = sprintf("_%s", $k);
- if (property_exists(get_class($this), $t)) {
- $tp = $this->{$t};
- $type = $tp['type'];
- $this->{$k} = $this->cast($v, $type);
- }
- }
- }
- } elseif (is_object($data) && $data instanceof $this) {
- foreach ($data as $k => $v) {
- $t = sprintf("_%s", $k);
- if (property_exists(get_class($this), $t)) {
- $this->{$k} = $v;
- }
- }
- } else {
- return false;
- }
- return true;
- }
- public function cast($value, $type) {
- switch ($type) {
- case 'integer':
- $ret = (int) $value;
- break;
- case 'float':
- $ret = (double) $value;
- break;
- case 'string':
- $ret = (string) $value;
- break;
- case 'boolean':
- $ret = (boolean) $value;
- break;
- default:
- if (preg_match('/^(array\[([A-Za-z_]+)\]|dict\[([A-Za-z_]+)\]([A-Za-z_]+))$/', $type, $matches)) {
- $ret = [];
- if (substr($matches[0], 0, 4) === 'dict') {
- foreach ($value as $k => $v) {
- $key = $this->cast($k, $matches[3]);
- $value = $this->cast($v, $matches[4]);
- $ret[$key] = $value;
- }
- } else {
- foreach ($value as $k => $v) {
- $ret[] = $this->cast($v, $matches[2]);
- }
- }
- } elseif (is_object($value)) {
- $klass = sprintf("%s\\%s", __NAMESPACE__, $type);
- if ($value instanceof $klass) {
- $ret = $this->iterate($value);
- } else {
- $ret = $value;
- }
- } else {
- $ret = $value;
- }
- break;
- }
- return $ret;
- }
- public function iterate($object = null) {
- $ret = [];
- if ($object === null) {
- $object = $this;
- }
- foreach ($object as $k => $v) {
- $t = sprintf("_%s", $k);
- if (property_exists(get_class($object), $t)) {
- $tp = $this->{$t};
- $type = $tp['type'];
- $ret[$k] = $this->cast($v, $type);
- }
- }
- return $ret;
- }
- }
- /*
- #token结构
- */
- final class token_info extends _Payload {
- public $token = "";
- protected $_token = [
- 'type' => 'string',
- 'required' => true,
- ];
- }
- /*
- #可用服务信息结构
- */
- final class service_info extends _Payload {
- public $name = "";
- protected $_name = [
- 'type' => 'string',
- 'required' => true,
- ];
- public $auth = 0;
- protected $_auth = [
- 'type' => 'integer',
- 'required' => true,
- ];
- }
- /*
- #appid-appkey结构
- */
- final class app_info extends _Payload {
- public $appid = "";
- protected $_appid = [
- 'type' => 'string',
- 'required' => true,
- ];
- }
- /*
- #单id结构
- */
- final class struct_id extends _Payload {
- public $id = 0;
- protected $_id = [
- 'type' => 'integer',
- 'required' => true,
- ];
- }
- /*
- #可用服务列表
- */
- final class service_list extends _Payload {
- public $service = [];
- protected $_service = [
- 'type' => 'array[service_info]',
- 'required' => true,
- ];
- }
- /*
- #请求token参数
- */
- final class token_req_info extends _Payload {
- public $server_id = "";
- protected $_server_id = [
- 'type' => 'string',
- 'required' => true,
- ];
- public $user_id = 0;
- protected $_user_id = [
- 'type' => 'integer',
- 'required' => true,
- ];
- }
|