AbstractModel.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /*
  3. * Copyright (c) 2017-2018 THL A29 Limited, a Tencent company. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing,
  12. * software distributed under the License is distributed on an
  13. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. * KIND, either express or implied. See the License for the
  15. * specific language governing permissions and limitations
  16. * under the License.
  17. */
  18. namespace TencentCloud\Common;
  19. use \ReflectionClass;
  20. /**
  21. * 抽象model类,禁止client引用
  22. * @package TencentCloud\Common
  23. */
  24. abstract class AbstractModel
  25. {
  26. /**
  27. * 内部实现,用户禁止调用
  28. */
  29. public function serialize()
  30. {
  31. $ret = $this->objSerialize($this);
  32. return $ret;
  33. }
  34. private function objSerialize($obj) {
  35. $memberRet = [];
  36. $ref = new ReflectionClass(get_class($obj));
  37. $memberList = $ref->getProperties();
  38. foreach ($memberList as $x => $member)
  39. {
  40. $name = ucfirst($member->getName());
  41. $member->setAccessible(true);
  42. $value = $member->getValue($obj);
  43. if ($value === null) {
  44. continue;
  45. }
  46. if ($value instanceof AbstractModel) {
  47. $memberRet[$name] = $this->objSerialize($value);
  48. } else if (is_array($value)) {
  49. $memberRet[$name] = $this->arraySerialize($value);
  50. } else {
  51. $memberRet[$name] = $value;
  52. }
  53. }
  54. return $memberRet;
  55. }
  56. private function arraySerialize($memberList) {
  57. $memberRet = [];
  58. foreach ($memberList as $name => $value)
  59. {
  60. if ($value === null) {
  61. continue;
  62. }
  63. if ($value instanceof AbstractModel) {
  64. $memberRet[$name] = $this->objSerialize($value);
  65. } elseif (is_array($value)) {
  66. $memberRet[$name] = $this->arraySerialize($value);
  67. }else {
  68. $memberRet[$name] = $value;
  69. }
  70. }
  71. return $memberRet;
  72. }
  73. private function arrayMerge($array, $prepend = null)
  74. {
  75. $results = array();
  76. foreach ($array as $key => $value) {
  77. if (is_array($value)) {
  78. $results = array_merge($results, static::arrayMerge($value, $prepend.$key.'.'));
  79. }
  80. else {
  81. if (is_bool($value)) {
  82. $results[$prepend.$key] = json_encode($value);
  83. } else {
  84. $results[$prepend.$key] = $value;
  85. }
  86. }
  87. }
  88. return $results;
  89. }
  90. abstract public function deserialize($param);
  91. /**
  92. * @param string $jsonString json格式的字符串
  93. */
  94. public function fromJsonString($jsonString)
  95. {
  96. $arr = json_decode($jsonString, true);
  97. $this->deserialize($arr);
  98. }
  99. public function toJsonString()
  100. {
  101. $r = $this->serialize();
  102. // it is an object rather than an array
  103. if (empty($r)) {
  104. return "{}";
  105. }
  106. return json_encode($r, JSON_UNESCAPED_UNICODE);
  107. }
  108. public function __call($member, $param)
  109. {
  110. $act = substr($member,0,3);
  111. $attr = substr($member,3);
  112. if ($act === "get") {
  113. return $this->$attr;
  114. } else if ($act === "set") {
  115. $this->$attr = $param[0];
  116. }
  117. }
  118. }