您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LuDbo.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace Luticate\Utils\Dbo;
  3. use Carbon\Carbon;
  4. use Luticate\Utils\Business\LuPropertyDocParser;
  5. use Luticate\Utils\Business\LuStringUtils;
  6. abstract class LuDbo implements \JsonSerializable {
  7. /**
  8. * @return string
  9. */
  10. public function __toString()
  11. {
  12. return json_encode($this);
  13. }
  14. /**
  15. * @return array
  16. */
  17. public function jsonSerialize()
  18. {
  19. $json = [];
  20. $reflect = new \ReflectionClass(static::class);
  21. $properties = $reflect->getProperties();
  22. foreach ($properties as $property) {
  23. $name = LuStringUtils::snakeToCamelCase($property->getName(), false);
  24. $property->setAccessible(true);
  25. $value = $property->getValue($this);
  26. $json[$name] = static::serializeValue($value);
  27. }
  28. return $json;
  29. }
  30. /**
  31. * @param $newClass LuDbo
  32. * @return LuDbo
  33. */
  34. public function castAs($newClass) {
  35. $obj = new $newClass;
  36. foreach (get_object_vars($this) as $key => $name) {
  37. $obj->$key = $name;
  38. }
  39. return $obj;
  40. }
  41. public static function serializeValue($value)
  42. {
  43. if (is_array($value)) {
  44. $array = [];
  45. foreach ($value as $key => $v) {
  46. $array[$key] = static::serializeValue($v);
  47. }
  48. return $array;
  49. }
  50. else {
  51. if ($value instanceof LuDbo) {
  52. return $value->jsonSerialize();
  53. }
  54. else if ($value instanceof Carbon) {
  55. return $value->__toString();
  56. }
  57. else {
  58. return $value;
  59. }
  60. }
  61. }
  62. public static function deserializeValue($value, $type, $constraints = []) {
  63. if (is_null($value)) {
  64. return null;
  65. }
  66. if (is_null($type) || $type == "") {
  67. $type = "mixed";
  68. }
  69. else if ($type == "array") {
  70. $type = "mixed[]";
  71. }
  72. if ($type == "string") {
  73. $dbo = LuStringDbo::jsonDeserialize($value);
  74. $dbo->checkConstraints($constraints);
  75. return $dbo->getString();
  76. }
  77. else if ($type == "int" || $type == "integer") {
  78. $dbo = LuIntDbo::jsonDeserialize($value);
  79. $dbo->checkConstraints($constraints);
  80. return $dbo->getInt();
  81. }
  82. else if ($type == "float" || $type == "double") {
  83. $dbo = LuFloatDbo::jsonDeserialize($value);
  84. $dbo->checkConstraints($constraints);
  85. return $dbo->getFloat();
  86. }
  87. else if ($type == "bool" || $type == "boolean") {
  88. $dbo = LuBoolDbo::jsonDeserialize($value);
  89. $dbo->checkConstraints($constraints);
  90. return $dbo->getBool();
  91. }
  92. else if ($type == "datetime" || $type == "date" || $type == "DateTime"
  93. || $type == 'Carbon\Carbon' || $type == 'Carbon') {
  94. $dbo = LuDateTimeDbo::jsonDeserialize($value);
  95. $dbo->checkConstraints($constraints);
  96. return $dbo->getDateTime();
  97. }
  98. else if ($type == "mixed") {
  99. return $value;
  100. }
  101. else if (LuStringUtils::endsWith($type, "[]")) {
  102. if (!is_array($value)) {
  103. throw new LuDboDeserializeException("Invalid array value");
  104. }
  105. $type = substr($type, 0, strlen($type) - 2);
  106. $data = [];
  107. foreach ($value as $key => $v) {
  108. $value = self::deserializeValue($v, $type, $constraints);
  109. $data[$key] = $value;
  110. }
  111. return $data;
  112. }
  113. else {
  114. $value = call_user_func_array(array($type, "jsonDeserialize"), array($value));
  115. $value->checkConstraints($constraints);
  116. return $value;
  117. }
  118. }
  119. /**
  120. * Deserialize from a JSON object
  121. * @param $json mixed The JSON data to deserialize
  122. * @return LuDbo
  123. * @throws LuDboConstraintException
  124. * @throws LuDboDeserializeException
  125. */
  126. public static function jsonDeserialize($json)
  127. {
  128. if (is_null($json)) {
  129. return null;
  130. }
  131. $dbo = new static();
  132. $reflect = new \ReflectionClass(static::class);
  133. $properties = $reflect->getProperties();
  134. foreach ($properties as $property) {
  135. $parser = new LuPropertyDocParser($property->getDocComment());
  136. $name = LuStringUtils::snakeToCamelCase($property->getName(), false);
  137. $doc = $parser->parse();
  138. $type = is_null($doc) ? null : $doc->getType();
  139. $value = null;
  140. if (isset($json[$name])) {
  141. $value = static::deserializeValue($json[$name], $type, $doc->getConstraints());
  142. }
  143. if ($doc->isNotNull() && is_null($value)) {
  144. throw new LuDboConstraintException("Field '" . $name . "' can not be null");
  145. }
  146. $property->setAccessible(true);
  147. $property->setValue($dbo, $value);
  148. }
  149. return $dbo;
  150. }
  151. /**
  152. * @param $constraints LuParameterConstraintDbo[]
  153. * @throws LuDboConstraintException
  154. */
  155. public function checkConstraints($constraints) {
  156. foreach ($constraints as $constraint) {
  157. if (!is_callable([$this, $constraint->getMethod()])) {
  158. throw new LuDboConstraintException("Constraint '" . $constraint->getMethod() . "' could not be found");
  159. }
  160. call_user_func_array([$this, $constraint->getMethod()], $constraint->getArguments());
  161. }
  162. }
  163. /**
  164. * Generate a sample JSON object for the DBO
  165. * @return array
  166. */
  167. public static function generateSample()
  168. {
  169. return null;
  170. }
  171. }