コード例 #1
0
 /**
  * Implements the loading of the class object
  * @throws Exception if the class is already generated(not null)
  */
 protected function generateClass()
 {
     if ($this->class != null) {
         throw new Exception("The class has already been generated");
     }
     $config = Generator::getInstance()->getConfig();
     $class = new PhpClass($this->phpIdentifier, $config->getClassExists());
     $constructorComment = new PhpDocComment();
     $constructorComment->setAccess(PhpDocElementFactory::getPublicAccess());
     $constructorSource = '';
     $constructorParameters = '';
     // Add member variables
     foreach ($this->members as $member) {
         $type = '';
         try {
             $type = Validator::validateType($member->getType());
         } catch (ValidationException $e) {
             $type .= 'Custom';
         }
         $name = Validator::validateNamingConvention($member->getName());
         $comment = new PhpDocComment();
         $comment->setVar(PhpDocElementFactory::getVar($type, $name, ''));
         $comment->setAccess(PhpDocElementFactory::getPublicAccess());
         $var = new PhpVariable('public', $name, '', $comment);
         $class->addVariable($var);
         $constructorSource .= '  $this->' . $name . ' = $' . $name . ';' . PHP_EOL;
         $constructorComment->addParam(PhpDocElementFactory::getParam($type, $name, ''));
         $constructorComment->setAccess(PhpDocElementFactory::getPublicAccess());
         $constructorParameters .= ', $' . $name;
     }
     $constructorParameters = substr($constructorParameters, 2);
     // Remove first comma
     $function = new PhpFunction('public', '__construct', $constructorParameters, $constructorSource, $constructorComment);
     // Only add the constructor if type constructor is selected
     if ($config->getNoTypeConstructor() == false) {
         $class->addFunction($function);
     }
     $this->class = $class;
 }
コード例 #2
0
ファイル: gen.php プロジェクト: nuth/php-mysql-row2object
                case "decimal":
                    $default = "{$field['Default']}";
                    break;
                default:
                    $default = "\"{$field['Default']}\"";
            }
        }
        $variable = new PhpVariable('public', $field['Field'], $default);
        $getter = new PhpFunction('public', "set{$field['Field']}", "\${$field['Field']}", "  \$this->{$field['Field']} = \${$field['Field']};\n  return \$this;");
        $setter = new PhpFunction('public', "get{$field['Field']}", "", "  return \$this->{$field['Field']};");
        if ('PRI' == $field['Key']) {
            $primary_key[] = "\${$field['Field']}";
            $const_source[] = "  \$this->{$field['Field']} = \${$field['Field']}";
        } else {
            if ('NO' == $field['Null']) {
                if ($field['Default'] != NULL) {
                    $const_params_def[] = "\${$field['Field']} = {$default}";
                } else {
                    $const_params[] = "\${$field['Field']}";
                }
                $const_source[] = "  \$this->{$field['Field']} = \${$field['Field']}";
            }
        }
        $classd->addVariable($variable);
        $classd->addFunction($getter);
        $classd->addFunction($setter);
    }
    $function = new PhpFunction('public', '__constructor', implode(', ', array_merge($primary_key, $const_params, $const_params_def)), implode(";\n", $const_source) . ';');
    echo $classd->addFunction($function);
    echo $classd->getSource();
}