Skip to content

Commit

Permalink
Merge pull request #18 from mtolhuys/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
mtolhuys committed Mar 7, 2020
2 parents f233f81 + 1d9aee8 commit 5000750
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 116 deletions.
2 changes: 1 addition & 1 deletion dist/app.js

Large diffs are not rendered by default.

89 changes: 39 additions & 50 deletions resources/js/components/Modal/FieldsExplanation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,65 +16,54 @@

<span class="supported-types items-center ml-5 pb-2 md:flex md:items-center">
<ul class="inline-block list-disc md:w-2/4">
<li>
increments
</li>
<li>
integer
</li>
<li>
boolean
</li>
<li>
dateTime
</li>
<li>
date
</li>
<li>
decimal
</li>
<li>bigIncrements</li>
<li>bigInteger</li>
<li>boolean</li>
<li>dateTime</li>
<li>dateTimeTz</li>
<li>date</li>
<li>decimal</li>
<li>enum</li>
<li>geometry</li>
<li>increments</li>
<li>integer</li>
</ul>
<ul class="inline-block list-disc md:w-2/4">
<li>
integer
</li>
<li>
ipAddress
</li>
<li>
string

</li>
<li>
text

</li>
<li>
timestamp
</li>
<li>
time
</li>
<li>ipAddress</li>
<li>json</li>
<li>jsonb</li>
<li>longText</li>
<li>macAddress</li>
<li>mediumInteger</li>
<li>mediumIncrements</li>
<li>point</li>
<li>polygon</li>
<li>rememberToken</li>
<li>smallIncrements</li>
</ul>
<ul class="inline-block list-disc md:w-2/4">
<li>smallInteger</li>
<li>string</li>
<li>text</li>
<li>timestamp</li>
<li>timestampTz</li>
<li>tinyIncrements</li>
<li>tinyInteger</li>
<li>time</li>
<li>unsignedInteger</li>
<li>uuid</li>
<li>year</li>
</ul>
</span>

<p class="py-2">Rules</p>

<span class="supported-types items-center ml-5 pb-2 md:flex md:items-center">
<ul class="inline-block list-disc md:w-2/4">
<li>
max:{int}
</li>
<li>
unsigned
</li>
<li>
unique
</li>
<li>
required
</li>
<li>max:{int}</li>
<li>unsigned</li>
<li>unique</li>
<li>required</li>
</ul>
</span>

Expand Down
48 changes: 48 additions & 0 deletions src/Models/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,52 @@ class Migration extends Model
'migration',
'batch',
];

/**
* Possible methods currently only used by RuleParser
* WARNING: be aware of word length vs. word matching
* f.e. 'date' <-> 'dateTime'
*
* @var array
*/
public static $methods = [
'rememberToken',
'softDeletes',
'softDeletesTz',
'bigIncrements',
'bigInteger',
'dropColumn',
'ipAddress',
'macAddress',
'mediumInteger',
'mediumIncrements',
'renameColumn',
'smallIncrements',
'timestampTz',
'timestamp',
'smallInteger',
'tinyIncrements',
'tinyInteger',
'unsignedInteger',
'increments',
'dateTimeTz',
'dateTime',
'longText',
'integer',
'boolean',
'decimal',
'date',
'enum',
'geometry',
'jsonb',
'json',
'point',
'polygon',
'string',
'text',
'time',
'unsigned',
'uuid',
'year',
];
}
97 changes: 56 additions & 41 deletions src/Services/RuleParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,10 @@

namespace Mtolhuys\LaravelSchematics\Services;

use Mtolhuys\LaravelSchematics\Models\Migration;

