Skip to content

Commit 710208f

Browse files
authored
Merge pull request #4 from giagara/main
Fix error in PR #3 and refactoring
2 parents 449e8ff + 4da639e commit 710208f

10 files changed

+124
-136
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ php artisan schema:generate-rules persons -cf --file Api\\V1\\StorePersonRequest
126126
To always skip columns add it in the config file, under `skip_columns` parameter.
127127

128128
```php
129-
'skip_columns' => ['created_at', 'updated_at', 'deleted_at', 'whatever'],
129+
'skip_columns' => ['whatever', 'some_other_column'],
130130
```
131131

132132

config/schema-rules.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
'string_min_length' => env('SCHEMA_RULES_STRING_MIN_LENGTH', 1),
1717

1818
/**
19-
* Always skip this columns (usefule for created_at, updated_at, deleted_at)
19+
* Always skip this columns
2020
*/
21-
'skip_columns' => []
21+
'skip_columns' => ['created_at', 'updated_at', 'deleted_at'],
2222

2323
];

src/Commands/GenerateRulesCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use LaracraftTech\LaravelSchemaRules\Exceptions\FailedToCreateRequestClassException;
1515
use LaracraftTech\LaravelSchemaRules\Exceptions\MultipleTablesSuppliedException;
1616
use LaracraftTech\LaravelSchemaRules\Exceptions\TableDoesNotExistException;
17-
use LaracraftTech\LaravelSchemaRules\Resolvers\SchemaRulesResolverInterface;
17+
use LaracraftTech\LaravelSchemaRules\Contracts\SchemaRulesResolverInterface;
1818

