annotate src/Replacer/TableReplacer.php @ 11:3426c7e91c24 main-dev

Modifying generaters and replacers
author luka
date Wed, 24 Apr 2024 19:53:42 -0400
parents 769a17898cc0
children c969ed13c570
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
1 <?php
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
2
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
3 namespace Wizzard\MagicForger\Replacer;
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
4
6
b46922d4a301 Update for psr compliance
luka
parents: 5
diff changeset
5 trait TableReplacer
5
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
6 {
7
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
7 protected $columns;
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
8
5
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
9 protected function get_columns()
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
10 {
7
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
11 if (is_null($this->columns)) {
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
12 $this->columns = $this->getCurrentTable()->getColumns();
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
13 }
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
14
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
15 return $this->columns;
5
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
16 }
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
17
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
18 protected function get_attributes()
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
19 {
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
20 }
7
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
21
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
22 protected function getValuesForCreation()
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
23 {
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
24 $insert = '';
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
25 foreach ($this->get_columns() as $column) {
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
26 $insert .= '$item->'.$column->getName().' = $validated["'.$column->getName().'"] ?? NULL;'."\n";
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
27 }
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
28
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
29 return $insert;
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
30 }
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
31
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
32 protected function getAttributes()
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
33 {
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
34 $insert = '';
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
35 foreach ($this->get_columns() as $column) {
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
36 $insert .= "'".$column->getName()."' => '',\n";
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
37 }
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
38
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
39 return $insert;
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
40 }
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
41
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
42 protected function getValuesForValidation()
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
43 {
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
44 $insert = '';
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
45 foreach ($this->get_columns() as $column) {
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
46 $insert .= "'".$column->getName()."' => 'nullable',\n";
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
47 }
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
48
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
49 return $insert;
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
50 }
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
51
7
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
52 public function apply_inserts(string $target): string
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
53 {
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
54 $inserts = $this->get_all_keywords($target);
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
55 $available_replacements = $this->get_available_inserts();
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
56
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
57 $target = str_replace(
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
58 array_keys($available_replacements),
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
59 $available_replacements,
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
60 $target
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
61 );
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
62
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
63 return $target;
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
64 }
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
65
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
66 public function get_available_inserts()
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
67 {
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
68 $table_name = $this->getTableInput();
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
69 $replacements = [
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
70 '// {{ valuesForCreation }}' => self::getValuesForCreation(),
11
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
71 '# {{ atributeInsertPoint }}' => self::getAttributes(),
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
72 '// {{ valuesForValidation }}' => self::getValuesForValidation(),
3426c7e91c24 Modifying generaters and replacers
luka
parents: 7
diff changeset
73 ];
7
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
74
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
75 foreach ($replacements as $key => &$replacement) {
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
76 $replacement = $replacement."\n".$key;
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
77 }
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
78
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
79 return $replacements;
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
80 }
5
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
81 }