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 5.4KB

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