Skip to content

Commit

Permalink
Merge pull request #394 from karmicdice/master
Browse files Browse the repository at this point in the history
Enabling decimal through cake bake and infering types latitude and longitude
  • Loading branch information
markstory authored Jul 22, 2019
2 parents e5a92d9 + ea9270b commit 38fbee6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Template/Bake/config/skeleton.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/

$wantedOptions = array_flip(['length', 'limit', 'default', 'unsigned', 'null', 'comment', 'autoIncrement']);
$wantedOptions = array_flip(['length', 'limit', 'default', 'unsigned', 'null', 'comment', 'autoIncrement', 'precision', 'scale']);
$tableMethod = $this->Migration->tableMethod($action);
$columnMethod = $this->Migration->columnMethod($action);
$indexMethod = $this->Migration->indexMethod($action);
Expand Down
34 changes: 28 additions & 6 deletions src/Util/ColumnParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,26 @@ class ColumnParser
*
* @var string
*/
protected $regexpParseColumn = '/^(\w*)(?::(\w*\??\[?\d*\]?))?(?::(\w*))?(?::(\w*))?/';
protected $regexpParseColumn = '/
^
(\w+)
(?::(\w+\??
(?:\[
(?:[0-9]|[1-9][0-9]+)
(?:,(?:[0-9]|[1-9][0-9]+))?
\])?
))?
(?::(\w+))?
(?::(\w+))?
$
/x';

/**
* Regex used to parse the field type and length
*
* @var string
*/
protected $regexpParseField = '/(\w+\??)\[(\d+)\]/';
protected $regexpParseField = '/(\w+\??)\[([0-9,]+)\]/';

/**
* Parses a list of arguments into an array of fields
Expand All @@ -50,8 +62,7 @@ public function parseFields($arguments)
$type = 'primary';
}
}

$nullable = (bool)preg_match('/\w+\?(\[\d+\])?/', $type);
$nullable = (bool)strpos($type, '?');
$type = $nullable ? str_replace('?', '', $type) : $type;

list($type, $length) = $this->getTypeAndLength($field, $type);
Expand All @@ -64,7 +75,11 @@ public function parseFields($arguments)
];

if ($length !== null) {
$fields[$field]['options']['limit'] = $length;
if (is_array($length)) {
list($fields[$field]['options']['precision'], $fields[$field]['options']['scale']) = $length;
} else {
$fields[$field]['options']['limit'] = $length;
}
}

if ($isPrimaryKey === true && $type === 'integer') {
Expand Down Expand Up @@ -172,6 +187,10 @@ public function validArguments($arguments)
public function getTypeAndLength($field, $type)
{
if (preg_match($this->regexpParseField, $type, $matches)) {
if (strpos($matches[2], ',') !== false) {
$matches[2] = explode(',', $matches[2]);
}

return [$matches[1], $matches[2]];
}

Expand All @@ -196,7 +215,6 @@ public function getType($field, $type)
$validTypes = $collection->filter(function ($value, $constant) {
return substr($constant, 0, strlen('PHINX_TYPE_')) === 'PHINX_TYPE_';
})->toArray();

$fieldType = $type;
if ($type === null || !in_array($type, $validTypes)) {
if ($type === 'primary') {
Expand All @@ -205,6 +223,8 @@ public function getType($field, $type)
$fieldType = 'integer';
} elseif (in_array($field, ['created', 'modified', 'updated']) || substr($field, -3) === '_at') {
$fieldType = 'datetime';
} elseif (in_array($field, ['latitude', 'longitude'])) {
$fieldType = 'decimal';
} else {
$fieldType = 'string';
}
Expand All @@ -228,6 +248,8 @@ public function getLength($type)
$length = 11;
} elseif ($type === 'biginteger') {
$length = 20;
} elseif ($type === 'decimal') {
$length = [10, 6];
}

return $length;
Expand Down
35 changes: 33 additions & 2 deletions tests/TestCase/Util/ColumnParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,25 @@ public function testParseFields()
'default' => null,
],
],
], $this->columnParser->parseFields(['id', 'created', 'modified', 'updated', 'deleted_at']));
'latitude' => [
'columnType' => 'decimal',
'options' => [
'default' => false,
'null' => false,
'precision' => 10,
'scale' => 6,
]
],
'longitude' => [
'columnType' => 'decimal',
'options' => [
'default' => false,
'null' => false,
'precision' => 10,
'scale' => 6,
]
],
], $this->columnParser->parseFields(['id', 'created', 'modified', 'updated', 'deleted_at', 'latitude', 'longitude']));

$expected = [
'id' => [
Expand Down Expand Up @@ -151,8 +169,17 @@ public function testParseFields()
'limit' => 11,
],
],
'amount' => [
'columnType' => 'decimal',
'options' => [
'null' => true,
'default' => null,
'precision' => 6,
'scale' => 3,
],
],
];
$actual = $this->columnParser->parseFields(['id', 'name:string', 'description:string?', 'age:integer?']);
$actual = $this->columnParser->parseFields(['id', 'name:string', 'description:string?', 'age:integer?', 'amount:decimal?[6,3]']);
$this->assertEquals($expected, $actual);

$expected = [
Expand Down Expand Up @@ -311,6 +338,8 @@ public function testGetType()
$this->assertEquals('string', $this->columnParser->getType('some_field', 'string'));
$this->assertEquals('boolean', $this->columnParser->getType('field', 'boolean'));
$this->assertEquals('polygon', $this->columnParser->getType('field', 'polygon'));
$this->assertEquals('decimal', $this->columnParser->getType('latitude', null));
$this->assertEquals('decimal', $this->columnParser->getType('longitude', null));
}

/**
Expand All @@ -327,6 +356,7 @@ public function testGetTypeAndLength()
$this->assertEquals(['string', 255], $this->columnParser->getTypeAndLength('username', null));
$this->assertEquals(['datetime', null], $this->columnParser->getTypeAndLength('created', null));
$this->assertEquals(['datetime', null], $this->columnParser->getTypeAndLength('changed_at', null));
$this->assertEquals(['decimal', [10, 6]], $this->columnParser->getTypeAndLength('latitude', 'decimal[10,6]'));
}

/**
Expand All @@ -337,6 +367,7 @@ public function testGetLength()
$this->assertEquals(255, $this->columnParser->getLength('string'));
$this->assertEquals(11, $this->columnParser->getLength('integer'));
$this->assertEquals(20, $this->columnParser->getLength('biginteger'));
$this->assertEquals([10, 6], $this->columnParser->getLength('decimal'));
$this->assertNull($this->columnParser->getLength('text'));
}

Expand Down

0 comments on commit 38fbee6

Please sign in to comment.