|
<?php |
|
|
|
// config |
|
$config= array( |
|
'vis'=> array( 'public', 'protected', 'private' ) |
|
); |
|
|
|
// class |
|
class ClassGen{ |
|
|
|
public $className; |
|
public $vis; |
|
public $properties; |
|
|
|
public function askClassName(){ |
|
fwrite(STDOUT, "Enter your class name:"); |
|
$this->className = trim(fgets(STDIN)); |
|
} |
|
|
|
public function askVis(){ |
|
$vis_list=""; |
|
global $config; |
|
foreach($config['vis'] as $k=>$v){ |
|
$vis_list.= "\n$k:$v"; |
|
} |
|
fwrite(STDOUT, "Enter the visibility of the class properties:$vis_list\n"); |
|
$i= trim(fgets(STDIN)); |
|
$accept= array(0,1,2); |
|
if ( in_array($i, $accept) ) { |
|
$this->vis = $config['vis'][$i]; |
|
} |
|
else { |
|
fwrite(STDOUT, "Please enter either 0 1 or 2:\n"); |
|
$this->askVis(); |
|
} |
|
} |
|
|
|
public function askProperties(){ |
|
fwrite(STDOUT, "Enter a comma seperated list of properties for the class:\n"); |
|
$this->properties = trim(fgets(STDIN)); |
|
} |
|
|
|
public function writeClass(){ |
|
$className= ucfirst($this->className); |
|
$fh= fopen($className.'.class.php', "w"); |
|
fwrite($fh,"<?php \n"); |
|
fwrite($fh, "class $className{\n\n"); |
|
|
|
if (strpos(',', $this->properties)!==-1) $arrProperties= explode(',', $this->properties); |
|
else { |
|
// Make sure we just get first valid word if incorrectly formatted string is provided |
|
preg_match('/(\b\w+\b)/', $this->properties, $matches); |
|
$arrProperties= array( $matches[0] ); |
|
} |
|
$arrProperties = array_map('strtolower', $arrProperties); // make sure all lowercase |
|
$arrProperties = array_map('trim', $arrProperties); // trim whitespace |
|
|
|
foreach($arrProperties as $k=>$v){ |
|
fwrite($fh, "\t".$this->vis." ".$v.";\n"); |
|
} |
|
|
|
fwrite($fh, "\n"); |
|
foreach($arrProperties as $k=>$v){ |
|
// getter |
|
fwrite($fh, "\tpublic function get".ucfirst($v)."(){\n"); |
|
fwrite($fh, "\t\treturn \$this->".$v.";\n"); |
|
fwrite($fh, "\t}\n"); |
|
|
|
// setter |
|
fwrite($fh, "\tpublic function set".ucfirst($v)."(\$$v){\n"); |
|
fwrite($fh, "\t\t\$this->".$v."=$v;\n"); |
|
fwrite($fh, "\t}\n"); |
|
} |
|
|
|
fwrite($fh, "\n}\n"); |
|
fwrite($fh,"?>"); |
|
fclose($fh); |
|
} |
|
|
|
} |
|
|
|
$myClassGen= new ClassGen(); |
|
$myClassGen->askClassName(); |
|
fwrite(STDOUT, "Class equals: $myClassGen->className\n"); |
|
$myClassGen->askVis(); |
|
fwrite(STDOUT, "Vis equals: $myClassGen->vis\n"); |
|
$myClassGen->askProperties(); |
|
$myClassGen->writeClass(); |
|
?> |