| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- namespace PhpDevCommunity\PaperORM\Mapping\Column;
- use PhpDevCommunity\PaperORM\Mapping\Index;
- use PhpDevCommunity\PaperORM\Tools\NamingStrategy;
- use PhpDevCommunity\PaperORM\Types\StringType;
- use PhpDevCommunity\PaperORM\Types\Type;
- use PhpDevCommunity\PaperORM\Types\TypeFactory;
- abstract class Column
- {
- private string $property;
- private ?string $name;
- private string $type;
- private bool $unique;
- private bool $nullable;
- private $defaultValue;
- private ?string $firstArgument;
- private ?string $secondArgument;
- private ?Index $index = null;
- public function __construct(
- string $property,
- ?string $name = null,
- string $type = StringType::class,
- bool $nullable = false,
- $defaultValue = null,
- bool $unique = false,
- ?string $firstArgument = null,
- ?string $secondArgument = null
- )
- {
- if (empty($property) && !empty($name)) {
- $property = $name;
- }
- if (!empty($name) && !preg_match('/^[a-zA-Z0-9_]+$/', $name)) {
- throw new \InvalidArgumentException(sprintf(
- 'Invalid column name "%s": only alphanumeric characters and underscores are allowed.',
- $name
- ));
- }
- $this->property = $property;
- $this->name = $name;
- $this->type = $type;
- $this->defaultValue = $defaultValue;
- $this->unique = $unique;
- $this->nullable = $nullable;
- $this->firstArgument = $firstArgument;
- $this->secondArgument = $secondArgument;
- }
- final public function __toString(): string
- {
- return $this->getProperty();
- }
- public function bindProperty(string $propertyName): self
- {
- $this->property = $propertyName;
- return $this;
- }
- public function getProperty(): string
- {
- if (empty($this->property)) {
- throw new \LogicException('Property must be set, use bindProperty');
- }
- return $this->property;
- }
- final public function getName(): ?string
- {
- $property = $this->getProperty();
- return $this->name ?: NamingStrategy::toSnakeCase($property);
- }
- public function getType(): string
- {
- return $this->type;
- }
- final public function type(string $type): self
- {
- $this->type = $type;
- return $this;
- }
- public function isUnique(): bool
- {
- return $this->unique;
- }
- public function isNullable(): bool
- {
- return $this->nullable;
- }
- final public function getFirstArgument(): ?string
- {
- return $this->firstArgument;
- }
- final public function getSecondArgument(): ?string
- {
- return $this->secondArgument;
- }
- public function getDefaultValue()
- {
- return $this->defaultValue;
- }
- public function getDefaultValueToDatabase()
- {
- return $this->convertToDatabase($this->getDefaultValue());
- }
- public function getIndex(): ?Index
- {
- if ($this->index === null && ($this instanceof JoinColumn || $this->isUnique())) {
- $this->index = new Index([$this->getName()], $this->isUnique());
- }
- return $this->index;
- }
- /**
- * Converts a value to its corresponding database representation.
- *
- * @param mixed $value The value to be converted.
- * @return mixed The converted value.
- * @throws \ReflectionException
- */
- final function convertToDatabase($value)
- {
- $type = $this->getType();
- if (is_subclass_of($type, Type::class)) {
- $value = TypeFactory::create($type)->convertToDatabase($value);
- }
- return $value;
- }
- /**
- * Converts a value to its corresponding PHP representation.
- *
- * @param mixed $value The value to be converted.
- * @return mixed The converted PHP value.
- * @throws \ReflectionException
- */
- final function convertToPHP($value)
- {
- $type = $this->getType();
- if (is_subclass_of($type, Type::class)) {
- $value = TypeFactory::create($type)->convertToPHP($value);
- }
- return $value;
- }
- }
|