diff src/Replacer/TableReplacer.php @ 23:827efbf4d73c main-dev

Huge changes to the relationships for models and more complex
author Luka Sitas <sitas.luka.97@gmail.com>
date Fri, 11 Apr 2025 20:50:20 -0400
parents f0b0d014e448
children 8dd668020310
line wrap: on
line diff
--- a/src/Replacer/TableReplacer.php	Wed Feb 26 20:12:17 2025 -0500
+++ b/src/Replacer/TableReplacer.php	Fri Apr 11 20:50:20 2025 -0400
@@ -6,15 +6,22 @@
 {
     protected ?array $columns = null;
 
+    protected array $columns_to_ignore = [
+        'id',
+        'created_at',
+        'updated_at',
+        'created_by',
+        'updated_by',
+        'deleted_at',
+    ];
+
     /**
      * Retrieve columns for the current table.
-     *
-     * @return array
      */
     protected function get_columns(): array
     {
         if (is_null($this->columns)) {
-            $this->columns = $this->getCurrentTable()->getColumns();
+            $this->columns = $this->getTableColumns($this->getCurrentTable());
         }
 
         return $this->columns;
@@ -22,14 +29,13 @@
 
     /**
      * Get a string representation of values for creation.
-     *
-     * @return string
      */
     protected function getValuesForCreation(): string
     {
         $insert = '';
         foreach ($this->get_columns() as $column) {
-            $insert .= sprintf('$item->%s = $validated["%s"] ?? NULL;', $column->getName(), $column->getName()) . "\n";
+            $column_name = $column['name'];
+            $insert .= sprintf('$item->%s = $validated["%s"] ?? NULL;', $column_name, $column_name)."\n";
         }
 
         return $insert;
@@ -37,14 +43,31 @@
 
     /**
      * Get a string representation of table attributes.
-     *
-     * @return string
      */
     protected function getAttributes(): string
     {
         $insert = '';
         foreach ($this->get_columns() as $column) {
-            $insert .= sprintf("'%s' => '',", $column->getName()) . "\n";
+            if (in_array($column['name'], $this->columns_to_ignore)) {
+                continue;
+            }
+            $insert .= sprintf("'%s' => '',", $column['name'])."\n";
+        }
+
+        return $insert;
+    }
+
+    /**
+     * Get a string representation of table fillable columns.
+     */
+    protected function getFillable(): string
+    {
+        $insert = '';
+        foreach ($this->get_columns() as $column) {
+            if (in_array($column['name'], $this->columns_to_ignore)) {
+                continue;
+            }
+            $insert .= sprintf("'%s',", $column['name'])."\n";
         }
 
         return $insert;
@@ -52,14 +75,15 @@
 
     /**
      * Get formatted validation rules for table columns.
-     *
-     * @return string
      */
     protected function getValuesForValidation(): string
     {
         $insert = '';
         foreach ($this->get_columns() as $column) {
-            $insert .= sprintf("'%s' => 'nullable',", $column->getName()) . "\n";
+            if (in_array($column['name'], $this->columns_to_ignore)) {
+                continue;
+            }
+            $insert .= sprintf("'%s' => 'nullable',", $column['name'])."\n";
         }
 
         return $insert;
@@ -67,9 +91,6 @@
 
     /**
      * Apply insertions in the target template.
-     *
-     * @param string $target
-     * @return string
      */
     public function apply_inserts(string $target): string
     {
@@ -85,14 +106,13 @@
 
     /**
      * Get available insertion points for the template.
-     *
-     * @return array
      */
     public function get_available_inserts(): array
     {
         return [
             '// {{ valuesForCreation }}' => $this->getValuesForCreation(),
             '# {{ attributeInsertPoint }}' => $this->getAttributes(),
+            '# {{ fillableInsertPoint }}' => $this->getFillable(),
             '// {{ valuesForValidation }}' => $this->getValuesForValidation(),
         ];
     }