Esempio n. 1
0
 public function generate()
 {
     // Start creating the PHP
     $indent = 1;
     $output = '<?php' . "\n\n";
     // TODO PHPDOC HERE
     $output .= returnIndent($indent) . 'class Application_Model_' . $this->_name . "\n";
     $output .= returnIndent($indent) . '{' . "\n\n";
     $indent++;
     // Write the properties
     foreach ($this->_fields as $field) {
         $output .= returnIndent($indent) . 'private $_' . $field->getName() . ';' . "\n";
     }
     $output .= "\n\n\n\n";
     // Start writing the getters and setters
     foreach ($this->_fields as $field) {
         $output .= $field->createSetter($indent);
         $output .= $field->createGetter($indent);
     }
     // Close class
     $indent--;
     $output .= returnIndent($indent) . '}' . "\n";
     $this->_output = $output;
     return true;
 }
Esempio n. 2
0
 public function generate()
 {
     $indent = 1;
     $output = '';
     $output .= returnIndent($indent) . "<?php \n\n";
     $output .= returnIndent($indent) . "/** \n";
     $output .= returnIndent($indent) . " * {$this->_name}Test \n";
     $output .= returnIndent($indent) . " * \n";
     $output .= returnIndent($indent) . " * Class description goes here... \n";
     $output .= returnIndent($indent) . " * \n";
     $output .= returnIndent($indent) . " */ \n";
     $output .= returnIndent($indent) . "class {$this->_name}Test extends PHPUnit_Framework_TestCase \n";
     $output .= returnIndent($indent) . "{ \n\n\n";
     $indent++;
     $output .= returnIndent($indent) . "public function test{$this->_name}() { \n\n";
     $indent++;
     $output .= returnIndent($indent) . "\$new{$this->_name} = new Application_Model_{$this->_name}; \n";
     $output .= returnIndent($indent) . "\$sampleDateTime = new DateTime(); \n";
     $output .= returnIndent($indent) . "\$sampleDateTime->setTimestamp( strtotime( 'now' ) ); \n\n";
     $sampleString = '"Sample Text"';
     $sampleInt = '1';
     $sampleBool = 'true';
     foreach ($this->_fields as $field) {
         if ($field->isPrimary()) {
             continue;
         }
         switch ($field->getTypeCast()) {
             case 'string':
                 $output .= returnIndent($indent) . "\$new{$this->_name}->set{$field->getName(true)}( {$sampleString} ); \n";
                 $output .= returnIndent($indent) . "\$this->assertEquals( {$sampleString}, \$new{$this->_name}->get{$field->getName(true)}() ); \n";
                 break;
             case 'bool':
                 $output .= returnIndent($indent) . "\$new{$this->_name}->set{$field->getName(true)}( {$sampleBool} ); \n";
                 $output .= returnIndent($indent) . "\$this->assertEquals( {$sampleBool}, \$new{$this->_name}->get{$field->getName(true)}() ); \n";
                 break;
             case 'int':
                 $output .= returnIndent($indent) . "\$new{$this->_name}->set{$field->getName(true)}( {$sampleInt} ); \n";
                 $output .= returnIndent($indent) . "\$this->assertEquals( {$sampleInt}, \$new{$this->_name}->get{$field->getName(true)}() ); \n";
                 break;
         }
         if ($field->isDateTime()) {
             $output .= returnIndent($indent) . "\$new{$this->_name}->set{$field->getName(true)}( \$sampleDateTime ); \n";
             $output .= returnIndent($indent) . "\$this->assertEquals( \$sampleDateTime, \$new{$this->_name}->get{$field->getName(true)}() ); \n";
         }
     }
     $primaryField = $this->findPrimaryKeyField();
     $output .= "\n";
     $output .= returnIndent($indent) . "\$mapper = new Application_Model_{$this->_name}Mapper(); \n";
     $output .= returnIndent($indent) . "\$stored = \$mapper->store( \$new{$this->_name} ); \n\n";
     $output .= returnIndent($indent) . "\$this->assertInstanceOf( 'Application_Model_{$this->_name}', \$stored ); \n\n";
     $sampleString = "'Updated Text'";
     // Test update
     foreach ($this->_fields as $field) {
         if ($field->isPrimary()) {
             continue;
         }
         if ($field->getTypeCast() == 'string') {
             $output .= returnIndent($indent) . "\$stored->set{$field->getName(true)}( {$sampleString} ); \n";
             $output .= returnIndent($indent) . "\$this->assertEquals( {$sampleString}, \$new{$this->_name}->get{$field->getName(true)}() ); \n";
         }
     }
     $output .= returnIndent($indent) . "\$updated = \$mapper->store( \$stored ); \n\n";
     $output .= returnIndent($indent) . "\$this->assertInstanceOf( 'Application_Model_{$this->_name}', \$updated ); \n\n";
     $output .= returnIndent($indent) . "\$got = \$mapper->getBy{$primaryField->getName(true)}( \$updated->get{$primaryField->getName(true)}() ); \n";
     $output .= returnIndent($indent) . "\$mapper->delete( \$got ); \n\n";
     $indent--;
     $output .= returnIndent($indent) . "} \n\n\n\n\n";
     $indent--;
     $output .= returnIndent($indent) . "} \n";
     $this->_output = $output;
 }