class RuleParser
{
public static $types = [
'boolean',
'dateTime',
'date',
'decimal',
'dropColumn',
'integer',
'increments',
'ipAddress',
'renameColumn',
'string',
'text',
'timestamp',
'time',
'unsigned',
];

/**
* Parse the rules to column creation methods
*
Expand All @@ -34,14 +19,20 @@ public static function fieldsToMigrationMethods(array $fields): string
foreach ($fields as $field) {
$column = key($field);
$rule = $field[$column];
$break = PHP_EOL . str_repeat(' ', 12);

if (self::isMethodOnly($rule)) {
$columns .= "\$table->{$rule}();{$break}";

continue;
}

$max = self::getMax($rule);
$type = self::getType($rule);
$method = self::getMethod($rule);
$oldName = self::getRenameFrom($rule);
$additional = self::getAdditionalUpMethods($rule);

$columns .=
"\$table->{$type}({$oldName}'$column'{$max}){$additional}" .
PHP_EOL . str_repeat(' ', 12);
$columns .= "\$table->{$method}({$oldName}'$column'{$max}){$additional}{$break}";
}

return $columns;
Expand All @@ -55,26 +46,40 @@ public static function getAdditionalUpMethods($rule): string
{
$methods = '';
$methods .= self::isUnsigned($rule) ? '->unsigned()' : '';
$methods .= self::isRequired($rule) ? '' : '->nullable()';
$methods .= !self::isRequired($rule) ? '->nullable()' : '';
$methods .= self::isUnique($rule) ? '->unique()' : '';
$methods .= self::hasChanged($rule) ? '->change()' : '';

return "$methods;";
}

/**
* Check if $rule contains any of the supported type
* WARNING: be aware of word length vs. word matching f.e. 'date' <-> 'dateTime'
* In that case the longest word should appear last in the array
* Check if $rule should be handled as single method
*
* @param $rule
* @return mixed|string
*/
public static function isMethodOnly($rule)
{

return self::ruleContains($rule, [
'softDeletes',
'rememberToken',
'softDeletesTz',
]);
}

/**
* Check if $rule contains any of the possible methods
*
* @param $rule
* @return mixed|string
*/
public static function getType($rule)
public static function getMethod($rule)
{
foreach (self::$types as $type) {
if (stripos($rule, strtolower($type)) !== false) {
return $type;
foreach (Migration::$methods as $method) {
if (stripos($rule, strtolower($method)) !== false) {
return $method;
}
}

Expand Down Expand Up @@ -106,8 +111,8 @@ public static function getMax($rule): string
*/
public static function getRenameFrom($rule): string
{
foreach(explode('|', $rule) as $token) {
$hasRenameRule = self::contains($token, 'from:');
foreach (explode('|', $rule) as $token) {
$hasRenameRule = stripos($token, 'from:') !== false;

if ($hasRenameRule) {
$from = substr($token, strpos($token, 'from:') + 5);
Expand All @@ -127,7 +132,7 @@ public static function getRenameFrom($rule): string
*/
public static function isRequired($rule): bool
{
return self::contains($rule, 'required')
return self::ruleContains($rule, ['required'])
|| self::isIncrements($rule)
|| self::isUnique($rule);
}
Expand All @@ -140,7 +145,13 @@ public static function isRequired($rule): bool
*/
public static function isIncrements($rule): bool
{
return self::contains($rule, 'increments');
return self::ruleContains($rule, [
'increments',
'bigIncrements',
'mediumIncrements',
'smallIncrements',
'tinyIncrements',
]);
}

/**
Expand All @@ -151,7 +162,7 @@ public static function isIncrements($rule): bool
*/
public static function isUnique($rule): bool
{
return self::contains($rule, 'unique');
return self::ruleContains($rule, ['unique']);
}

/**
Expand All @@ -162,8 +173,8 @@ public static function isUnique($rule): bool
*/
public static function isUnsigned($rule): bool
{
return
self::contains($rule, 'unsigned');
return self::ruleContains($rule, ['unsigned'])
&& ! self::ruleContains($rule, ['unsignedInteger']);
}


Expand All @@ -175,7 +186,7 @@ public static function isUnsigned($rule): bool
*/
public static function hasChanged($rule): bool
{
return self::contains($rule, 'change');
return self::ruleContains($rule, ['change']);
}

/**
Expand All @@ -186,19 +197,23 @@ public static function hasChanged($rule): bool
*/
public static function isForeign($rule): bool
{
return self::contains($rule, 'foreign');
return self::ruleContains($rule, ['foreign']);
}

/**
* For aesthetic reasons
*
* @param $rule
* @param $needle
* @param $needles
* @return bool
*/
private static function contains($rule, $needle): bool
private static function ruleContains($rule, array $needles): bool
{
return stripos($rule, $needle) !== false;
return !empty(array_filter(
array_map(static function ($rule) use ($needles) {
return in_array($rule, $needles, true);
}, explode('|', $rule))
));
}
}

0 comments on commit 5000750

Please sign in to comment.