annotate src/Replacer/TableReplacer.php @ 7:769a17898cc0

Various changes to the generators and replacers - probably mostly just formatting
author luka
date Wed, 18 Oct 2023 21:04:11 -0400
parents b46922d4a301
children 3426c7e91c24
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
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
32 public function apply_inserts(string $target): string
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
33 {
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
34 $inserts = $this->get_all_keywords($target);
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
35 $available_replacements = $this->get_available_inserts();
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
36
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
37 $target = str_replace(
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
38 array_keys($available_replacements),
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
39 $available_replacements,
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
40 $target
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
41 );
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
42
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
43 return $target;
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
44 }
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
45
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
46 public function get_available_inserts()
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
47 {
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
48 $table_name = $this->getTableInput();
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
49 $replacements = [
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
50 '// {{ valuesForCreation }}' => self::getValuesForCreation(),
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
51 ];
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
52
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
53 foreach ($replacements as $key => &$replacement) {
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
54 $replacement = $replacement."\n".$key;
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
55 }
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 return $replacements;
769a17898cc0 Various changes to the generators and replacers - probably mostly just formatting
luka
parents: 6
diff changeset
58 }
5
b0b2e79ad8e6 Not exatly sure what was changed but commiting to it :)
luka
parents:
diff changeset
59 }