Skip to content

Commit 672a662

Browse files
authored
Merge pull request #4 from webfactor/2.0
[RF] refactor
2 parents f4d3663 + 3fa2acb commit 672a662

15 files changed

+148
-149
lines changed

src/Contracts/FieldTypeInterface.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/Contracts/MigrationFieldAbstract.php

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,30 @@
22

33
namespace Webfactor\Laravel\Generators\Contracts;
44

5-
abstract class MigrationFieldAbstract implements FieldTypeInterface
6-
{
7-
private $name;
5+
use Webfactor\Laravel\Generators\Traits\CrudColumn;
6+
use Webfactor\Laravel\Generators\Traits\CrudField;
7+
use Webfactor\Laravel\Generators\Traits\ValidationRule;
88

9-
private $nullable = false;
9+
abstract class MigrationFieldAbstract implements MigrationFieldTypeInterface
10+
{
11+
use CrudColumn, CrudField, ValidationRule;
1012

11-
private $unique = false;
13+
private $name;
1214

13-
private $default = null;
15+
private $nullable;
1416

15-
private $foreign = null;
17+
private $unique;
1618

1719
public function __construct(string $name, array $options = [])
1820
{
1921
$this->name = $name;
2022

2123
foreach ($options as $option) {
22-
$this->fillObject($option);
24+
$this->parseOptions($option);
2325
}
2426
}
2527

26-
private function fillObject(string $param)
28+
private function parseOptions(string $param)
2729
{
2830
if ($param == 'nullable') {
2931
return $this->nullable = true;
@@ -33,10 +35,6 @@ private function fillObject(string $param)
3335
return $this->unique = true;
3436
}
3537

36-
if ($param == 'foreign') {
37-
return $this->foreign = true;
38-
}
39-
4038
if (starts_with($param, 'default(')) {
4139
preg_match('/\((.*)\)/', $param, $match);
4240

@@ -52,22 +50,6 @@ public function getName()
5250
return $this->name;
5351
}
5452

55-
/**
56-
* @return string
57-
*/
58-
public function getType()
59-
{
60-
return $this->type;
61-
}
62-
63-
/**
64-
* @return mixed
65-
*/
66-
public function getDefault()
67-
{
68-
return $this->default;
69-
}
70-
7153
/**
7254
* @return bool
7355
*/
@@ -83,10 +65,4 @@ public function isUnique(): bool
8365
{
8466
return $this->unique;
8567
}
86-
87-
abstract public function getRule(): string;
88-
89-
abstract public function getColumn(): array;
90-
91-
abstract public function getField(): array;
9268
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Webfactor\Laravel\Generators\Contracts;
4+
5+
use Webfactor\Laravel\Generators\Traits\CrudColumn;
6+
use Webfactor\Laravel\Generators\Schemas\CrudField;
7+
use Webfactor\Laravel\Generators\Schemas\RequestValidationRule;
8+
9+
interface MigrationFieldTypeInterface
10+
{
11+
public function getValidationRule(): string;
12+
13+
public function getCrudColumn(): array;
14+
15+
public function getCrudField(): array;
16+
}

src/Schemas/FieldTypes/BooleanType.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,9 @@
66

77
class BooleanType extends MigrationFieldAbstract
88
{
9-
public function getRule(): string
10-
{
11-
return 'required';
12-
}
9+
protected $validationRuleType = 'boolean';
1310

14-
public function getColumn(): array
15-
{
16-
return [
17-
'name' => $this->getName(),
18-
'label' => $this->getName(),
19-
];
20-
}
11+
protected $crudColumnType = 'boolean';
2112

22-
public function getField(): array
23-
{
24-
return [
25-
'name' => $this->getName(),
26-
'label' => $this->getName(),
27-
];
28-
}
13+
protected $crudFieldType = 'boolean';
2914
}

src/Schemas/FieldTypes/DateType.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,9 @@
66

77
class DateType extends MigrationFieldAbstract
88
{
9-
public function getRule(): string
10-
{
11-
return 'required';
12-
}
9+
protected $validationRuleType = 'date';
1310

14-
public function getColumn(): array
15-
{
16-
return [
17-
'name' => $this->getName(),
18-
'label' => $this->getName(),
19-
];
20-
}
11+
protected $crudColumnType = 'date';
2112

22-
public function getField(): array
23-
{
24-
return [
25-
'name' => $this->getName(),
26-
'label' => $this->getName(),
27-
];
28-
}
13+
protected $crudFieldType = 'date';
2914
}

src/Schemas/FieldTypes/IntegerType.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,9 @@
66

77
class IntegerType extends MigrationFieldAbstract
88
{
9-
public function getRule(): string
10-
{
11-
return 'required';
12-
}
9+
protected $validationRuleType = 'integer';
1310

14-
public function getColumn(): array
15-
{
16-
return [
17-
'name' => $this->getName(),
18-
'label' => $this->getName(),
19-
];
20-
}
11+
protected $crudColumnType = 'number';
2112

22-
public function getField(): array
23-
{
24-
return [
25-
'name' => $this->getName(),
26-
'label' => $this->getName(),
27-
];
28-
}
13+
protected $crudFieldType = 'number';
2914
}

src/Schemas/FieldTypes/StringType.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,9 @@
66

77
class StringType extends MigrationFieldAbstract
88
{
9-
public function getRule(): string
10-
{
11-
return 'required';
12-
}
9+
protected $validationRuleType = 'string';
1310

14-
public function getColumn(): array
15-
{
16-
return [
17-
'name' => $this->getName(),
18-
'label' => $this->getName(),
19-
];
20-
}
11+
protected $crudColumnType = 'text';
2112

22-
public function getField(): array
23-
{
24-
return [
25-
'name' => $this->getName(),
26-
'label' => $this->getName(),
27-
];
28-
}
13+
protected $crudFieldType = 'text';
2914
}

src/Schemas/FieldTypes/TextType.php

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,9 @@
66

77
class TextType extends MigrationFieldAbstract
88
{
9-
public function getRule(): string
10-
{
11-
return 'required';
12-
}
9+
protected $validationRuleType = 'text';
1310

14-
public function getColumn(): array
15-
{
16-
return [
17-
'name' => $this->getName(),
18-
'label' => $this->getName(),
19-
];
20-
}
11+
protected $crudColumnType = 'text';
2112

22-
public function getField(): array
23-
{
24-
return [
25-
'name' => $this->getName(),
26-
'label' => $this->getName(),
27-
];
28-
}
13+
protected $crudFieldType = 'summernote';
2914
}

src/Schemas/MigrationSchema.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Webfactor\Laravel\Generators\Schemas;
44

55
use Illuminate\Support\Collection;
6-
use Webfactor\Laravel\Generators\Contracts\FieldTypeInterface;
6+
use Webfactor\Laravel\Generators\Contracts\MigrationFieldTypeInterface;
77

88
class MigrationSchema
99
{
@@ -25,23 +25,23 @@ private function setMigrationField($field)
2525
$name = array_shift($options);
2626
$type = array_shift($options);
2727

28-
if ($fieldType = $this->getFieldType($type, $name, $options)) {
29-
$this->structure->push($fieldType);
28+
if ($migrationFieldType = $this->getMigrationFieldType($type, $name, $options)) {
29+
$this->structure->push($migrationFieldType);
3030
}
3131
}
3232

33-
protected function getFieldType($type, $name, $options)
33+
protected function getMigrationFieldType($type, $name, $options)
3434
{
3535
$typeClass = 'Webfactor\\Laravel\\Generators\\Schemas\\FieldTypes\\' . ucfirst($type) . 'Type';
3636

3737
if (class_exists($typeClass)) {
38-
return $this->loadFieldType(new $typeClass($name, $options));
38+
return $this->loadMigrationFieldType(new $typeClass($name, $options));
3939
}
4040

4141
return null;
4242
}
4343

44-
private function loadFieldType(FieldTypeInterface $fieldType)
44+
private function loadMigrationFieldType(MigrationFieldTypeInterface $fieldType)
4545
{
4646
return $fieldType;
4747
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Webfactor\Laravel\Generators\Schemas;
4+
5+
class RequestValidationRule
6+
{
7+
}

0 commit comments

Comments
 (0)