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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 endsWith($haystack, $needle) {
  58. return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0
  59. && strpos($haystack, $needle, $temp) !== false);
  60. }
  61. public static function deserializeValue($value, $type) {
  62. if (is_null($value)) {
  63. return null;
  64. }
  65. if (is_null($type) || $type == "") {
  66. $type = "mixed";
  67. }
  68. else if ($type == "array") {
  69. $type = "mixed[]";
  70. }
  71. if ($type == "string") {
  72. $dbo = LuStringDbo::jsonDeserialize($value);
  73. return $dbo->getString();
  74. }
  75. else if ($type == "int") {
  76. $dbo = LuIntDbo::jsonDeserialize($value);
  77. return $dbo->getInt();
  78. }
  79. else if ($type == "float") {
  80. $dbo = LuFloatDbo::jsonDeserialize($value);
  81. return $dbo->getFloat();
  82. }
  83. else if ($type == "bool") {
  84. $dbo = LuBoolDbo::jsonDeserialize($value);
  85. return $dbo->getBool();
  86. }
  87. else if ($type == "mixed") {
  88. return $value;
  89. }
  90. else if (self::endsWith($type, "[]")) {
  91. if (!is_array($value)) {
  92. throw new LuDboDeserializeException("Invalid array value");
  93. }
  94. $type = substr($type, 0, strlen($type) - 2);
  95. $data = [];
  96. foreach ($value as $v) {
  97. $data[] = self::deserializeValue($v, $type);
  98. }
  99. return $data;
  100. }
  101. else {
  102. return call_user_func_array(array($type, "jsonDeserialize"), array($value));
  103. }
  104. }
  105. /**
  106. * Deserialize from a JSON object
  107. * @param $json mixed The JSON data to deserialize
  108. * @return LuDbo
  109. */
  110. public static function jsonDeserialize($json)
  111. {
  112. $dbo = new static();
  113. $reflect = new \ReflectionClass(static::class);
  114. $properties = $reflect->getProperties();
  115. foreach ($properties as $property) {
  116. $parser = new LuPropertyDocParser($property->getDocComment());
  117. $name = LuStringUtils::stringSnakeToCamelCase($property->getName(), true);
  118. $doc = $parser->parse();
  119. $type = is_null($doc) ? null : $doc->getType();
  120. if (isset($json[$name])) {
  121. $value = static::deserializeValue($json[$name], $type);
  122. $property->setAccessible(true);
  123. $property->setValue($dbo, $value);
  124. }
  125. }
  126. return $dbo;
  127. }
  128. /**
  129. * Generate a sample JSON object for the DBO
  130. * @return array
  131. */
  132. public static function generateSample()
  133. {
  134. return null;
  135. }
  136. }