選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

LuDbo.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. * @throws LuDboConstraintException
  106. * @throws LuDboDeserializeException
  107. */
  108. public static function jsonDeserialize($json)
  109. {
  110. if (is_null($json)) {
  111. return null;
  112. }
  113. $dbo = new static();
  114. $reflect = new \ReflectionClass(static::class);
  115. $properties = $reflect->getProperties();
  116. foreach ($properties as $property) {
  117. $parser = new LuPropertyDocParser($property->getDocComment());
  118. $name = LuStringUtils::stringSnakeToCamelCase($property->getName(), true);
  119. $doc = $parser->parse();
  120. $type = is_null($doc) ? null : $doc->getType();
  121. $value = null;
  122. if (isset($json[$name])) {
  123. $value = static::deserializeValue($json[$name], $type);
  124. }
  125. if ($doc->isNotNull() && is_null($value)) {
  126. throw new LuDboConstraintException("Field '" . $name . "' can not be null");
  127. }
  128. if (!is_null($doc) && !is_null($value)) {
  129. foreach ($doc->getConstraints() as $constraint) {
  130. call_user_func_array([$value, $constraint->getMethod()], $constraint->getArguments());
  131. }
  132. }
  133. $property->setAccessible(true);
  134. $property->setValue($dbo, $value);
  135. }
  136. return $dbo;
  137. }
  138. /**
  139. * Generate a sample JSON object for the DBO
  140. * @return array
  141. */
  142. public static function generateSample()
  143. {
  144. return null;
  145. }
  146. }