2
0

ColumnMetadata.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace PhpDevCommunity\PaperORM\Metadata;
  3. use PhpDevCommunity\PaperORM\Entity\EntityInterface;
  4. use PhpDevCommunity\PaperORM\Mapper\EntityMapper;
  5. use PhpDevCommunity\PaperORM\Mapping\Column\Column;
  6. use PhpDevCommunity\PaperORM\Mapping\Column\JoinColumn;
  7. use PhpDevCommunity\PaperORM\Mapping\Column\PrimaryKeyColumn;
  8. class ColumnMetadata
  9. {
  10. private string $name;
  11. private string $type;
  12. private bool $isPrimary;
  13. private array $foreignKeyMetadata;
  14. private bool $isNullable;
  15. private $defaultValue;
  16. private ?string $comment;
  17. private array $attributes;
  18. private ?IndexMetadata $indexMetadata;
  19. public function __construct(
  20. string $name,
  21. string $type,
  22. bool $isPrimary = false,
  23. array $foreignKeyMetadata = [],
  24. bool $isNullable = true,
  25. $defaultValue = null,
  26. ?string $comment = null,
  27. array $attributes = []
  28. )
  29. {
  30. $this->name = $name;
  31. $this->type = strtoupper($type);
  32. $this->isPrimary = $isPrimary;
  33. $this->foreignKeyMetadata = $foreignKeyMetadata;
  34. $this->isNullable = $isNullable;
  35. $this->defaultValue = $defaultValue;
  36. $this->comment = $comment;
  37. $this->attributes = $attributes;
  38. }
  39. // Getters
  40. public function getName(): string
  41. {
  42. return $this->name;
  43. }
  44. public function getType(): string
  45. {
  46. return $this->type;
  47. }
  48. public function getTypeWithAttributes(): string
  49. {
  50. if (!empty($this->attributes)) {
  51. return sprintf('%s(%s)', $this->getType(), implode(',', $this->attributes));
  52. }
  53. return $this->getType();
  54. }
  55. public function isPrimary(): bool
  56. {
  57. return $this->isPrimary;
  58. }
  59. public function getForeignKeyMetadata(): array
  60. {
  61. return $this->foreignKeyMetadata;
  62. }
  63. public function isNullable(): bool
  64. {
  65. return $this->isNullable;
  66. }
  67. public function getDefaultValue()
  68. {
  69. return $this->defaultValue;
  70. }
  71. public function getComment(): ?string
  72. {
  73. return $this->comment;
  74. }
  75. public function getAttributes(): array
  76. {
  77. return $this->attributes;
  78. }
  79. public static function fromColumn(Column $column, string $sqlType): self
  80. {
  81. $foreignKeyMetadata = [];
  82. if ($column instanceof JoinColumn) {
  83. $targetEntity = $column->getTargetEntity();
  84. if (is_subclass_of($targetEntity, EntityInterface::class)) {
  85. $foreignKeyMetadata = [
  86. 'referencedTable' => EntityMapper::getTable($targetEntity),
  87. 'referencedColumn' => $column->getReferencedColumnName(),
  88. ];
  89. }
  90. }
  91. $arguments = [];
  92. if ($column->getFirstArgument()) {
  93. $arguments[] = $column->getFirstArgument();
  94. }
  95. if ($column->getSecondArgument()) {
  96. $arguments[] = $column->getSecondArgument();
  97. }
  98. $defaultValue = $column->getDefaultValue();
  99. if (is_array($defaultValue)) {
  100. $defaultValue = json_encode($defaultValue);
  101. }
  102. return new self(
  103. $column->getName(),
  104. $sqlType,
  105. $column instanceof PrimaryKeyColumn,
  106. $foreignKeyMetadata,
  107. $column->isNullable(),
  108. $defaultValue,
  109. null,
  110. $arguments
  111. );
  112. }
  113. public static function fromArray(array $data): self
  114. {
  115. return new self(
  116. $data['name'],
  117. $data['type'],
  118. $data['primary'] ?? false,
  119. $data['foreignKeyMetadata'] ?? false,
  120. $data['null'] ?? true,
  121. $data['default'] ?? null,
  122. $data['comment'] ?? null,
  123. $data['attributes'] ?? []
  124. );
  125. }
  126. public function toArray(): array
  127. {
  128. return [
  129. 'name' => $this->getName(),
  130. 'type' => $this->getType(),
  131. 'primary' => $this->isPrimary(),
  132. 'foreignKeyMetadata' => $this->getForeignKeyMetadata(),
  133. 'null' => $this->isNullable(),
  134. 'default' => $this->getDefaultValue(),
  135. 'comment' => $this->getComment(),
  136. 'attributes' => $this->getAttributes(),
  137. ];
  138. }
  139. }