diff --git a/src/Services/Generators/ColumnsGenerator.php b/src/Services/Generators/ColumnsGenerator.php index 32d7878..84c776d 100644 --- a/src/Services/Generators/ColumnsGenerator.php +++ b/src/Services/Generators/ColumnsGenerator.php @@ -6,15 +6,18 @@ use Doctrine\DBAL\Schema\Column; use Illuminate\Database\Connection; use Illuminate\Database\Eloquent\Model; +use ReflectionClass; class ColumnsGenerator implements PhpDocGeneratorContract { + protected AbstractSchemaManager $schema; protected Model $model; public function __construct( Connection $databaseConnection - ) { + ) + { $this->schema = $databaseConnection->getDoctrineSchemaManager(); } @@ -30,10 +33,11 @@ public function generate(Model $model, array $options = []): string // columns foreach ($columns as $column) { $phpDocStr .= sprintf( - '%s * @property %s %s', + '%s * @property %s %s %s', "\n", $this->resolveColumnType($column), - '$' . $column->getName() + '$' . $column->getName(), + $column->getComment(), ); } @@ -50,27 +54,34 @@ protected function resolveColumnType(Column $column): string { $columnType = strtolower($column->getType()->getName()); - $type = match ($columnType) { - 'int', 'smallint', 'tinyint', - 'mediumint', 'bigint', 'integer' => 'int', - 'float', 'double', 'decimal', 'dec' => 'float', + $cast = $this->model->getCasts()[$column->getName()] ?? null; + if (class_exists($cast) && (new ReflectionClass($cast))->isEnum()) { + $type = "\\${cast}"; + } + + if (!isset($type)) { + $type = match ($columnType) { + 'int', 'smallint', 'tinyint', + 'mediumint', 'bigint', 'integer' => 'int', + 'float', 'double', 'decimal', 'dec' => 'float', - 'bool', 'boolean' => 'bool', + 'bool', 'boolean' => 'bool', - // would be string if you don't add 'casts' - 'date', 'datetime', 'timestamp', 'time', 'year' => $this->hasDateCasting($column->getName()) - ? '\Carbon\Carbon' - : 'string', + // would be string if you don't add 'casts' + 'date', 'datetime', 'timestamp', 'time', 'year' => $this->hasDateCasting($column->getName()) + ? '\Carbon\Carbon' + : 'string', - 'char', 'string', 'varchar', - 'text', 'tinytext', 'mediumtext', - 'longtext', 'enum', 'binary', - 'varbinary', 'set', 'json', 'jsonb' => $this->getJsonCastType($column->getName()), - // ^ because sqlite doesn't have json, they will use `text` but Eloquent can parse text to particular cast + 'char', 'string', 'varchar', + 'text', 'tinytext', 'mediumtext', + 'longtext', 'enum', 'binary', + 'varbinary', 'set', 'json', 'jsonb' => $this->getJsonCastType($column->getName()), + // ^ because sqlite doesn't have json, they will use `text` but Eloquent can parse text to particular cast - // would be string if you don't add 'casts', default to array - default => 'mixed', - }; + // would be string if you don't add 'casts', default to array + default => 'mixed', + }; + } if (!$column->getNotnull()) { $type .= '|null'; @@ -104,4 +115,5 @@ protected function getJsonCastType(string $column): string return 'string'; } -} \ No newline at end of file + +}