Skip to content

Commit

Permalink
Make sure arguments can be passed to types
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Feb 26, 2015
1 parent 6e7b498 commit a2691cf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
15 changes: 11 additions & 4 deletions spec/Migrations/SchemaParserSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,29 @@ function it_is_initializable()
function it_parses_a_basic_string_schema()
{
$this->parse('name:string')->shouldReturn([
['name' => 'name', 'type' => 'string', 'options' => []]
['name' => 'name', 'type' => 'string', 'arguments' => [], 'options' => []]
]);
}

function it_parses_schema_with_multiple_fields()
{
$this->parse('name:string, age:integer')->shouldReturn([
['name' => 'name', 'type' => 'string', 'options' => []],
['name' => 'age', 'type' => 'integer', 'options' => []],
['name' => 'name', 'type' => 'string', 'arguments' => [], 'options' => []],
['name' => 'age', 'type' => 'integer', 'arguments' => [], 'options' => []],
]);
}

function it_parses_schema_that_includes_extras()
{
$this->parse('age:integer:nullable:default(21)')->shouldReturn([
['name' => 'age', 'type' => 'integer', 'options' => ['nullable' => true, 'default' => '21']]
['name' => 'age', 'type' => 'integer', 'arguments' => [], 'options' => ['nullable' => true, 'default' => '21']]
]);
}

function it_parses_correctly_when_the_type_contains_method_arguments()
{
$this->parse('amount:decimal(5,2)')->shouldReturn([
['name' => 'amount', 'type' => 'decimal', 'arguments' => ['5', '2'], 'options' => []]
]);
}
}
3 changes: 2 additions & 1 deletion spec/Migrations/SyntaxBuilderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function it_creates_the_php_syntax_for_the_schema()
$schema = [[
"name" => "email",
"type" => "string",
"arguments" => ["100"],
"options" => [
"unique" => true,
"nullable" => true,
Expand All @@ -34,7 +35,7 @@ function getStub() {
return <<<EOT
Schema::create('{{table}}', function(Blueprint \$table) {
\$table->increments('id');
\$table->string('email')->unique()->nullable()->default("foo@example.com");
\$table->string('email', 100)->unique()->nullable()->default("foo@example.com");
\$table->timestamps();
});
EOT;
Expand Down
20 changes: 14 additions & 6 deletions src/Migrations/SchemaParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function parse($schema)
*/
private function getFields($schema)
{
return preg_split('/\s?,\s?/', $schema);
return preg_split('/\s?,\s/', $schema);
}

/**
Expand All @@ -47,11 +47,19 @@ private function getDetails($field)
{
$segments = explode(':', $field);

return [
'name' => array_shift($segments),
'type' => array_shift($segments),
'options' => $this->parseOptions($segments)
];
$name = array_shift($segments);
$type = array_shift($segments);
$arguments = [];
$options = $this->parseOptions($segments);

// Do we have arguments being used here?
// Like: string(100)
if (preg_match('/(.+?)\(([^)]+)\)/', $type, $matches)) {
$type = $matches[1];
$arguments = explode(',', $matches[2]);
}

return compact('name', 'type', 'arguments', 'options');
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/Migrations/SyntaxBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ private function addColumn($field)
{
$syntax = sprintf("\$table->%s('%s')", $field['type'], $field['name']);

// If there are arguments for the schema type, like decimal('amount', 5, 2)
// then we have to remember to work those in.
if ($field['arguments']) {
$syntax = substr($syntax, 0, -1) . ', ';

$syntax .= implode(', ', $field['arguments']) . ')';
}

foreach ($field['options'] as $method => $value) {
$syntax .= sprintf("->%s(%s)", $method, $value === true ? '' : $value);
}
Expand Down

0 comments on commit a2691cf

Please sign in to comment.