PHP Classes

File: VOFormatter.class.php

Recommend this page to a friend!
  Classes of Christian Velin   DbCodeBuilder   VOFormatter.class.php   Download  
File: VOFormatter.class.php
Role: Class source
Content type: text/plain
Description: Code generator for VOs
Class: DbCodeBuilder
Generate code to access databases as data objects
Author: By
Last change: Changed license type
Date: 16 years ago
Size: 4,025 bytes
 

Contents

Class file image Download
<?php
/**
 * A class to generate value objects from database tables.
 *
 * @author Christian Velin, christian.velin@conjurer.org
 * @version 0.2
 * @since 0.1 Added phpdoc tags to each generated variable and method.
 * @package DbCodeBuilder
 *
 * Copyright (C) 2007 Christian Velin
 *
 * This program is free software; distributed under the artistic license.
 */
class VOFormatter
{
   
/**
     * The String variable to store all the generated code.
     *
     * @var String
     */
   
private $output;
   
   
    public function
__construct($tables, $columns)
    {
       
$this -> generate($tables, $columns);
    }
   
   
   
   
/**
     * Master method to produce workable VO's to work with DAO's.
     *
     * @param Array $tables array of the tables in the db.
     * @param Array $columns recursive array containing all column information.
     */
   
private function generate($tables, $columns)
    {
        for (
$i = 0; $i < sizeOf($tables); $i++)
        {
           
$this -> output .= '============ ' . ucfirst($tables[$i]) . '.VO.php ============<br><br>';
           
$this -> output .= 'class ' . $tables[$i] . 'VO<br>{<br>';
           
$this -> generateVariables($i, $columns);
           
$this -> generateSetters($i, $columns);
           
$this -> generateGetters($i, $columns);
           
$this -> output .= '}<br>';
           
$this -> output .= '============ ' . ucfirst($tables[$i]) . '.VO.php ============<br><br>';
        }
    }
   
   
   
   
/**
     * A method to generate code for the needed variables.
     *
     * @param Integer $i keeps track of which table for which we are constructing a VO.
     * @param Array $columns recursive array containing all column information.
     */
   
private function generateVariables($i, $columns)
    {
        for (
$j = 0; $j < sizeOf($columns[$i]); $j++)
        {
           
$this -> output .= '/**<br>';
           
$this -> output .= ' * Enter description here...<br>';
           
$this -> output .= ' *<br>';
           
$this -> output .= ' * @var ' .$columns[$i][$j]['Type'] . '<br>';
           
$this -> output .= ' */<br>';

           
$this -> output .= 'private $' .$columns[$i][$j]['Field'] .';<br><br>';
        }
       
$this -> output .= '<br><br>';
    }
   
   
   
   
/**
     * Method to generate the 'setter' methods.
     *
     * @param Integer $i keeps track of which table for which we are constructing a VO.
     * @param Array $columns recursive array containing all column information.
     */
   
private function generateSetters($i, $columns)
    {
        for (
$j = 0; $j < sizeOf($columns[$i]); $j++)
        {
           
$this -> output .= '/**<br>';
           
$this -> output .= ' * Enter description here...<br>';
           
$this -> output .= ' *<br>';
           
$this -> output .= ' * @param ' .$columns[$i][$j]['Type'] . ' $' .$columns[$i][$j]['Field'] . '<br>';
           
$this -> output .= ' */<br>';
           
$this -> output .= 'public function set' . ucfirst($columns[$i][$j]['Field']) . '($' . $columns[$i][$j]['Field'] . ')<br>';
           
$this -> output .= '{<br>';
           
$this -> output .= '$this -> ' . $columns[$i][$j]['Field'] . ' = $' . $columns[$i][$j]['Field'] . ';<br>';
           
$this -> output .= '}<br><br><br><br>';
        }
    }
   
   
   
   
/**
     * Method to generate the 'getter' methods.
     *
     * @param Integer $i keeps track of which table for which we are constructing a VO.
     * @param Array $columns recursive array containing all column information.
     */
   
private function generateGetters($i, $columns)
    {
        for (
$j = 0; $j < sizeOf($columns[$i]); $j++)
        {
           
$this -> output .= '/**<br>';
            
$this -> output .= '* Enter description here...<br>';
           
$this -> output .= ' *<br>';
           
$this -> output .= ' * @return ' .$columns[$i][$j]['Type'] . '<br>';
           
$this -> output .= ' */<br>';
           
$this -> output .= 'public function get' . ucfirst($columns[$i][$j]['Field']) . '()<br>';
           
$this -> output .= '{<br>';
           
$this -> output .= 'return $this -> ' . $columns[$i][$j]['Field'] . ';<br>';
           
$this -> output .= '}<br><br><br><br>';
        }
    }
   
   
   
   
/**
     * Method to get the generated code.
     *
     * @return String the generated VO(s).
     */
       
public function getOutput()
    {
        return
$this -> output;
    }
}
?>