annotate src/Generator/Factory/FactoryGenerator.php @ 39:b5c6ebd33547 ls_dev_2025_09

Improving the factories, tests, and requests
author Luka Sitas <sitas.luka.97@gmail.com>
date Thu, 25 Sep 2025 23:16:13 -0400
parents 76584181267a
children 2cf26b593f4a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
1 <?php
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
2
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
3 namespace Wizard\MagicForger\Generator\Factory;
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
4
39
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
5 use Illuminate\Support\Str;
35
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
6 use Symfony\Component\Console\Attribute\AsCommand;
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
7 use Wizard\MagicForger\Generator\BaseGenerator;
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
8 use Wizard\MagicForger\Helpers\RelationshipNavigator;
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
9
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
10 #[AsCommand(name: 'mf:factory')]
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
11 class FactoryGenerator extends BaseGenerator
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
12 {
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
13 /**
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
14 * The name and signature of the console command.
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
15 *
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
16 * @var string
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
17 */
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
18 protected $name = 'mf:factory';
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
19
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
20 /**
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
21 * The console command description.
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
22 *
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
23 * @var string
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
24 */
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
25 protected $description = 'Generates the Factory File for a table.';
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
26
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
27 /**
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
28 * The type of class being generated.
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
29 *
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
30 * @var string
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
31 */
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
32 protected $type = 'Factory';
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
33
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
34 protected static $cached_snippets = [];
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
35
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
36 /**
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
37 * Execute the console command.
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
38 *
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
39 * @return mixed
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
40 */
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
41 public function handle()
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
42 {
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
43 // Delegate to parent handler (includes replacements and insertions)
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
44 return parent::handle();
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
45 }
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
46
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
47 /**
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
48 * Get the stub file for the generator.
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
49 *
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
50 * @return string
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
51 */
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
52 protected function getStub()
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
53 {
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
54 return $this->resolveStubPath('/stubs/factory.stub');
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
55 }
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
56
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
57 /**
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
58 * Resolve the fully-qualified path to the stub.
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
59 *
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
60 * @param string $stub
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
61 * @return string
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
62 */
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
63 protected function resolveStubPath($stub)
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
64 {
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
65 return is_file($customPath = $this->laravel->basePath(trim($stub, '/')))
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
66 ? $customPath
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
67 : __DIR__.$stub;
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
68 }
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
69
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
70 protected function getClassName($name)
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
71 {
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
72 return $this->factory_name($name);
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
73 }
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
74
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
75 /**
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
76 * Get the stub file for the generator.
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
77 *
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
78 * @return string
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
79 */
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
80 protected function getPath($name = null)
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
81 {
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
82 return str_replace(['App\\', '\\'], ['app/', '/'], $this->getFactoryNamespace().'/'.$this->factory_name($this->getTableInput()).'.php');
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
83 }
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
84
39
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
85 protected function gatherRelations()
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
86 {
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
87 $relations = RelationshipNavigator::getRelations($this->getCurrentTable());
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
88
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
89 return $relations;
35
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
90
39
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
91 }
35
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
92
39
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
93 protected function renderColumns()
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
94 {
35
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
95 $insert = '';
39
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
96 $values = [];
35
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
97 foreach ($this->get_columns() as $column) {
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
98 if (in_array($column['name'], $this->columns_to_ignore)) {
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
99 continue;
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
100 }
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
101
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
102 $type = $column['type_name'];
39
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
103 $nullable = ($column['nullable'] ? '->optional($weight = 0.5)' : '');
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
104 $value = 'fake()'.$nullable;
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
105 $name = $column['name'];
35
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
106
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
107 // Get the expected header name
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
108 $replacements = [
39
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
109 '{{value}}' => $value.'->text()',
35
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
110 '{{column_name}}' => $name,
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
111 ];
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
112
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
113 // date
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
114 if (in_array($type, ['date'])) {
39
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
115 $replacements['{{value}}'] = $value.'->date()';
35
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
116 }
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
117 // time
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
118 if (in_array($type, ['timestamp'])) {
39
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
119 $replacements['{{value}}'] = $value.'->timestamp()';
35
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
120 }
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
121 // checkbox
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
122 if (in_array($type, ['tinyint'])) {
39
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
123 $replacements['{{value}}'] = $value.'->numberBetween(0,1)';
35
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
124 }
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
125 // select
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
126 elseif (in_array($type, ['bigint']) && array_key_exists($name, $selects)) {
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
127 $replacements['{{header}}'] = Str::headline(Str::singular($selects[$name]));
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
128 $replacements['{{value}}'] = '{{ $item->'.Str::singular($selects[$name]).'?->name ?? "" }}';
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
129 }
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
130 // bigint, float
36
76584181267a Got factories working in a basic way, not sure how complex tables will handle it though
Luka Sitas <sitas.luka.97@gmail.com>
parents: 35
diff changeset
131 elseif (in_array($type, ['bigint', 'int'])) {
39
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
132 $replacements['{{value}}'] = $value.'->randomNumber()';
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
133 } elseif (in_array($type, ['float'])) {
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
134 $replacements['{{value}}'] = $value.'->randomFloat()';
35
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
135 } else {
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
136 // text area
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
137 // varchar, , etc
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
138 }
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
139
36
76584181267a Got factories working in a basic way, not sure how complex tables will handle it though
Luka Sitas <sitas.luka.97@gmail.com>
parents: 35
diff changeset
140 $snippet = $this->getSnippet('value');
35
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
141 // Replace placeholders with actual values
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
142 $values[] = str_replace(
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
143 array_keys($replacements),
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
144 $replacements,
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
145 $snippet
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
146 );
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
147
39
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
148 // Replace placeholders with actual values
35
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
149 }
39
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
150 $insert = implode("\n", $values);
35
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
151
39
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
152 return $insert;
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
153 }
35
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
154
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
155 /**
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
156 * Get available insertions including model relationships.
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
157 */
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
158 public function get_available_inserts(): array
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
159 {
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
160 // Merge parent insertions (attributes, fillable, etc.)
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
161 $inserts = parent::get_available_inserts();
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
162
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
163 // Gather and render relationships for this model
39
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
164 $columns = $this->renderColumns();
35
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
165
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
166 // Assign to stub placeholders
39
b5c6ebd33547 Improving the factories, tests, and requests
Luka Sitas <sitas.luka.97@gmail.com>
parents: 36
diff changeset
167 $inserts['# {{ factoryInsertPoint }}'] = $columns;
35
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
168
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
169 return $inserts;
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
170 }
55d2e5c5dad9 Working on the factory, it's in a semi working state but obviously not complete
Luka Sitas <sitas.luka.97@gmail.com>
parents:
diff changeset
171 }