2
0

Column.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace PhpDevCommunity\PaperORM\Mapping\Column;
  3. use PhpDevCommunity\PaperORM\Mapping\Index;
  4. use PhpDevCommunity\PaperORM\Tools\NamingStrategy;
  5. use PhpDevCommunity\PaperORM\Types\StringType;
  6. use PhpDevCommunity\PaperORM\Types\Type;
  7. use PhpDevCommunity\PaperORM\Types\TypeFactory;
  8. abstract class Column
  9. {
  10. private string $property;
  11. private ?string $name;
  12. private string $type;
  13. private bool $unique;
  14. private bool $nullable;
  15. private $defaultValue;
  16. private ?string $firstArgument;
  17. private ?string $secondArgument;
  18. private ?Index $index = null;
  19. public function __construct(
  20. string $property,
  21. ?string $name = null,
  22. string $type = StringType::class,
  23. bool $nullable = false,
  24. $defaultValue = null,
  25. bool $unique = false,
  26. ?string $firstArgument = null,
  27. ?string $secondArgument = null
  28. )
  29. {
  30. if (empty($property) && !empty($name)) {
  31. $property = $name;
  32. }
  33. if (!empty($name) && !preg_match('/^[a-zA-Z0-9_]+$/', $name)) {
  34. throw new \InvalidArgumentException(sprintf(
  35. 'Invalid column name "%s": only alphanumeric characters and underscores are allowed.',
  36. $name
  37. ));
  38. }
  39. $this->property = $property;
  40. $this->name = $name;
  41. $this->type = $type;
  42. $this->defaultValue = $defaultValue;
  43. $this->unique = $unique;
  44. $this->nullable = $nullable;
  45. $this->firstArgument = $firstArgument;
  46. $this->secondArgument = $secondArgument;
  47. }
  48. final public function __toString(): string
  49. {
  50. return $this->getProperty();
  51. }
  52. public function bindProperty(string $propertyName): self
  53. {
  54. $this->property = $propertyName;
  55. return $this;
  56. }
  57. public function getProperty(): string
  58. {
  59. if (empty($this->property)) {
  60. throw new \LogicException('Property must be set, use bindProperty');
  61. }
  62. return $this->property;
  63. }
  64. final public function getName(): ?string
  65. {
  66. $property = $this->getProperty();
  67. return $this->name ?: NamingStrategy::toSnakeCase($property);
  68. }
  69. public function getType(): string
  70. {
  71. return $this->type;
  72. }
  73. final public function type(string $type): self
  74. {
  75. $this->type = $type;
  76. return $this;
  77. }
  78. public function isUnique(): bool
  79. {
  80. return $this->unique;
  81. }
  82. public function isNullable(): bool
  83. {
  84. return $this->nullable;
  85. }
  86. final public function getFirstArgument(): ?string
  87. {
  88. return $this->firstArgument;
  89. }
  90. final public function getSecondArgument(): ?string
  91. {
  92. return $this->secondArgument;
  93. }
  94. public function getDefaultValue()
  95. {
  96. return $this->defaultValue;
  97. }
  98. public function getDefaultValueToDatabase()
  99. {
  100. return $this->convertToDatabase($this->getDefaultValue());
  101. }
  102. public function getIndex(): ?Index
  103. {
  104. if ($this->index === null && ($this instanceof JoinColumn || $this->isUnique())) {
  105. $this->index = new Index([$this->getName()], $this->isUnique());
  106. }
  107. return $this->index;
  108. }
  109. /**
  110. * Converts a value to its corresponding database representation.
  111. *
  112. * @param mixed $value The value to be converted.
  113. * @return mixed The converted value.
  114. * @throws \ReflectionException
  115. */
  116. final function convertToDatabase($value)
  117. {
  118. $type = $this->getType();
  119. if (is_subclass_of($type, Type::class)) {
  120. $value = TypeFactory::create($type)->convertToDatabase($value);
  121. }
  122. return $value;
  123. }
  124. /**
  125. * Converts a value to its corresponding PHP representation.
  126. *
  127. * @param mixed $value The value to be converted.
  128. * @return mixed The converted PHP value.
  129. * @throws \ReflectionException
  130. */
  131. final function convertToPHP($value)
  132. {
  133. $type = $this->getType();
  134. if (is_subclass_of($type, Type::class)) {
  135. $value = TypeFactory::create($type)->convertToPHP($value);
  136. }
  137. return $value;
  138. }
  139. }