Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

LuDbo.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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" || $type == "double") {
  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 == "datetime" || $type == "date" || $type == "DateTime"
  91. || $type == 'Carbon\Carbon' || $type == 'Carbon') {
  92. $dbo = LuDateTimeDbo::jsonDeserialize($value);
  93. $dbo->checkConstraints($constraints);
  94. return $dbo->getDateTime();
  95. }
  96. else if ($type == "mixed") {
  97. return $value;
  98. }
  99. else if (LuStringUtils::endsWith($type, "[]")) {
  100. if (!is_array($value)) {
  101. throw new LuDboDeserializeException("Invalid array value");
  102. }
  103. $type = substr($type, 0, strlen($type) - 2);
  104. $data = [];
  105. foreach ($value as $key => $v) {
  106. $value = self::deserializeValue($v, $type, $constraints);
  107. $data[$key] = $value;
  108. }
  109. return $data;
  110. }
  111. else {
  112. $value = call_user_func_array(array($type, "jsonDeserialize"), array($value));
  113. $value->checkConstraints($constraints);
  114. return $value;
  115. }
  116. }
  117. /**
  118. * Deserialize from a JSON object
  119. * @param $json mixed The JSON data to deserialize
  120. * @return LuDbo
  121. * @throws LuDboConstraintException
  122. * @throws LuDboDeserializeException
  123. */
  124. public static function jsonDeserialize($json)
  125. {
  126. if (is_null($json)) {
  127. return null;
  128. }
  129. $dbo = new static();
  130. $reflect = new \ReflectionClass(static::class);
  131. $properties = $reflect->getProperties();
  132. foreach ($properties as $property) {
  133. $parser = new LuPropertyDocParser($property->getDocComment());
  134. $name = LuStringUtils::snakeToCamelCase($property->getName(), true);
  135. $doc = $parser->parse();
  136. $type = is_null($doc) ? null : $doc->getType();
  137. $value = null;
  138. if (isset($json[$name])) {
  139. $value = static::deserializeValue($json[$name], $type, $doc->getConstraints());
  140. }
  141. if ($doc->isNotNull() && is_null($value)) {
  142. throw new LuDboConstraintException("Field '" . $name . "' can not be null");
  143. }
  144. $property->setAccessible(true);
  145. $property->setValue($dbo, $value);
  146. }
  147. return $dbo;
  148. }
  149. /**
  150. * @param $constraints LuParameterConstraintDbo[]
  151. * @throws LuDboConstraintException
  152. */
  153. public function checkConstraints($constraints) {
  154. foreach ($constraints as $constraint) {
  155. if (!is_callable([$this, $constraint->getMethod()])) {
  156. throw new LuDboConstraintException("Constraint '" . $constraint->getMethod() . "' could not be found");
  157. }
  158. call_user_func_array([$this, $constraint->getMethod()], $constraint->getArguments());
  159. }
  160. }
  161. /**
  162. * Generate a sample JSON object for the DBO
  163. * @return array
  164. */
  165. public static function generateSample()
  166. {
  167. return null;
  168. }
  169. }