Location: PHPKode > scripts > XML DAO Builder > xml-dao-builder/table_class.php
<?php
   /**
    * XMLColumn
    * 
    * class to represent a table column
    * 
    * @author Adolfo Eloy
    * @version 0.1
    * @package XMLTableClass
    *
    */
   class XMLColumn 
   {
      protected $column_name      = "";
      protected $column_data_type = null;
      protected $column_size      = null;
      
      public static $TEXT    = 1;
      public static $NUMERIC = 2;
      public static $DATE    = 3;
      
      public function get_column_name() { return $this->column_name; }
      public function get_column_data_type() { return $this->column_data_type; }
      public function get_column_size() { return $this->column_size; }
      
      /**
       * constructor
       *
       * @param string $n column name
       * @param integer $d data type( use static XMLColumn attributes )
       * @param integer $s column size 
       */
      public function __construct( $n, $d, $s = "" ) {
         $this->column_name = $n;
         $this->column_data_type = $d;
         $this->column_size = $s;
      }
   }
   
   /**
    * XMLTable
    * 
    * class to represent a table 
    * 
    * @author Adolfo Eloy
    * @version 0.1
    * @package XMLTableClass
    *
    */
   class XMLTable 
   {
   	/**
   	  * table name
   	  * @var string
   	*/
      protected $table_name = "";
      
      /**
        * defined columns
        * @var XMLColumn[] 
      */
      protected $columns    = array();

      public function get_table_name() { return $this->table_name; }
      public function get_columns() { return $this->columns; }
      
      public function __construct( $name ) {
         $this->table_name = $name;
      }
      
      /**
       * add a column to this table
       *
       * @param XMLColumn $column
       */
      public function add_column( $column ) {
         array_push( $this->columns, $column );
      }
      
   }
   
   /**
    * XMLTableExtract
    * 
    * provides method for extrating data from XML table representation
    * and save it into XMLTable model
    * 
    * @author Adolfo Eloy
    * @version 0.1
    * @package XMLTableClass
    *
    */
   class XMLTableExtract {
      
      protected $table = null;
      
      /**
       * load_xml
       * 
       * method to load given XML path, load it and build the XMLTable object
       *
       * @param string $path
       * @return XMLTable
       */
      public function load_xml( $path ) {
         
         $xml = new DOMDocument();
         $xml->load( $path );

			$table  = $xml->getElementsByTagName("TABLE")->item(0);
         $table_name = $table->getAttribute("NAME");
         
         $xmltable = new XMLTable( $table_name );
         
         $columns = $table->getElementsByTagName("COLUMN");
         foreach( $columns as $column ) {
            $name = $column->getAttribute('NAME');
            $type = $column->getAttribute('TYPE');
            $size = $column->getAttribute('SIZE');
            
				$xmltable->add_column( new XMLColumn( $name, $type, $size ) );            
         }
         
         return $xmltable;
      }
   }

   /**
    * XMLClassMaker
    * 
    * provides the method for building the PHP class
    * 
    * @author Adolfo Eloy
    * @version 0.1
    * @package XMLTableClass
    *
    */
   class XMLClassMaker 
   {
   	  /**
   	   * using the XMLTable object, builds the PHP class into the given path 
   	   *
   	   * @param XMLTable $xmltable
   	   * @param string $path
   	   */
      public function build( $xmltable, $path ) {
         $r = "<?\n";
         
         // writing the class string
         $g = ""; // getters
         $s = ""; // setters
         
         $t = $xmltable->get_table_name();
         $vt = explode("_", $t);
         if (count($vt) > 1) { 
            foreach($vt as $word) {
               $table_name .= ucwords(strtolower($word));
            }
         } else {
            $table_name = ucwords(strtolower($t));
         }
         
         $r .= "class " . $table_name . " {\n";
         
         $cols = $xmltable->get_columns();
         foreach( $cols as $c ) {
         	$cn = strtolower($c->get_column_name());
         	
            $r .= "\tprivate \$" . $cn . ";\n";
            
            $g .= "\tpublic function get_" . $cn . "() {\n";
            $g .= "\t\treturn \$this->" . $cn . ";\n";
            $g .= "\t}\n\n";

            $s .= "\tpublic function set_" . $cn . "( \$v ) {\n";
            $s .= "\t\t\$this->" . $cn . " = \$v;\n";
            $s .= "\t}\n\n";
            
         }
         
         $r .= "\n";
         $r .= $g;
         $r .= $s;
         
         $r .= "}\n?>";
         
         // writing to file 
         $file = fopen( $path, "w" );
         if ($file) {
            fwrite($file, $r);
            fclose($file);
         }
         
      }
   }
?>
Return current item: XML DAO Builder