1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PrismX\Generators\Generators; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Illuminate\Support\Facades\File; |
7
|
|
|
use PrismX\Generators\Support\Model; |
8
|
|
|
use PrismX\Generators\Support\AbstractGenerator; |
9
|
|
|
|
10
|
|
|
class FactoryGenerator extends AbstractGenerator |
11
|
|
|
{ |
12
|
|
|
const INDENT = ' '; |
13
|
|
|
|
14
|
|
|
public function __construct(Model $model) |
15
|
|
|
{ |
16
|
|
|
parent::__construct($model); |
17
|
|
|
$this->stub = File::get(STUBS_PATH.'/factory.stub'); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function getPath(): string |
21
|
|
|
{ |
22
|
|
|
return 'database/factories/'.$this->model->name().'Factory.php'; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function populateStub(): string |
26
|
|
|
{ |
27
|
|
|
$stub = $this->stub; |
28
|
|
|
$stub = str_replace('{{Namespace}}', config('generators.model_namespace'), $stub); |
29
|
|
|
$stub = str_replace('{{ClassName}}', $this->model->name(), $stub); |
30
|
|
|
$stub = str_replace('{{fields}}', $this->buildDefinition(), $stub); |
31
|
|
|
|
32
|
|
|
return $stub; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function buildDefinition() |
36
|
|
|
{ |
37
|
|
|
$definition = ''; |
38
|
|
|
|
39
|
|
|
foreach ($this->model->columns() as $column) { |
40
|
|
|
if ($column->name() === 'id') { |
41
|
|
|
continue; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if ($column->dataType() === 'id') { |
45
|
|
|
$name = Str::substr($column->name(), 0, -3); |
46
|
|
|
$class = Str::studly($column->attributes()[0] ?? $name); |
47
|
|
|
|
48
|
|
|
$definition .= self::INDENT."'{$column->name()}' => "; |
49
|
|
|
$definition .= sprintf('factory(\\'.config('generators.model_namespace')."\%s::class)", $class); |
50
|
|
|
$definition .= ','.PHP_EOL; |
51
|
|
|
} else { |
52
|
|
|
$definition .= self::INDENT."'{$column->name()}' => "; |
53
|
|
|
$faker = $this->fakerData($column->name()) ?? $this->fakerDataType($column->dataType()); |
54
|
|
|
$definition .= '$faker->'.$faker; |
55
|
|
|
$definition .= ','.PHP_EOL; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return trim($definition); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function fakerData(string $name) |
63
|
|
|
{ |
64
|
|
|
static $fakeableNames = [ |
65
|
|
|
'city' => 'city', |
66
|
|
|
'company' => 'company', |
67
|
|
|
'content' => 'paragraphs(3, true)', |
68
|
|
|
'country' => 'country', |
69
|
|
|
'description' => 'text', |
70
|
|
|
'email' => 'safeEmail', |
71
|
|
|
'first_name' => 'firstName', |
72
|
|
|
'firstname' => 'firstName', |
73
|
|
|
'guid' => 'uuid', |
74
|
|
|
'last_name' => 'lastName', |
75
|
|
|
'lastname' => 'lastName', |
76
|
|
|
'lat' => 'latitude', |
77
|
|
|
'latitude' => 'latitude', |
78
|
|
|
'lng' => 'longitude', |
79
|
|
|
'longitude' => 'longitude', |
80
|
|
|
'name' => 'name', |
81
|
|
|
'password' => 'password', |
82
|
|
|
'phone' => 'phoneNumber', |
83
|
|
|
'phone_number' => 'phoneNumber', |
84
|
|
|
'postcode' => 'postcode', |
85
|
|
|
'postal_code' => 'postcode', |
86
|
|
|
'slug' => 'slug', |
87
|
|
|
'street' => 'streetName', |
88
|
|
|
'address1' => 'streetAddress', |
89
|
|
|
'address2' => 'secondaryAddress', |
90
|
|
|
'summary' => 'text', |
91
|
|
|
'title' => 'sentence(4)', |
92
|
|
|
'url' => 'url', |
93
|
|
|
'user_name' => 'userName', |
94
|
|
|
'username' => 'userName', |
95
|
|
|
'uuid' => 'uuid', |
96
|
|
|
'zip' => 'postcode', |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
return $fakeableNames[$name] ?? null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function fakerDataType(string $type) |
103
|
|
|
{ |
104
|
|
|
$fakeableTypes = [ |
105
|
|
|
'id' => 'randomDigitNotNull', |
106
|
|
|
'string' => 'word', |
107
|
|
|
'text' => 'text', |
108
|
|
|
'date' => 'date()', |
109
|
|
|
'time' => 'time()', |
110
|
|
|
'guid' => 'word', |
111
|
|
|
'datetimetz' => 'dateTime()', |
112
|
|
|
'datetime' => 'dateTime()', |
113
|
|
|
'timestamp' => 'dateTime()', |
114
|
|
|
'integer' => 'randomNumber()', |
115
|
|
|
'bigint' => 'randomNumber()', |
116
|
|
|
'smallint' => 'randomNumber()', |
117
|
|
|
'decimal' => 'randomFloat()', |
118
|
|
|
'float' => 'randomFloat()', |
119
|
|
|
'boolean' => 'boolean', |
120
|
|
|
]; |
121
|
|
|
|
122
|
|
|
return $fakeableTypes[$type] ?? null; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|