1919
class GenerateRulesCommand extends Command
2020
{

src/Resolvers/SchemaRulesResolverInterface.php renamed to src/Contracts/SchemaRulesResolverInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace LaracraftTech\LaravelSchemaRules\Resolvers;
3+
namespace LaracraftTech\LaravelSchemaRules\Contracts;
44

55
interface SchemaRulesResolverInterface
66
{

src/LaravelSchemaRulesServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use LaracraftTech\LaravelSchemaRules\Commands\GenerateRulesCommand;
66
use LaracraftTech\LaravelSchemaRules\Exceptions\UnsupportedDbDriverException;
7-
use LaracraftTech\LaravelSchemaRules\Resolvers\SchemaRulesResolverInterface;
7+
use LaracraftTech\LaravelSchemaRules\Contracts\SchemaRulesResolverInterface;
88
use LaracraftTech\LaravelSchemaRules\Resolvers\SchemaRulesResolverMySql;
99
use LaracraftTech\LaravelSchemaRules\Resolvers\SchemaRulesResolverPgSql;
1010
use LaracraftTech\LaravelSchemaRules\Resolvers\SchemaRulesResolverSqlite;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace LaracraftTech\LaravelSchemaRules\Resolvers;
4+
5+
use LaracraftTech\LaravelSchemaRules\Contracts\SchemaRulesResolverInterface;
6+
use stdClass;
7+
8+
abstract class BaseSchemaRulesResolver implements SchemaRulesResolverInterface
9+
{
10+
11+
private string $table;
12+
private array $columns;
13+
14+
public function __construct(string $table, array $columns = [])
15+
{
16+
$this->table = $table;
17+
$this->columns = $columns;
18+
}
19+
20+
public function generate(): array
21+
{
22+
$tableColumns = $this->getColumnsDefinitionsFromTable();
23+
24+
$skip_columns = config('schema-rules.skip_columns');
25+
26+
$tableRules = [];
27+
foreach ($tableColumns as $column) {
28+
$field = $this->getField($column);
29+
30+
// If specific columns where supplied only process those...
31+
if (! empty($this->columns()) && ! in_array($field, $this->columns())) {
32+
continue;
33+
}
34+
35+
// If column should be skipped
36+
if (in_array($field, $skip_columns)) {
37+
continue;
38+
}
39+
40+
// We do not need a rule for auto increments
41+
if ($this->isAutoIncrement($column)) {
42+
continue;
43+
}
44+
45+
$tableRules[$field] = $this->generateColumnRules($column);
46+
}
47+
48+
return $tableRules;
49+
}
50+
51+
protected function table()
52+
{
53+
return $this->table;
54+
}
55+
56+
protected function columns()
57+
{
58+
return $this->columns;
59+
}
60+
61+
abstract protected function isAutoIncrement($column) : bool;
62+
63+
abstract protected function getField($column) : string;
64+
65+
abstract protected function getColumnsDefinitionsFromTable();
66+
67+
abstract protected function generateColumnRules(stdClass $column): array;
68+
69+
}

src/Resolvers/SchemaRulesResolverMySql.php

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44

55
use Illuminate\Support\Facades\DB;
66
use Illuminate\Support\Str;
7+
use LaracraftTech\LaravelSchemaRules\Contracts\SchemaRulesResolverInterface;
78
use stdClass;
89

9-
class SchemaRulesResolverMySql implements SchemaRulesResolverInterface
10+
class SchemaRulesResolverMySql extends BaseSchemaRulesResolver implements SchemaRulesResolverInterface
1011
{
11-
private string $table;
12-
private array $columns;
1312

1413
public static array $integerTypes = [
1514
'tinyint' => [
@@ -34,47 +33,10 @@ class SchemaRulesResolverMySql implements SchemaRulesResolverInterface
3433
],
3534
];
3635

37-
public function __construct(string $table, array $columns = [])
38-
{
39-
$this->table = $table;
40-
$this->columns = $columns;
41-
}
42-
43-
public function generate(): array
44-
{
45-
$tableColumns = $this->getColumnsDefinitionsFromTable();
46-
47-
$skip_columns = config('schema-rules.skip_columns');
48-
49-
$tableRules = [];
50-
foreach ($tableColumns as $column) {
51-
$field = $column->Field;
52-
53-
// If specific columns where supplied only process those...
54-
if (! empty($this->columns) && ! in_array($field, $this->columns)) {
55-
continue;
56-
}
57-
58-
// If column should be skipped
59-
if (in_array($column, $skip_columns)) {
60-
continue;
61-
}
62-
63-
// We do not need a rule for auto increments
64-
if ($column->Extra === 'auto_increment') {
65-
continue;
66-
}
67-
68-
$tableRules[$field] = $this->generateColumnRules($column);
69-
}
70-
71-
return $tableRules;
72-
}
73-
74-
private function getColumnsDefinitionsFromTable()
36+
protected function getColumnsDefinitionsFromTable()
7537
{
7638
$databaseName = config('database.connections.mysql.database');
77-
$tableName = $this->table;
39+
$tableName = $this->table();
7840

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

@@ -97,7 +59,7 @@ private function getColumnsDefinitionsFromTable()
9759
return $tableColumns;
9860
}
9961

100-
private function generateColumnRules(stdClass $column): array
62+
protected function generateColumnRules(stdClass $column): array
10163
{
10264
$columnRules = [];
10365
$columnRules[] = $column->Null === "YES" ? 'nullable' : 'required' ;
@@ -177,4 +139,15 @@ private function generateColumnRules(stdClass $column): array
177139

178140
return $columnRules;
179141
}
142+
143+
protected function isAutoIncrement($column) : bool
144+
{
145+
return $column->Extra === 'auto_increment';
146+
}
147+
148+
protected function getField($column) : string
149+
{
150+
return $column->Field;
151+
}
152+
180153
}

src/Resolvers/SchemaRulesResolverPgSql.php

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,22 @@
44

55
use Illuminate\Support\Facades\DB;
66
use Illuminate\Support\Str;
7+
use LaracraftTech\LaravelSchemaRules\Contracts\SchemaRulesResolverInterface;
78
use stdClass;
89

9-
class SchemaRulesResolverPgSql implements SchemaRulesResolverInterface
10+
class SchemaRulesResolverPgSql extends BaseSchemaRulesResolver implements SchemaRulesResolverInterface
1011
{
11-
private string $table;
12-
private array $columns;
1312

1413
public static array $integerTypes = [
1514
'smallint' => ['-32768', '32767'],
1615
'integer' => ['-2147483648', '2147483647'],
1716
'bigint' => ['-9223372036854775808', '9223372036854775807'],
1817
];
1918

20-
public function __construct(string $table, array $columns = [])
21-
{
22-
$this->table = $table;
23-
$this->columns = $columns;
24-
}
25-
26-
public function generate(): array
27-
{
28-
$tableColumns = $this->getColumnsDefinitionsFromTable();
29-
30-
$skip_columns = config('schema-rules.skip_columns');
31-
32-
$tableRules = [];
33-
foreach ($tableColumns as $column) {
34-
$field = $column->column_name;
35-
36-
// If specific columns where supplied only process those...
37-
if (! empty($this->columns) && ! in_array($field, $this->columns)) {
38-
continue;
39-
}
40-
41-
// If column should be skipped
42-
if (in_array($column, $skip_columns)) {
43-
continue;
44-
}
45-
46-
// We do not need a rule for auto increments
47-
if (Str::contains($column->column_default, 'nextval')) {
48-
continue;
49-
}
50-
51-
$tableRules[$field] = $this->generateColumnRules($column);
52-
}
53-
54-
return $tableRules;
55-
}
56-
57-
private function getColumnsDefinitionsFromTable()
19+
protected function getColumnsDefinitionsFromTable()
5820
{
5921
$databaseName = config('database.connections.mysql.database');
60-
$tableName = $this->table;
22+
$tableName = $this->table();
6123

6224
$tableColumns = collect(DB::select(
6325
"
@@ -93,7 +55,7 @@ private function getColumnsDefinitionsFromTable()
9355
return $tableColumns;
9456
}
9557

96-
private function generateColumnRules(stdClass $column): array
58+
protected function generateColumnRules(stdClass $column): array
9759
{
9860
$columnRules = [];
9961
$columnRules[] = $column->is_nullable === "YES" ? 'nullable' : 'required' ;
@@ -157,4 +119,15 @@ private function generateColumnRules(stdClass $column): array
157119

158120
return $columnRules;
159121
}
122+
123+
protected function isAutoIncrement($column) : bool
124+
{
125+
return Str::contains($column->column_default, 'nextval');
126+
}
127+
128+
protected function getField($column) : string
129+
{
130+
return $column->column_name;
131+
}
132+
160133
}

src/Resolvers/SchemaRulesResolverSqlite.php

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,18 @@
44

55
use Illuminate\Support\Facades\DB;
66
use Illuminate\Support\Str;
7+
use LaracraftTech\LaravelSchemaRules\Contracts\SchemaRulesResolverInterface;
78
use stdClass;
89

9-
class SchemaRulesResolverSqlite implements SchemaRulesResolverInterface
10+
class SchemaRulesResolverSqlite extends BaseSchemaRulesResolver implements SchemaRulesResolverInterface
1011
{
11-
private string $table;
12-
private array $columns;
1312

14-
public function __construct(string $table, array $columns = [])
13+
protected function getColumnsDefinitionsFromTable()
1514
{
16-
$this->table = $table;
17-
$this->columns = $columns;
18-
}
19-
20-
public function generate(): array
21-
{
22-
$tableColumns = $this->getColumnsDefinitionsFromTable();
23-
24-
$skip_columns = config('schema-rules.skip_columns');
25-
26-
$tableRules = [];
27-
foreach ($tableColumns as $column) {
28-
$field = $column->name;
29-
30-
// If specific columns where supplied only process those...
31-
if (! empty($this->columns) && ! in_array($field, $this->columns)) {
32-
continue;
33-
}
34-
35-
// If column should be skipped
36-
if (in_array($column, $skip_columns)) {
37-
continue;
38-
}
39-
40-
// We do not need a rule for auto increments
41-
if ($column->pk) {
42-
continue;
43-
}
44-
45-
$tableRules[$field] = $this->generateColumnRules($column);
46-
}
47-
//dd($tableColumns);
48-
return $tableRules;
49-
}
5015

51-
private function getColumnsDefinitionsFromTable()
52-
{
53-
$tableColumns = collect(DB::select('PRAGMA table_info(' . $this->table . ')'))->keyBy('name')->toArray();
16+
$tableColumns = collect(DB::select("PRAGMA table_info('{$this->table()}')"))->keyBy('name')->toArray();
5417

55-
$foreignKeys = DB::select("PRAGMA foreign_key_list($this->table)");
18+
$foreignKeys = DB::select("PRAGMA foreign_key_list({$this->table()})");
5619

5720
foreach ($foreignKeys as $foreignKey) {
5821
$tableColumns[$foreignKey->from]->Foreign = [
@@ -64,7 +27,7 @@ private function getColumnsDefinitionsFromTable()
6427
return $tableColumns;
6528
}
6629

67-
private function generateColumnRules(stdClass $column): array
30+
protected function generateColumnRules(stdClass $column): array
6831
{
6932
$columnRules = [];
7033
$columnRules[] = $column->notnull ? 'required' : 'nullable' ;
@@ -109,4 +72,14 @@ private function generateColumnRules(stdClass $column): array
10972

11073
return $columnRules;
11174
}
75+
76+
protected function isAutoIncrement($column) : bool
77+
{
78+
return $column->pk;
79+
}
80+
81+
protected function getField($column) : string
82+
{
83+
return $column->name;
84+
}
11285
}

tests/SchemaRulesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use LaracraftTech\LaravelSchemaRules\Exceptions\ColumnDoesNotExistException;
66
use LaracraftTech\LaravelSchemaRules\Exceptions\MultipleTablesSuppliedException;
77
use LaracraftTech\LaravelSchemaRules\Exceptions\TableDoesNotExistException;
8-
use LaracraftTech\LaravelSchemaRules\Resolvers\SchemaRulesResolverInterface;
8+
use LaracraftTech\LaravelSchemaRules\Contracts\SchemaRulesResolverInterface;
99
use LaracraftTech\LaravelSchemaRules\Resolvers\SchemaRulesResolverMySql;
1010

1111
beforeEach(function () {

0 commit comments

Comments
 (0)