Esempio n. 3
0
 public function createSetter($indent)
 {
     $output = returnIndent($indent) . '/**' . "\n";
     $output .= returnIndent($indent) . ' * set' . $this->_ucfName . "\n";
     $output .= returnIndent($indent) . ' * ' . "\n";
     $output .= returnIndent($indent) . ' * Explaination goes here..' . "\n";
     $output .= returnIndent($indent) . ' * ' . "\n";
     $output .= returnIndent($indent) . ' * @param ' . $this->_typeInfo->human . " \${$this->_name} \n";
     $output .= returnIndent($indent) . ' * @throws InvalidArgumentException' . "\n";
     $output .= returnIndent($indent) . ' */' . "\n";
     $output .= returnIndent($indent) . 'public function set' . $this->_ucfName . '( $' . $this->_name . ' ) {' . "\n\n";
     $indent++;
     $output .= returnIndent($indent) . 'if(! ' . $this->_typeInfo->validation . ')' . "\n";
     $output .= returnIndent($indent + 1) . 'throw new InvalidArgumentException( \'' . $this->_name . ' must be of type: ' . $this->_typeInfo->human . '\' );' . "\n\n";
     $output .= returnIndent($indent) . '$this->_' . $this->_name . ' = $' . $this->_name . ";\n";
     $indent--;
     $output .= returnIndent($indent) . '}' . "\n\n\n\n\n";
     return $output;
 }
