Advertisement
linuxyamigos

Untitled

Dec 29th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.45 KB | None | 0 0
  1. <?php
  2.  
  3. class Schema {
  4.     public $name;
  5.     public $engine;
  6.     public $charset = 'utf8';
  7.     public $collation;
  8.    
  9.     protected $fields = [];
  10.     protected $current_field;
  11.    
  12.     function __construct($name){
  13.         $this->name = $name;
  14.     }  
  15.    
  16.     // type
  17.    
  18.     function integer($name){
  19.         $this->current_field = $name;
  20.         $this->fields[$this->current_field] = [];
  21.         $this->fields[$this->current_field]['type'] = 'integer';
  22.         return $this;      
  23.     }  
  24.    
  25.     function varchar($name){
  26.         $this->current_field = $name;
  27.         $this->fields[$this->current_field] = [];
  28.         $this->fields[$this->current_field]['type'] = 'varchar';
  29.         return $this;      
  30.     }  
  31.    
  32.     // modifiers
  33.    
  34.     function auto(){
  35.         // chequear si es un entero
  36.         $this->fields[$this->current_field]['auto'] =  true;
  37.         return $this;
  38.     }
  39.    
  40.     function unsigned(){
  41.         // chequear si es un entero
  42.         $this->fields[$this->current_field]['unsigned'] =  true;
  43.         return $this;
  44.     }
  45.    
  46.     function nullable(){
  47.         // chequear si es un entero
  48.         $this->fields[$this->current_field]['nullable'] =  true;
  49.         return $this;
  50.     }
  51.    
  52.    
  53.     // reflexion
  54.    
  55.     function getSchema(){
  56.         return $this->fields;
  57.     }
  58. }
  59.  
  60.  
  61. $table = new Schema('test');
  62.  
  63. $table->name      = 'users';
  64. $table->engine    = 'InnoDB';
  65. $table->charset   = 'utf8';
  66. $table->collation = 'utf8_general_ci';
  67.  
  68. $table->integer('id')->auto()->unsigned();
  69. $table->varchar('firstname');
  70. $table->varchar('lastname')->nullable();
  71. // ..
  72.  
  73. echo '<pre>';
  74. var_dump($table->getSchema());
  75. echo '</pre>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement