Skip to content

Commit

Permalink
Fix styling
Browse files Browse the repository at this point in the history
  • Loading branch information
Sairahcaz authored and github-actions[bot] committed Oct 19, 2023
1 parent dc87e21 commit 71a45c9
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 61 deletions.
2 changes: 1 addition & 1 deletion config/schema-rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
* Always skip this columns
*/
'skip_columns' => ['created_at', 'updated_at', 'deleted_at'],

];
4 changes: 0 additions & 4 deletions src/Contracts/SchemaRulesResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@
interface SchemaRulesResolverInterface
{
/**
* @param string $table
* @param array $columns
* @return array
*/
public function __construct(string $table, array $columns = []);

/**
* Generate the rules of the provided tables schema.
*
* @return array
*/
public function generate(): array;
}
2 changes: 1 addition & 1 deletion src/LaravelSchemaRulesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function register()

break;
default: throw new UnsupportedDbDriverException('This db driver is not supported: '.$driver);
};
}

return new $class(...array_values($parameters));
});
Expand Down
1 change: 1 addition & 0 deletions src/Resolvers/BaseSchemaRulesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
abstract class BaseSchemaRulesResolver implements SchemaRulesResolverInterface
{
private string $table;

private array $columns;

public function __construct(string $table, array $columns = [])
Expand Down
34 changes: 17 additions & 17 deletions src/Resolvers/SchemaRulesResolverMySql.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function getColumnsDefinitionsFromTable()
$databaseName = config('database.connections.mysql.database');
$tableName = $this->table();

$tableColumns = collect(DB::select('SHOW COLUMNS FROM ' . $tableName))->keyBy('Field')->toArray();
$tableColumns = collect(DB::select('SHOW COLUMNS FROM '.$tableName))->keyBy('Field')->toArray();

$foreignKeys = DB::select("
SELECT k.COLUMN_NAME, k.REFERENCED_TABLE_NAME, k.REFERENCED_COLUMN_NAME
Expand All @@ -61,45 +61,45 @@ protected function getColumnsDefinitionsFromTable()
protected function generateColumnRules(stdClass $column): array
{
$columnRules = [];
$columnRules[] = $column->Null === "YES" ? 'nullable' : 'required' ;
$columnRules[] = $column->Null === 'YES' ? 'nullable' : 'required';

if (! empty($column->Foreign)) {
$columnRules[] = "exists:".implode(',', $column->Foreign);
$columnRules[] = 'exists:'.implode(',', $column->Foreign);

return $columnRules;
}

$type = Str::of($column->Type);
switch (true) {
case $type == 'tinyint(1)' && config('schema-rules.tinyint1_to_bool'):
$columnRules[] = "boolean";
$columnRules[] = 'boolean';

break;
case $type->contains('char'):
$columnRules[] = "string";
$columnRules[] = "min:".config('schema-rules.string_min_length');
$columnRules[] = "max:".filter_var($type, FILTER_SANITIZE_NUMBER_INT);
$columnRules[] = 'string';
$columnRules[] = 'min:'.config('schema-rules.string_min_length');
$columnRules[] = 'max:'.filter_var($type, FILTER_SANITIZE_NUMBER_INT);

break;
case $type == 'text':
$columnRules[] = "string";
$columnRules[] = "min:".config('schema-rules.string_min_length');
$columnRules[] = 'string';
$columnRules[] = 'min:'.config('schema-rules.string_min_length');

break;
case $type->contains('int'):
$columnRules[] = "integer";
$sign = ($type->contains('unsigned')) ? 'unsigned' : 'signed' ;
$columnRules[] = 'integer';
$sign = ($type->contains('unsigned')) ? 'unsigned' : 'signed';
$intType = $type->before(' unsigned')->__toString();

// prevent int(xx) for mysql
$intType = preg_replace("/\([^)]+\)/", "", $intType);
$intType = preg_replace("/\([^)]+\)/", '', $intType);

if(! array_key_exists($intType, self::$integerTypes)) {
$intType = "int";
if (! array_key_exists($intType, self::$integerTypes)) {
$intType = 'int';
}

$columnRules[] = "min:".self::$integerTypes[$intType][$sign][0];
$columnRules[] = "max:".self::$integerTypes[$intType][$sign][1];
$columnRules[] = 'min:'.self::$integerTypes[$intType][$sign][0];
$columnRules[] = 'max:'.self::$integerTypes[$intType][$sign][1];

break;
case $type->contains('double') ||
Expand All @@ -108,7 +108,7 @@ protected function generateColumnRules(stdClass $column): array
$type->contains('float'):
// should we do more specific here?
// some kind of regex validation for double, double unsigned, double(8, 2), decimal etc...?
$columnRules[] = "numeric";
$columnRules[] = 'numeric';

break;
case $type->contains('enum') || $type->contains('set'):
Expand Down
28 changes: 14 additions & 14 deletions src/Resolvers/SchemaRulesResolverPgSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ protected function getColumnsDefinitionsFromTable()
$tableName = $this->table();

$tableColumns = collect(DB::select(
"
'
SELECT column_name, data_type, character_maximum_length, is_nullable, column_default
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = :table",
WHERE table_name = :table',
['table' => $tableName]
))->keyBy('column_name')->toArray();

Expand Down Expand Up @@ -57,35 +57,35 @@ protected function getColumnsDefinitionsFromTable()
protected function generateColumnRules(stdClass $column): array
{
$columnRules = [];
$columnRules[] = $column->is_nullable === "YES" ? 'nullable' : 'required' ;
$columnRules[] = $column->is_nullable === 'YES' ? 'nullable' : 'required';

if (! empty($column->Foreign)) {
$columnRules[] = "exists:".implode(',', $column->Foreign);
$columnRules[] = 'exists:'.implode(',', $column->Foreign);

return $columnRules;
}

$type = Str::of($column->data_type);
switch (true) {
case $type == 'boolean':
$columnRules[] = "boolean";
$columnRules[] = 'boolean';

break;
case $type->contains('char'):
$columnRules[] = "string";
$columnRules[] = "min:".config('schema-rules.string_min_length');
$columnRules[] = "max:".$column->character_maximum_length;
$columnRules[] = 'string';
$columnRules[] = 'min:'.config('schema-rules.string_min_length');
$columnRules[] = 'max:'.$column->character_maximum_length;

break;
case $type == 'text':
$columnRules[] = "string";
$columnRules[] = "min:".config('schema-rules.string_min_length');
$columnRules[] = 'string';
$columnRules[] = 'min:'.config('schema-rules.string_min_length');

break;
case $type->contains('int'):
$columnRules[] = "integer";
$columnRules[] = "min:" . self::$integerTypes[$type->__toString()][0];
$columnRules[] = "max:" . self::$integerTypes[$type->__toString()][1];
$columnRules[] = 'integer';
$columnRules[] = 'min:'.self::$integerTypes[$type->__toString()][0];
$columnRules[] = 'max:'.self::$integerTypes[$type->__toString()][1];

break;
case $type->contains('double') ||
Expand All @@ -94,7 +94,7 @@ protected function generateColumnRules(stdClass $column): array
$type->contains('real'):
// should we do more specific here?
// some kind of regex validation for double, double unsigned, double(8, 2), decimal etc...?
$columnRules[] = "numeric";
$columnRules[] = 'numeric';

break;
// unfortunately, it's not so easy in pgsql to find out if a column is an enum
Expand Down
18 changes: 9 additions & 9 deletions src/Resolvers/SchemaRulesResolverSqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,35 @@ protected function getColumnsDefinitionsFromTable()
protected function generateColumnRules(stdClass $column): array
{
$columnRules = [];
$columnRules[] = $column->notnull ? 'required' : 'nullable' ;
$columnRules[] = $column->notnull ? 'required' : 'nullable';

if (! empty($column->Foreign)) {
$columnRules[] = "exists:".implode(',', $column->Foreign);
$columnRules[] = 'exists:'.implode(',', $column->Foreign);

return $columnRules;
}

$type = Str::of($column->type);
switch (true) {
case $type == 'tinyint(1)' && config('schema-rules.tinyint1_to_bool'):
$columnRules[] = "boolean";
$columnRules[] = 'boolean';

break;
case $type == 'varchar' || $type == 'text':
$columnRules[] = "string";
$columnRules[] = "min:".config('schema-rules.min_string');
$columnRules[] = 'string';
$columnRules[] = 'min:'.config('schema-rules.min_string');

break;
case $type == 'integer':
$columnRules[] = "integer";
$columnRules[] = "min:-9223372036854775808";
$columnRules[] = "max:9223372036854775807";
$columnRules[] = 'integer';
$columnRules[] = 'min:-9223372036854775808';
$columnRules[] = 'max:9223372036854775807';

break;
case $type->contains('numeric') || $type->contains('float'):
// should we do more specific here?
// some kind of regex validation for double, double unsigned, double(8, 2), decimal etc...?
$columnRules[] = "numeric";
$columnRules[] = 'numeric';

break;
case $type == 'date' || $type == 'time' || $type == 'datetime':
Expand Down
30 changes: 15 additions & 15 deletions tests/SchemaRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

$this->expectException(\Symfony\Component\Console\Exception\InvalidArgumentException::class);

$this->artisan("schema:generate-rules", [
$this->artisan('schema:generate-rules', [
'foo' => $this->tableName,
]);
});
Expand All @@ -42,7 +42,7 @@

$this->expectException(\Symfony\Component\Console\Exception\InvalidOptionException::class);

$this->artisan("schema:generate-rules", [
$this->artisan('schema:generate-rules', [
'table' => $this->tableName,
'--foo' => 'test_bool',
]);
Expand All @@ -55,7 +55,7 @@

$this->expectException(TableDoesNotExistException::class);

$this->artisan("schema:generate-rules", [
$this->artisan('schema:generate-rules', [
'table' => $this->tableName.'1',
]);
});
Expand All @@ -67,7 +67,7 @@

$this->expectException(MultipleTablesSuppliedException::class);

$this->artisan("schema:generate-rules", [
$this->artisan('schema:generate-rules', [
'table' => $this->tableName.',tests2',
]);
});
Expand All @@ -79,7 +79,7 @@

$this->expectException(ColumnDoesNotExistException::class);

$this->artisan("schema:generate-rules", [
$this->artisan('schema:generate-rules', [
'table' => $this->tableName,
'--columns' => 'foo',
]);
Expand All @@ -106,7 +106,7 @@
$stringNullableColumnName => ['nullable', 'string', 'min:1', 'max:255'],
]);

$this->artisan("schema:generate-rules", [
$this->artisan('schema:generate-rules', [
'table' => $this->tableName,
])->assertSuccessful();
});
Expand All @@ -132,7 +132,7 @@
$boolNullableColumnName => ['nullable', 'boolean'],
]);

$this->artisan("schema:generate-rules", [
$this->artisan('schema:generate-rules', [
'table' => $this->tableName,
])->assertSuccessful();
});
Expand Down Expand Up @@ -170,7 +170,7 @@
$textColumnName => ['required', 'string', 'min:1'],
]);

$this->artisan("schema:generate-rules", [
$this->artisan('schema:generate-rules', [
'table' => $this->tableName,
])->assertSuccessful();
});
Expand Down Expand Up @@ -234,7 +234,7 @@
$bigintNullableColumnName => ['nullable', 'integer', 'min:'.$integerTypes['bigint']['signed'][0], 'max:'.$integerTypes['bigint']['signed'][1]],
]);

$this->artisan("schema:generate-rules", [
$this->artisan('schema:generate-rules', [
'table' => $this->tableName,
])->assertSuccessful();
});
Expand Down Expand Up @@ -282,7 +282,7 @@
$decimalNullableColumnName => ['nullable', 'numeric'],
]);

$this->artisan("schema:generate-rules", [
$this->artisan('schema:generate-rules', [
'table' => $this->tableName,
])->assertSuccessful();
});
Expand Down Expand Up @@ -310,7 +310,7 @@
$setColumnName => ['required', 'string', 'in:'.implode(',', $allowed)],
]);

$this->artisan("schema:generate-rules", [
$this->artisan('schema:generate-rules', [
'table' => $this->tableName,
])->assertSuccessful();
});
Expand Down Expand Up @@ -344,7 +344,7 @@
$timestampColumnName => ['required', 'date', 'after_or_equal:1970-01-01 00:00:01', 'before_or_equal:2038-01-19 03:14:07'],
]);

$this->artisan("schema:generate-rules", [
$this->artisan('schema:generate-rules', [
'table' => $this->tableName,
])->assertSuccessful();
});
Expand All @@ -364,7 +364,7 @@
$jsonColumnName => ['required', 'json'],
]);

$this->artisan("schema:generate-rules", [
$this->artisan('schema:generate-rules', [
'table' => $this->tableName,
])->assertSuccessful();
});
Expand Down Expand Up @@ -396,7 +396,7 @@
$foreignKeyColumnName => ['required', 'exists:'.implode(',', $constrained)],
]);

$this->artisan("schema:generate-rules", [
$this->artisan('schema:generate-rules', [
'table' => $foreignTable,
])->assertSuccessful();

Expand Down Expand Up @@ -426,7 +426,7 @@
$stringColumnName => ['required', 'string', 'min:1', 'max:255'],
]);

$this->artisan("schema:generate-rules", [
$this->artisan('schema:generate-rules', [
'table' => $this->tableName,
])->assertSuccessful();

Expand Down

0 comments on commit 71a45c9

Please sign in to comment.