Example #1
0
 /**
  * @param \Model\Generator\Part\PartInterface $part
  */
 public function preRun(PartInterface $part)
 {
     /**
      * @var $part \Model\Generator\Part\Entity
      */
     /**
      * @var $file \Model\Code\Generator\FileGenerator
      */
     $file = $part->getFile();
     /**
      * @var $table \Model\Cluster\Schema\Table
      */
     $table = $part->getTable();
     if ($file->getClass()->getDocblock()) {
         $docBlock = $file->getClass()->getDocblock();
     } else {
         $docBlock = new DocBlockGenerator('Сущность ' . $table->getNameAsCamelCase());
         $file->getClass()->setDocblock($docBlock);
     }
     $columnList = $table->getColumn();
     $config = $part->getOption('config');
     $configFields = isset($config['fields']) ? $config['fields'] : array();
     $usedMethods = array();
     /** @var $columnList \Model\Cluster\Schema\Table\Column[]  */
     foreach ($columnList as $column) {
         foreach ($configFields as $configField) {
             if (isset($configField['match']) && ($columnConfig = $part->getColumntConfig($column))) {
                 /*foreach ($configField['match'] as $match) {
                                         $isMatched = false;
                                         if (isset($match['type'])) {
                                             $matchTypes = is_array($match['type']) ? $match['type'] : array($match['type']);
                                             $isMatched = in_array($column->getColumnType(), $matchTypes);
                                         }
                 
                                         $isMatched = $isMatched && preg_match($match['regexp'], $column->getFullName());
                 
                                         $columnLength = $column->getCharacterMaximumLength() ? $column->getCharacterMaximumLength() : $column->getNumericPrecision();
                 
                                         if ($isMatched && isset($match['length'])) {
                                             foreach ($match['length'] as $operation => $lengthMatch) {
                                                 $operation = preg_replace('#\s+#', '', $operation);
                                                 switch ($operation) {
                                                     case '<':
                                                         $isMatched = ($columnLength < $lengthMatch);
                                                         break;
                                                     case '>':
                                                         $isMatched = ($columnLength > $lengthMatch);
                                                         break;
                                                     case '>=':
                                                         $isMatched = ($columnLength >= $lengthMatch);
                                                         break;
                                                     case '<=':
                                                         $isMatched = ($columnLength <= $lengthMatch);
                                                         break;
                                                     case '==':
                                                         $isMatched = ($columnLength == $lengthMatch);
                                                         break;
                                                     case '=':
                                                         $isMatched = ($columnLength == $lengthMatch);
                                                         break;
                                                     default:
                                                         $isMatched = false;
                                                 }
                                             }
                                         }
                 
                                         if ($isMatched) {
                                             break;
                                         }
                                     }*/
                 if ($columnConfig && isset($configField['decorators'])) {
                     foreach ($configField['decorators'] as $decorator) {
                         $methodName = 'get' . $column->getNameAsCamelCase() . 'As' . $decorator['name'] . 'Decorator';
                         if (!isset($usedMethods[$methodName])) {
                             $file->addUse('\\Model\\Entity\\Decorator\\' . "{$decorator['name']}Decorator");
                             $docBlock->setTag(array('name' => 'method', 'description' => "{$decorator['name']}Decorator {$methodName}() {$methodName}() Декорируем данные как {$decorator['name']}"));
                             $usedMethods[$methodName] = 1;
                         }
                     }
                 }
             }
         }
         $decoratorArray = $column->getDecorator();
         foreach ($decoratorArray as $decorator) {
             $methodName = 'get' . $column->getNameAsCamelCase() . 'As' . $decorator['name'] . 'Decorator';
             if (!isset($usedMethods[$methodName])) {
                 $file->addUse('\\Model\\Entity\\Decorator\\' . "{$decorator['name']}Decorator");
                 $docBlock->setTag(array('name' => 'method', 'description' => "{$decorator['name']}Decorator {$methodName}() {$methodName}() Декорируем данные как {$decorator['name']}"));
                 $usedMethods[$methodName] = 1;
             }
         }
     }
 }
