comparison src/Replacer/TableReplacer.php @ 34:f65ab84ee47f default

merge with codex
author luka
date Wed, 10 Sep 2025 21:00:47 -0400
parents 8dd668020310
children b5c6ebd33547
comparison
equal deleted inserted replaced
10:a9ff874afdbd 34:f65ab84ee47f
1 <?php 1 <?php
2 2
3 namespace Wizzard\MagicForger\Replacer; 3 namespace Wizard\MagicForger\Replacer;
4 4
5 trait TableReplacer 5 trait TableReplacer
6 { 6 {
7 protected $columns; 7 protected ?array $columns = null;
8 8
9 protected function get_columns() 9 protected array $columns_to_ignore = [
10 'id',
11 'created_at',
12 'updated_at',
13 'created_by',
14 'updated_by',
15 'deleted_at',
16 ];
17
18 /**
19 * Retrieve columns for the current table.
20 */
21 protected function get_columns(): array
10 { 22 {
11 if (is_null($this->columns)) { 23 if (is_null($this->columns)) {
12 $this->columns = $this->getCurrentTable()->getColumns(); 24 $this->columns = $this->getTableColumns($this->getCurrentTable());
13 } 25 }
14 26
15 return $this->columns; 27 return $this->columns;
16 } 28 }
17 29
18 protected function get_attributes() 30 /**
19 { 31 * Get a string representation of values for creation.
20 } 32 */
21 33 protected function getValuesForCreation(): string
22 protected function getValuesForCreation()
23 { 34 {
24 $insert = ''; 35 $insert = '';
25 foreach ($this->get_columns() as $column) { 36 foreach ($this->get_columns() as $column) {
26 $insert .= '$item->'.$column->getName().' = $validated["'.$column->getName().'"] ?? NULL;'."\n"; 37 $column_name = $column['name'];
38 $insert .= sprintf('$item->%s = $validated["%s"] ?? NULL;', $column_name, $column_name)."\n";
27 } 39 }
28 40
29 return $insert; 41 return $insert;
30 } 42 }
31 43
44 /**
45 * Get a string representation of table attributes.
46 */
47 protected function getCasts(): string
48 {
49 $insert = '';
50 foreach ($this->get_columns() as $column) {
51 if (in_array($column['name'], $this->columns_to_ignore)) {
52 continue;
53 }
54 $type = $column['type_name'];
55 //date
56 if(in_array($type, ['date'])) {
57 $insert .= sprintf("'%s' => 'date:Y-m-d',", $column['name'])."\n";
58 }
59 //time
60 if(in_array($type, ['timestamp'])) {
61 $insert .= sprintf("'%s' => 'date:Y-m-d H:i',", $column['name'])."\n";
62 }
63 }
64
65 return $insert;
66 }
67
68 /**
69 * Get a string representation of table attributes.
70 */
71 protected function getAttributes(): string
72 {
73 $insert = '';
74 foreach ($this->get_columns() as $column) {
75 if (in_array($column['name'], $this->columns_to_ignore)) {
76 continue;
77 }
78 $insert .= sprintf("'%s' => '',", $column['name'])."\n";
79 }
80
81 return $insert;
82 }
83
84 /**
85 * Get a string representation of table fillable columns.
86 */
87 protected function getFillable(): string
88 {
89 $insert = '';
90 foreach ($this->get_columns() as $column) {
91 if (in_array($column['name'], $this->columns_to_ignore)) {
92 continue;
93 }
94 $insert .= sprintf("'%s',", $column['name'])."\n";
95 }
96
97 return $insert;
98 }
99
100 /**
101 * Get formatted validation rules for table columns.
102 */
103 protected function getValuesForValidation(): string
104 {
105 $insert = '';
106 foreach ($this->get_columns() as $column) {
107 if (in_array($column['name'], $this->columns_to_ignore)) {
108 continue;
109 }
110 $insert .= sprintf("'%s' => 'nullable',", $column['name'])."\n";
111 }
112
113 return $insert;
114 }
115
116 /**
117 * Apply insertions in the target template.
118 */
32 public function apply_inserts(string $target): string 119 public function apply_inserts(string $target): string
33 { 120 {
34 $inserts = $this->get_all_keywords($target); 121 $inserts = $this->get_all_keywords($target);
35 $available_replacements = $this->get_available_inserts(); 122 $available_insertions = $this->get_available_inserts();
36 123
37 $target = str_replace( 124 return str_replace(
38 array_keys($available_replacements), 125 array_keys($available_insertions),
39 $available_replacements, 126 $available_insertions,
40 $target 127 $target
41 ); 128 );
42
43 return $target;
44 } 129 }
45 130
46 public function get_available_inserts() 131 /**
132 * Get available insertion points for the template.
133 */
134 public function get_available_inserts(): array
47 { 135 {
48 $table_name = $this->getTableInput(); 136 return [
49 $replacements = [ 137 '// {{ valuesForCreation }}' => $this->getValuesForCreation(),
50 '// {{ valuesForCreation }}' => self::getValuesForCreation(), 138 '# {{ attributeInsertPoint }}' => $this->getAttributes(),
139 '# {{ castInsertPoint }}' => $this->getCasts(),
140 '# {{ fillableInsertPoint }}' => $this->getFillable(),
141 '// {{ valuesForValidation }}' => $this->getValuesForValidation(),
51 ]; 142 ];
52
53 foreach ($replacements as $key => &$replacement) {
54 $replacement = $replacement."\n".$key;
55 }
56
57 return $replacements;
58 } 143 }
59 } 144 }