Skip to content

Commit f4d3663

Browse files
authored
Merge pull request #3 from webfactor/2.0
2.0.1
2 parents ba9ea6a + 3bdc913 commit f4d3663

13 files changed

+230
-249
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Webfactor\Laravel\Generators\Contracts;
4+
5+
interface FieldTypeInterface
6+
{
7+
public function getRule(): string;
8+
9+
public function getColumn(): array;
10+
11+
public function getField(): array;
12+
}

src/Schemas/MigrationField.php renamed to src/Contracts/MigrationFieldAbstract.php

Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
<?php
22

3-
namespace Webfactor\Laravel\Generators\Schemas;
3+
namespace Webfactor\Laravel\Generators\Contracts;
44

5-
class MigrationField
5+
abstract class MigrationFieldAbstract implements FieldTypeInterface
66
{
77
private $name;
88

9-
private $type;
10-
119
private $nullable = false;
1210

1311
private $unique = false;
@@ -16,20 +14,12 @@ class MigrationField
1614

1715
private $foreign = null;
1816

19-
public function __construct(string $field)
17+
public function __construct(string $name, array $options = [])
2018
{
21-
$this->parse($field);
22-
}
23-
24-
private function parse(string $field)
25-
{
26-
$params = collect(explode(':', $field));
27-
28-
$this->name = $params->pull(0);
29-
$this->type = $params->pull(1);
19+
$this->name = $name;
3020

31-
foreach ($params as $param) {
32-
$this->fillObject($param);
21+
foreach ($options as $option) {
22+
$this->fillObject($option);
3323
}
3424
}
3525

@@ -94,43 +84,9 @@ public function isUnique(): bool
9484
return $this->unique;
9585
}
9686

97-
public function makeValidationRule(): string
98-
{
99-
$rule = 'required';
100-
101-
switch ($this->getType()) {
87+
abstract public function getRule(): string;
10288

103-
case 'string':
104-
$rule .= '|between:3,255';
105-
break;
89+
abstract public function getColumn(): array;
10690

107-
case 'integer':
108-
$rule .= '|integer';
109-
break;
110-
111-
case 'date':
112-
$rule .= '|date';
113-
break;
114-
}
115-
116-
return $rule;
117-
}
118-
119-
public function makeColumn()
120-
{
121-
return [
122-
'name' => 'title',
123-
'type' => 'text',
124-
'label' => 'Title',
125-
];
126-
}
127-
128-
public function makeField()
129-
{
130-
return [
131-
'name' => 'title',
132-
'type' => 'text',
133-
'label' => 'Title',
134-
];
135-
}
91+
abstract public function getField(): array;
13692
}

src/Schemas/CrudColumn.php

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

src/Schemas/CrudField.php

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Webfactor\Laravel\Generators\Schemas\FieldTypes;
4+
5+
use Webfactor\Laravel\Generators\Contracts\MigrationFieldAbstract;
6+
7+
class BooleanType extends MigrationFieldAbstract
8+
{
9+
public function getRule(): string
10+
{
11+
return 'required';
12+
}
13+
14+
public function getColumn(): array
15+
{
16+
return [
17+
'name' => $this->getName(),
18+
'label' => $this->getName(),
19+
];
20+
}
21+
22+
public function getField(): array
23+
{
24+
return [
25+
'name' => $this->getName(),
26+
'label' => $this->getName(),
27+
];
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Webfactor\Laravel\Generators\Schemas\FieldTypes;
4+
5+
use Webfactor\Laravel\Generators\Contracts\MigrationFieldAbstract;
6+
7+
class DateType extends MigrationFieldAbstract
8+
{
9+
public function getRule(): string
10+
{
11+
return 'required';
12+
}
13+
14+
public function getColumn(): array
15+
{
16+
return [
17+
'name' => $this->getName(),
18+
'label' => $this->getName(),
19+
];
20+
}
21+
22+
public function getField(): array
23+
{
24+
return [
25+
'name' => $this->getName(),
26+
'label' => $this->getName(),
27+
];
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Webfactor\Laravel\Generators\Schemas\FieldTypes;
4+
5+
use Webfactor\Laravel\Generators\Contracts\MigrationFieldAbstract;
6+
7+
class IntegerType extends MigrationFieldAbstract
8+
{
9+
public function getRule(): string
10+
{
11+
return 'required';
12+
}
13+
14+
public function getColumn(): array
15+
{
16+
return [
17+
'name' => $this->getName(),
18+
'label' => $this->getName(),
19+
];
20+
}
21+
22+
public function getField(): array
23+
{
24+
return [
25+
'name' => $this->getName(),
26+
'label' => $this->getName(),
27+
];
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Webfactor\Laravel\Generators\Schemas\FieldTypes;
4+
5+
use Webfactor\Laravel\Generators\Contracts\MigrationFieldAbstract;
6+
7+
class StringType extends MigrationFieldAbstract
8+
{
9+
public function getRule(): string
10+
{
11+
return 'required';
12+
}
13+
14+
public function getColumn(): array
15+
{
16+
return [
17+
'name' => $this->getName(),
18+
'label' => $this->getName(),
19+
];
20+
}
21+
22+
public function getField(): array
23+
{
24+
return [
25+
'name' => $this->getName(),
26+
'label' => $this->getName(),
27+
];
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Webfactor\Laravel\Generators\Schemas\FieldTypes;
4+
5+
use Webfactor\Laravel\Generators\Contracts\MigrationFieldAbstract;
6+
7+
class TextType extends MigrationFieldAbstract
8+
{
9+
public function getRule(): string
10+
{
11+
return 'required';
12+
}
13+
14+
public function getColumn(): array
15+
{
16+
return [
17+
'name' => $this->getName(),
18+
'label' => $this->getName(),
19+
];
20+
}
21+
22+
public function getField(): array
23+
{
24+
return [
25+
'name' => $this->getName(),
26+
'label' => $this->getName(),
27+
];
28+
}
29+
}

src/Schemas/MigrationSchema.php

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

55
use Illuminate\Support\Collection;
6+
use Webfactor\Laravel\Generators\Contracts\FieldTypeInterface;
67

78
class MigrationSchema
89
{
@@ -13,10 +14,38 @@ public function __construct(string $schema)
1314
$this->structure = collect();
1415

1516
foreach (explode(',', $schema) as $field) {
16-
$this->structure->push(new MigrationField($field));
17+
$this->setMigrationField($field);
1718
}
1819
}
1920

21+
private function setMigrationField($field)
22+
{
23+
$options = explode(':', $field);
24+
25+
$name = array_shift($options);
26+
$type = array_shift($options);
27+
28+
if ($fieldType = $this->getFieldType($type, $name, $options)) {
29+
$this->structure->push($fieldType);
30+
}
31+
}
32+
33+
protected function getFieldType($type, $name, $options)
34+
{
35+
$typeClass = 'Webfactor\\Laravel\\Generators\\Schemas\\FieldTypes\\' . ucfirst($type) . 'Type';
36+
37+
if (class_exists($typeClass)) {
38+
return $this->loadFieldType(new $typeClass($name, $options));
39+
}
40+
41+
return null;
42+
}
43+
44+
private function loadFieldType(FieldTypeInterface $fieldType)
45+
{
46+
return $fieldType;
47+
}
48+
2049
/**
2150
* @return Collection
2251
*/

0 commit comments

Comments
 (0)