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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace Tqdev\PhpCrudApi\Column\Reflection;
  3. use Tqdev\PhpCrudApi\Database\GenericReflection;
  4. class ReflectedColumn implements \JsonSerializable
  5. {
  6. public const DEFAULT_LENGTH = 255;
  7. public const DEFAULT_PRECISION = 19;
  8. public 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 = (int) $columnResult['CHARACTER_MAXIMUM_LENGTH'];
  33. $type = $reflection->toJdbcType($columnResult['DATA_TYPE'], $length);
  34. $precision = (int) $columnResult['NUMERIC_PRECISION'];
  35. $scale = (int) $columnResult['NUMERIC_SCALE'];
  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 isInteger(): bool
  108. {
  109. return in_array($this->type, ['integer', 'bigint', 'smallint', 'tinyint']);
  110. }
  111. public function setPk($value) /*: void*/
  112. {
  113. $this->pk = $value;
  114. }
  115. public function getPk(): bool
  116. {
  117. return $this->pk;
  118. }
  119. public function setFk($value) /*: void*/
  120. {
  121. $this->fk = $value;
  122. }
  123. public function getFk(): string
  124. {
  125. return $this->fk;
  126. }
  127. public function serialize()
  128. {
  129. return [
  130. 'name' => $this->name,
  131. 'type' => $this->type,
  132. 'length' => $this->length,
  133. 'precision' => $this->precision,
  134. 'scale' => $this->scale,
  135. 'nullable' => $this->nullable,
  136. 'pk' => $this->pk,
  137. 'fk' => $this->fk,
  138. ];
  139. }
  140. public function jsonSerialize()
  141. {
  142. return array_filter($this->serialize());
  143. }
  144. }