Esempio n. 4
0
 public function generate()
 {
     $indent = 1;
     $output = '';
     $output .= returnIndent($indent) . "<?php \n\n";
     $output .= returnIndent($indent) . "/** \n";
     $output .= returnIndent($indent) . " * Application_Model_{$this->_name}Mapper \n";
     $output .= returnIndent($indent) . " * \n";
     $output .= returnIndent($indent) . " * Class description goes here... \n";
     $output .= returnIndent($indent) . " * \n";
     $output .= returnIndent($indent) . " */ \n";
     $output .= returnIndent($indent) . "class Application_Model_{$this->_name}Mapper \n";
     $output .= returnIndent($indent) . "{ \n\n\n";
     $indent++;
     $output .= returnIndent($indent) . "/** \n";
     $output .= returnIndent($indent) . " * _dbTable \n";
     $output .= returnIndent($indent) . " * \n";
     $output .= returnIndent($indent) . " * The {$this->_name} table object \n";
     $output .= returnIndent($indent) . " * \n";
     $output .= returnIndent($indent) . " * @var Zend_Db_Table \n";
     $output .= returnIndent($indent) . " */ \n";
     $output .= returnIndent($indent) . 'private $_dbTable;' . "\n\n\n\n\n";
     $output .= returnIndent($indent) . "/** \n";
     $output .= returnIndent($indent) . " * getDbTable \n";
     $output .= returnIndent($indent) . " * \n";
     $output .= returnIndent($indent) . " * Get the {$this->_name} Table object. \n";
     $output .= returnIndent($indent) . " * \n";
     $output .= returnIndent($indent) . " * @return Zend_Db_Table \n";
     $output .= returnIndent($indent) . " */ \n";
     $output .= returnIndent($indent) . "private function getDbTable() { \n\n";
     $indent++;
     $output .= returnIndent($indent) . 'if (is_null( $this->_dbTable ))' . "\n";
     $indent++;
     $output .= returnIndent($indent) . '$this->_dbTable = new Zend_Db_Table( \'' . $this->_name . '\' );' . " \n";
     $indent--;
     $output .= returnIndent($indent) . 'return $this->_dbTable;' . "\n";
     $indent--;
     $output .= returnIndent($indent) . "} \n\n\n\n\n";
     $primaryField = $this->findPrimaryKeyField();
     $output .= returnIndent($indent) . "/** \n";
     $output .= returnIndent($indent) . " * getBy{$primaryField->getName(true)} \n";
     $output .= returnIndent($indent) . " * \n";
     $output .= returnIndent($indent) . " * Get an Application_Model_{$this->_name} object by its {$primaryField->getName()}. \n";
     $output .= returnIndent($indent) . " * \n";
     $output .= returnIndent($indent) . " * @return Application_Model_{$this->_name} \n";
     $output .= returnIndent($indent) . " */ \n";
     $output .= returnIndent($indent) . "public function getBy{$primaryField->getName(true)}( \${$primaryField->getName()} ) { \n\n";
     $indent++;
     $output .= returnIndent($indent) . '$dbTable = $this->getDbTable();' . "\n";
     $output .= returnIndent($indent) . '$data = $dbTable->fetchRow( ' . "\"{$primaryField->getRawName()}={\${$primaryField->getName()}}\" ); \n\n";
     $output .= returnIndent($indent) . "\$new{$this->_name} = new Application_Model_{$this->_name}(); \n";
     // Now for the tricky part
     // TODO some entries might be null, do an isset()
     foreach ($this->_fields as $field) {
         // Is it a DateTime?
         $isDateTime = false;
         if ($field->getTypeCast() === false) {
             $isDateTime = true;
         }
         // Do we need to typeCast at all?
         $doTypeCast = false;
         if ($field->getTypeCast() !== 'string') {
             $doTypeCast = true;
         }
         // Do it
         if ($isDateTime) {
             $output .= returnIndent($indent) . "\n";
             $output .= returnIndent($indent) . "\${$field->getName()} = new DateTime(); \n";
             $output .= returnIndent($indent) . "\${$field->getName()}->setTimestamp( strtotime( \$data['{$field->getRawName()}'] ) ); \n";
             $output .= returnIndent($indent) . "\$new{$this->_name}->set{$field->getName(true)}( \${$field->getName()} );\n";
         } else {
             $output .= returnIndent($indent) . "\$new{$this->_name}->set{$field->getName(true)}( " . ($doTypeCast ? "({$field->getTypeCast()}) " : '') . "\$data['{$field->getRawName()}'] ); \n";
         }
     }
     $output .= returnIndent($indent) . "\n";
     $output .= returnIndent($indent) . "return \$new{$this->_name};\n";
     $indent--;
     $output .= returnIndent($indent) . "} \n\n\n\n\n";
     $output .= returnIndent($indent) . "/** \n";
     $output .= returnIndent($indent) . " * store \n";
     $output .= returnIndent($indent) . " * \n";
     $output .= returnIndent($indent) . " * Store an Application_Model_{$this->_name} object to the db. \n";
     $output .= returnIndent($indent) . " * The returned object will have its {$primaryField->getName()} set to the insert id. \n";
     $output .= returnIndent($indent) . " * \n";
     $output .= returnIndent($indent) . " * @param Application_Model_{$this->_name} \$new{$this->_name} \n";
     $output .= returnIndent($indent) . " * @throws InvalidArgumentException \n";
     $output .= returnIndent($indent) . " * @return Application_Model_{$this->_name} \n";
     $output .= returnIndent($indent) . " */ \n";
     $output .= returnIndent($indent) . "public function store( Application_Model_{$this->_name} \$new{$this->_name} ) { \n\n";
     $indent++;
     $output .= returnIndent($indent) . "// Check for nulls \n";
     // I know this is wasteful but its a lot easier to follow
     foreach ($this->_fields as $field) {
         if ($field->allowsNull()) {
             $output .= returnIndent($indent) . "if (is_null( \$new{$this->_name}->get{$field->getName(true)}() )) \n";
             $output .= returnIndent($indent + 1) . "throw new InvalidArgumentException( 'Cannot store \$new{$this->_name}: {$field->getName()} must be set first.' ); \n";
         }
     }
     $output .= "\n\n";
     // Create the data map
     $output .= returnIndent($indent) . "\$dataMap = array( \n";
     for ($i = 0; $i < count($this->_fields); $i++) {
         $field = $this->_fields[$i];
         if ($field->isPrimary()) {
             continue;
         }
         $output .= returnIndent($indent + 1) . "'{$field->getRawName()}' => \$new{$this->_name}->get{$field->getName(true)}()";
         if ($field->isDateTime()) {
             $output .= "->format( 'Y-m-d H:i:s' )";
         }
         if ($i + 1 < count($this->_fields)) {
             $output .= ", \n";
         } else {
             $output .= "\n";
         }
     }
     $output .= returnIndent($indent) . "); \n\n";
     // Storage
     $output .= returnIndent($indent) . "if (is_int( \$new{$this->_name}->get{$primaryField->getName(true)}() )) { \n";
     $indent++;
     $output .= returnIndent($indent) . '$this->getDbTable()->update( $dataMap, "' . $primaryField->getRawName() . '={$new' . $this->_name . '->get' . $primaryField->getName(true) . '()}" );' . "\n";
     $indent--;
     $output .= returnIndent($indent) . "}\n";
     $output .= returnIndent($indent) . "else { \n";
     $indent++;
     $output .= returnIndent($indent) . '$insertId = $this->getDbTable()->insert( $dataMap );' . "\n";
     $output .= returnIndent($indent) . "\$new{$this->_name}->set{$primaryField->getName(true)}( \$insertId ); \n";
     $indent--;
     $output .= returnIndent($indent) . "}\n\n";
     $output .= returnIndent($indent) . "return \$new{$this->_name}; \n";
     $indent--;
     $output .= returnIndent($indent) . "}\n\n\n\n\n";
     $output .= returnIndent($indent) . "/** \n";
     $output .= returnIndent($indent) . " * delete() \n";
     $output .= returnIndent($indent) . " * \n";
     $output .= returnIndent($indent) . " * Delete an Application_Model_{$this->_name} object from the db. \n";
     $output .= returnIndent($indent) . " * \n";
     $output .= returnIndent($indent) . " * @param Application_Model_{$this->_name} \$my{$this->_name} \n";
     $output .= returnIndent($indent) . " * @throws InvalidArgumentException \n";
     $output .= returnIndent($indent) . " * @throws Zend_Db_Statement_Exception \n";
     $output .= returnIndent($indent) . " */ \n";
     $output .= returnIndent($indent) . "public function delete( Application_Model_{$this->_name} \$my{$this->_name} ) { \n\n";
     $indent++;
     $output .= returnIndent($indent) . " // Check the object exists in the DB \n";
     $output .= returnIndent($indent) . " if (! is_int( \$my{$this->_name}->get{$primaryField->getName(true)}() )) \n";
     $output .= returnIndent($indent + 1) . "throw new InvalidArgumentException( 'Cannot delete an object that doesnt exist in the DB.' ); \n\n";
     $output .= returnIndent($indent) . "if (! \$this->getDbTable()->delete( '{$primaryField->getRawName()}=' . \$my{$this->_name}->get{$primaryField->getName(true)}() )) \n";
     $output .= returnIndent($indent + 1) . "Zend_Db_Exception( 'Could not delete the {$this->_name}' ); \n\n";
     $indent--;
     $output .= returnIndent($indent) . "}\n\n\n\n\n";
     $indent--;
     $output .= returnIndent($indent) . "}\n";
     $this->_output = $output;
     return true;
 }