You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LuDbo.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace Luticate\Utils\Dbo;
  3. use Luticate\Utils\Business\LuPropertyDocParser;
  4. use Luticate\Utils\Business\LuStringUtils;
  5. abstract class LuDbo implements \JsonSerializable {
  6. /**
  7. * @return string
  8. */
  9. public function __toString()
  10. {
  11. return json_encode($this);
  12. }
  13. public function jsonSerialize()
  14. {
  15. $json = [];
  16. $reflect = new \ReflectionClass(static::class);
  17. $properties = $reflect->getProperties();
  18. foreach ($properties as $property) {
  19. $name = LuStringUtils::stringSnakeToCamelCase($property->getName(), true);
  20. $property->setAccessible(true);
  21. $value = $property->getValue($this);
  22. if (is_array($value)) {
  23. $array = [];
  24. foreach ($value as $key => $v) {
  25. if ($v instanceof LuDbo) {
  26. $array[$key] = $v->jsonSerialize();
  27. }
  28. else {
  29. $array[$key] = $v;
  30. }
  31. }
  32. $json[$name] = $array;
  33. }
  34. else {
  35. $value = $property->getValue($this);
  36. if ($value instanceof LuDbo) {
  37. $json[$name] = $value->jsonSerialize();
  38. }
  39. else {
  40. $json[$name] = $value;
  41. }
  42. }
  43. }
  44. return $json;
  45. }
  46. /**
  47. * @param $newClass LuDbo
  48. * @return LuDbo
  49. */
  50. public function castAs($newClass) {
  51. $obj = new $newClass;
  52. foreach (get_object_vars($this) as $key => $name) {
  53. $obj->$key = $name;
  54. }
  55. return $obj;
  56. }
  57. public static function deserializeValue($value, $type) {
  58. if (is_null($value)) {
  59. return null;
  60. }
  61. if (is_null($type) || $type == "") {
  62. $type = "mixed";
  63. }
  64. else if ($type == "array") {
  65. $type = "mixed[]";
  66. }
  67. if ($type == "string") {
  68. $dbo = LuStringDbo::jsonDeserialize($value);
  69. return $dbo->getString();
  70. }
  71. else if ($type == "int") {
  72. $dbo = LuIntDbo::jsonDeserialize($value);
  73. return $dbo->getInt();
  74. }
  75. else if ($type == "float") {
  76. $dbo = LuFloatDbo::jsonDeserialize($value);
  77. return $dbo->getFloat();
  78. }
  79. else if ($type == "bool") {
  80. $dbo = LuBoolDbo::jsonDeserialize($value);
  81. return $dbo->getBool();
  82. }
  83. else if ($type == "mixed") {
  84. return $value;
  85. }
  86. else if (LuStringUtils::endsWith($type, "[]")) {
  87. if (!is_array($value)) {
  88. throw new LuDboDeserializeException("Invalid array value");
  89. }
  90. $type = substr($type, 0, strlen($type) - 2);
  91. $data = [];
  92. foreach ($value as $v) {
  93. $data[] = self::deserializeValue($v, $type);
  94. }
  95. return $data;
  96. }
  97. else {
  98. return call_user_func_array(array($type, "jsonDeserialize"), array($value));
  99. }
  100. }
  101. /**
  102. * Deserialize from a JSON object
  103. * @param $json mixed The JSON data to deserialize
  104. * @return LuDbo
  105. */
  106. public static function jsonDeserialize($json)
  107. {
  108. if (is_null($json)) {
  109. return null;
  110. }
  111. $dbo = new static();
  112. $reflect = new \ReflectionClass(static::class);
  113. $properties = $reflect->getProperties();
  114. foreach ($properties as $property) {
  115. $parser = new LuPropertyDocParser($property->getDocComment());
  116. $name = LuStringUtils::stringSnakeToCamelCase($property->getName(), true);
  117. $doc = $parser->parse();
  118. $type = is_null($doc) ? null : $doc->getType();
  119. if (isset($json[$name])) {
  120. $value = static::deserializeValue($json[$name], $type);
  121. $property->setAccessible(true);
  122. $property->setValue($dbo, $value);
  123. }
  124. }
  125. return $dbo;
  126. }
  127. /**
  128. * Generate a sample JSON object for the DBO
  129. * @return array
  130. */
  131. public static function generateSample()
  132. {
  133. return null;
  134. }
  135. }