Example #2
0
    public function postRun(PartInterface $part)
    {
        /**
         * @var $part \Model\Generator\Part\Entity
         */
        /**
         * @var $file \Model\Code\Generator\FileGenerator
         */
        $file = $part->getFile();
        $columnCollection = $part->getTable()->getColumn();
        $template = '';
        /** @var $column Column */
        foreach ($columnCollection as $column) {
            $name = $column->getName();
            $requiredFlag = !($column->isNullable() || $column->getName() == 'id');
            if ($columnConfig = $part->getColumntConfig($column)) {
                if ($columnConfig && isset($columnConfig['validators'])) {
                    foreach ($columnConfig['validators'] as $validator) {
                        if (isset($validator['params'])) {
                            $validatorParams = $this->prepareValidatorParams($validator['params'], $column);
                            $validatorParams = $this->varExportMin($validatorParams, true);
                        } else {
                            $validatorParams = null;
                        }
                        if ($validatorParams && $validatorParams != 'NULL') {
                            $template .= "\$this->addValidatorRule('{$name}', Model::getValidatorAdapter()->getValidatorInstance('{$validator['name']}', {$validatorParams}), " . ($requiredFlag ? 'true' : 'false') . ");\n";
                        } else {
                            $template .= "\$this->addValidatorRule('{$name}', Model::getValidatorAdapter()->getValidatorInstance('{$validator['name']}'), " . ($requiredFlag ? 'true' : 'false') . ");\n";
                        }
                    }
                }
            }
        }
        $template = rtrim($template, "\r\n, ");
        //$tableNameAsCamelCase = $part->getTable()->getNameAsCamelCase();
        $tags = array(array('name' => 'return', 'description' => 'array Model массив с фильтрами по полям'));
        $docblock = new DocBlockGenerator('Получить правила для фильтрации ');
        $docblock->setTags($tags);
        $method = new MethodGenerator();
        $method->setName('initValidatorRules');
        $method->setVisibility(AbstractMemberGenerator::VISIBILITY_PUBLIC);
        $method->setStatic(false);
        $method->setFinal(true);
        $method->setDocBlock($docblock);
        $method->setBody(<<<EOS
{$template}
\$this->setupValidatorRules();
EOS
);
        $file->getClass()->addMethodFromGenerator($method);
    }
Example #3
0
    public function postRun(PartInterface $part)
    {
        /**
         * @var $part \Model\Generator\Part\Entity
         */
        /**
         * @var $file \Model\Code\Generator\FileGenerator
         */
        $file = $part->getFile();
        $file->addUse('Model\\Filter\\Filter');
        $columnCollection = $part->getTable()->getColumn();
        $template = '';
        /** @var $column Column */
        foreach ($columnCollection as $column) {
            $name = $column->getName();
            if ($columnConfig = $part->getColumntConfig($column)) {
                if ($columnConfig && isset($columnConfig['filters'])) {
                    foreach ($columnConfig['filters'] as $filter) {
                        $filterParams = isset($validator['params']) ? $this->varExportMin($validator['params'], true) : null;
                        if ($filterParams && $filterParams != 'NULL') {
                            $template .= "\$this->addFilterRule('{$name}', Filter::getFilterInstance('{$filter['name']}', {$filterParams}));\n";
                        } else {
                            $template .= "\$this->addFilterRule('{$name}', Filter::getFilterInstance('{$filter['name']}'));\n";
                        }
                    }
                }
            }
            /*
                        $filterArray = $column->getFilter();
            
                        foreach ($filterArray as $filter) {
                            if (empty($filter['params'])) {
                                $template .= "\$this->addFilterRule('$name', Filter::getFilterInstance('{$filter['name']}'));\n";
                            } else {
                                $filterParams = $this->varExportMin($filter['params'], true);
                                $template .= "\$this->addFilterRule('$name', Filter::getFilterInstance('{$filter['name']}', {$filterParams}));\n";
                            }
                        }
            */
            if ($column->isNullable()) {
                $template .= "\$this->addFilterRule('{$name}', Filter::getFilterInstance('\\Model\\Filter\\Null'));\n";
            }
        }
        //$tableNameAsCamelCase = $part->getTable()->getNameAsCamelCase();
        $tags = array(array('name' => 'return', 'description' => 'array Model массив с фильтрами по полям'));
        $docblock = new DocBlockGenerator('Получить правила для фильтрации ');
        $docblock->setTags($tags);
        $method = new MethodGenerator();
        $method->setName('initFilterRules');
        $method->setVisibility(AbstractMemberGenerator::VISIBILITY_PUBLIC);
        $method->setStatic(false);
        $method->setFinal(true);
        $method->setDocBlock($docblock);
        $method->setBody(<<<EOS
{$template}
\$this->setupFilterRules();
return \$this->getFilterRules();
EOS
);
        $file->getClass()->addMethodFromGenerator($method);
    }