From 4b47c0b61c60eac4ef2cbb35057cb71e83e9cb59 Mon Sep 17 00:00:00 2001 From: leo108 Date: Fri, 19 Aug 2016 07:35:07 +0800 Subject: [PATCH] add support for $casts --- src/Console/ModelsCommand.php | 55 +++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/src/Console/ModelsCommand.php b/src/Console/ModelsCommand.php index 07f724e..d1324fc 100644 --- a/src/Console/ModelsCommand.php +++ b/src/Console/ModelsCommand.php @@ -192,6 +192,7 @@ class ModelsCommand extends Command if ($hasDoctrine) { $this->getPropertiesFromTable($model); + $this->castPropertiesType($model); } $this->getPropertiesFromMethods($model); @@ -229,6 +230,60 @@ class ModelsCommand extends Command return $models; } + /** + * cast the properties's type from $casts. + * + * @param \Illuminate\Database\Eloquent\Model $model + */ + protected function castPropertiesType($model) + { + $casts = $model->getCasts(); + foreach ($casts as $name => $type) { + switch ($type) { + case 'boolean': + case 'bool': + $realType = 'boolean'; + break; + case 'string': + $realType = 'string'; + break; + case 'array': + case 'json': + $realType = 'array'; + break; + case 'object': + $realType = 'object'; + break; + case 'int': + case 'integer': + case 'timestamp': + $realType = 'integer'; + break; + case 'real': + case 'double': + case 'float': + $realType = 'float'; + break; + case 'date': + case 'datetime': + $realType = '\Carbon\Carbon'; + break; + case 'collection': + $realType = '\Illuminate\Support\Collection'; + break; + default: + $realType = 'mixed'; + break; + } + + if (!isset($this->properties[$name])) { + continue; + } else { + $this->properties[$name]['type'] = $realType; + } + } + } + /** * Load the properties from the database table. *