Location: PHPKode > scripts > Scrubs > scrubs/ClassWriter.php
<?php

/**
 * 
 *
 * @version $Id$
 * @copyright 2003 
 **/

class classWriter {

	var $className    = "";
	var $includes     = array();
	var $properties   = array();
	var $relationship = array();
	var $objectArray  = array();
	
	function classWriter($_className) {
		$this->className = ucfirst($_className);
	}
	
	function setProperty($_property) {
		$this->properties[] = $_property;
	}
	
	function setInclude($_include) {
		$this->includes[] = ucfirst($_include);
	}
	
	function setRelationship($_relationship){
		$this->relationship[] = $_relationship;
	}
	
	function setObjectArray($_objectArray) {
		$this->objectArray[] = $_objectArray;
	}
	
	function write() {
		ob_start();
		echo "<?php\n\n";
		echo "/**\n * \n *created by Scrubs DataObject Generator\n * \n **/\n\n";
		echo "define('SCRUBS', dirname(dirname(__FILE__)));\n";
		echo "include_once(SCRUBS.'/core/BaseObject.php');\n";
		foreach($this->includes as $include){		
			echo "include_once('".$include.".php');\n";
		}
		echo "\n";
		echo "class $this->className extends BaseObject {\n\n";
		foreach($this->properties as $property) {
			echo "  var $$property = \"\";\n";
		}
		if (!empty($this->relationship)) {
			for($i = 0; $i < sizeof($this->relationship); $i++) {
				echo "  var \${$this->relationship[$i]} = \"\";\n";
				echo "  var \${$this->objectArray[$i]} = array();\n";
			}			
			echo "\n";
		} else {
			echo "\n";
		}
		echo "  function $this->className() {\n";
		echo "    BaseObject::BaseObject();\n";
		if(!empty($this->relationship)) {
			for($i = 0; $i < sizeof($this->relationship); $i++) {
				echo "    \$this->{$this->relationship[$i]} = new ".ucfirst($this->relationship[$i])."();\n";
			}
		}
		echo "  }\n\n";
		foreach($this->properties as $property) {
			echo "  function set".ucfirst($property)."(\$_$property) {\n";
			echo "    \$this->$property = \$_$property;\n";
			echo "  }\n";
			echo "  function get".ucfirst($property)."() {\n";
			echo "    return \$this->$property;\n";
			echo "  }\n\n";
		}
		if (!empty($this->objectArray)) {
			for($i = 0; $i < sizeof($this->objectArray); $i++) {
				echo "  function set".ucfirst($this->objectArray[$i])."(\$_{$this->objectArray[$i]}) {\n";
				echo "    \$this->{$this->objectArray[$i]} = \$_{$this->objectArray[$i]};\n";
				echo "  }\n";
				echo "  function get".ucfirst($this->objectArray[$i])."() {\n";
				echo "    return \$this->{$this->objectArray[$i]};\n";
				echo "  }\n\n";
			}		
		} else {
			echo "\n";
		}
		echo "}\n";
		echo "?>";    
		
		$classFile = ucfirst($this->className).".php";
		$fp = fopen("classes/".$classFile, "w");
    fwrite($fp, ob_get_clean());
    fclose($fp);
		ob_end_flush();
	}

}
?>
Return current item: Scrubs