Naohiro19

Auto Generator Model for database

Jun 19th, 2023
3,115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.82 KB | None | 0 0
  1. <?php
  2.  
  3. class ModelGenerator {
  4.     private $tableName;
  5.     private $columns;
  6.  
  7.     public function __construct($tableName, $columns) {
  8.         $this->tableName = $tableName;
  9.         $this->columns = $columns;
  10.     }
  11.  
  12.     private function convertToPascalCase($str) {
  13.         return str_replace(' ', '', ucwords(str_replace('_', ' ', $str)));
  14.     }
  15.  
  16.     private function convertToCamelCase($str) {
  17.         $str = $this->convertToPascalCase($str);
  18.         return lcfirst($str);
  19.     }
  20.  
  21.     public function generateModel() {
  22.         $modelName = $this->convertToPascalCase($this->tableName);
  23.  
  24.         $code = "<?php\n\n";
  25.         $code .= "/**\n";
  26.         $code .= " * This file was automatically generated by ModelGenerator.\n";
  27.         $code .= " * Generated on: " . date('Y-m-d H:i:s') . "\n";
  28.         $code .= " */\n\n";
  29.         $code .= "class $modelName\n";
  30.         $code .= "{\n";
  31.  
  32.         foreach ($this->columns as $columnName => $columnType) {
  33.             $propertyName = $this->convertToCamelCase($columnName);
  34.             $code .= "\tprivate $$propertyName;\n\n";
  35.             $code .= "\tpublic function get" . ucfirst($propertyName) . "()\n";
  36.             $code .= "\t{\n";
  37.             $code .= "\t\treturn \$this->$propertyName;\n";
  38.             $code .= "\t}\n\n";
  39.             $code .= "\tpublic function set" . ucfirst($propertyName) . "(\$value)\n";
  40.             $code .= "\t{\n";
  41.             $code .= "\t\t\$this->$propertyName = \$value;\n";
  42.             $code .= "\t}\n\n";
  43.         }
  44.  
  45.         $code .= "}\n";
  46.  
  47.         return $code;
  48.     }
  49. }
  50.  
  51. // Read the JSON file path from command-line arguments
  52. if (count($argv) < 2) {
  53.     echo "Usage: php ModelGenerator.php <json_file_path>" . PHP_EOL;
  54.     exit(1);
  55. }
  56.  
  57. $jsonFilePath = $argv[1];
  58.  
  59. if (!file_exists($jsonFilePath)) {
  60.     echo "Error: JSON file not found: $jsonFilePath" . PHP_EOL;
  61.     exit(1);
  62. }
  63.  
  64. // Read the JSON file
  65. $jsonData = file_get_contents($jsonFilePath);
  66. $tableData = json_decode($jsonData, true);
  67.  
  68. if ($tableData === null) {
  69.     echo "Error: Invalid JSON format" . PHP_EOL;
  70.     exit(1);
  71. }
  72.  
  73. // Extract table name and columns from JSON data
  74. $tableName = key($tableData);
  75. $columns = $tableData[$tableName];
  76.  
  77. // Create an instance of ModelGenerator
  78. $modelGenerator = new ModelGenerator($tableName, $columns);
  79.  
  80. // Generate the model code
  81. $modelCode = $modelGenerator->generateModel();
  82.  
  83. // Get the directory of the JSON file
  84. $jsonFileDir = dirname($jsonFilePath);
  85.  
  86. // Specify the output file path
  87. $outputFilePath = $jsonFileDir . '/' . ucfirst($tableName) . '.php';
  88.  
  89. // Output the generated code to a file
  90. if (file_put_contents($outputFilePath, $modelCode) === false) {
  91.     echo "Error: Failed to write the output file: $outputFilePath" . PHP_EOL;
  92.     exit(1);
  93. }
  94.  
  95. echo "Model class generated successfully: $outputFilePath" . PHP_EOL;
  96.  
Advertisement
Add Comment
Please, Sign In to add comment