api de gestion de ticket, basé sur php-crud-api. Le but est de décorrélé les outils de gestion des données, afin
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.

ReflectedColumn.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace Tqdev\PhpCrudApi\Column\Reflection;
  3. use Tqdev\PhpCrudApi\Database\GenericReflection;
  4. class ReflectedColumn implements \JsonSerializable
  5. {
  6. const DEFAULT_LENGTH = 255;
  7. const DEFAULT_PRECISION = 19;
  8. const DEFAULT_SCALE = 4;
  9. private $name;
  10. private $type;
  11. private $length;
  12. private $precision;
  13. private $scale;
  14. private $nullable;
  15. private $pk;
  16. private $fk;
  17. public function __construct(String $name, String $type, int $length, int $precision, int $scale, bool $nullable, bool $pk, String $fk)
  18. {
  19. $this->name = $name;
  20. $this->type = $type;
  21. $this->length = $length;
  22. $this->precision = $precision;
  23. $this->scale = $scale;
  24. $this->nullable = $nullable;
  25. $this->pk = $pk;
  26. $this->fk = $fk;
  27. $this->sanitize();
  28. }
  29. public static function fromReflection(GenericReflection $reflection, array $columnResult): ReflectedColumn
  30. {
  31. $name = $columnResult['COLUMN_NAME'];
  32. $length = $columnResult['CHARACTER_MAXIMUM_LENGTH'] + 0;
  33. $type = $reflection->toJdbcType($columnResult['DATA_TYPE'], $length);
  34. $precision = $columnResult['NUMERIC_PRECISION'] + 0;
  35. $scale = $columnResult['NUMERIC_SCALE'] + 0;
  36. $nullable = in_array(strtoupper($columnResult['IS_NULLABLE']), ['TRUE', 'YES', 'T', 'Y', '1']);
  37. $pk = false;
  38. $fk = '';
  39. return new ReflectedColumn($name, $type, $length, $precision, $scale, $nullable, $pk, $fk);
  40. }
  41. public static function fromJson( /* object */$json): ReflectedColumn
  42. {
  43. $name = $json->name;
  44. $type = $json->type;
  45. $length = isset($json->length) ? $json->length : 0;
  46. $precision = isset($json->precision) ? $json->precision : 0;
  47. $scale = isset($json->scale) ? $json->scale : 0;
  48. $nullable = isset($json->nullable) ? $json->nullable : false;
  49. $pk = isset($json->pk) ? $json->pk : false;
  50. $fk = isset($json->fk) ? $json->fk : '';
  51. return new ReflectedColumn($name, $type, $length, $precision, $scale, $nullable, $pk, $fk);
  52. }
  53. private function sanitize()
  54. {
  55. $this->length = $this->hasLength() ? $this->getLength() : 0;
  56. $this->precision = $this->hasPrecision() ? $this->getPrecision() : 0;
  57. $this->scale = $this->hasScale() ? $this->getScale() : 0;
  58. }
  59. public function getName(): String
  60. {
  61. return $this->name;
  62. }
  63. public function getNullable(): bool
  64. {
  65. return $this->nullable;
  66. }
  67. public function getType(): String
  68. {
  69. return $this->type;
  70. }
  71. public function getLength(): int
  72. {
  73. return $this->length ?: self::DEFAULT_LENGTH;
  74. }
  75. public function getPrecision(): int
  76. {
  77. return $this->precision ?: self::DEFAULT_PRECISION;
  78. }
  79. public function getScale(): int
  80. {
  81. return $this->scale ?: self::DEFAULT_SCALE;
  82. }
  83. public function hasLength(): bool
  84. {
  85. return in_array($this->type, ['varchar', 'varbinary']);
  86. }
  87. public function hasPrecision(): bool
  88. {
  89. return $this->type == 'decimal';
  90. }
  91. public function hasScale(): bool
  92. {
  93. return $this->type == 'decimal';
  94. }
  95. public function isBinary(): bool
  96. {
  97. return in_array($this->type, ['blob', 'varbinary']);
  98. }
  99. public function isBoolean(): bool
  100. {
  101. return $this->type == 'boolean';
  102. }
  103. public function isGeometry(): bool
  104. {
  105. return $this->type == 'geometry';
  106. }
  107. public function setPk($value) /*: void*/
  108. {
  109. $this->pk = $value;
  110. }
  111. public function getPk(): bool
  112. {
  113. return $this->pk;
  114. }
  115. public function setFk($value) /*: void*/
  116. {
  117. $this->fk = $value;
  118. }
  119. public function getFk(): String
  120. {
  121. return $this->fk;
  122. }
  123. public function serialize()
  124. {
  125. return [
  126. 'name' => $this->name,
  127. 'type' => $this->type,
  128. 'length' => $this->length,
  129. 'precision' => $this->precision,
  130. 'scale' => $this->scale,
  131. 'nullable' => $this->nullable,
  132. 'pk' => $this->pk,
  133. 'fk' => $this->fk,
  134. ];
  135. }
  136. public function jsonSerialize()
  137. {
  138. return array_filter($this->serialize());
  139. }
  140. }