public function postRun(PartInterface $part) { /** * @var $part \Model\Generator\Part\Entity */ /** * @var $file \Model\Code\Generator\FileGenerator */ $file = $part->getFile(); $tableNameAsCamelCase = $part->getTable()->getNameAsCamelCase(); $tableNameAsVar = $part->getTable()->getNameAsVar(); //$tableComment = $part->getTable()->getComment(); $tags = array(array('name' => 'param', 'description' => 'array|' . $tableNameAsCamelCase . 'Model $' . $tableNameAsVar), array('name' => 'return', 'description' => '\\Model\\Entity\\' . $tableNameAsCamelCase)); $docblock = new \Zend\Code\Generator\DocBlockGenerator('Добавить ' . $tableNameAsCamelCase); $docblock->setTags($tags); $method = new \Zend\Code\Generator\MethodGenerator(); $method->setName('add' . $tableNameAsCamelCase); $method->setParameter(new \Zend\Code\Generator\ParameterGenerator($tableNameAsVar, 'mixed')); $method->setVisibility(\Zend\Code\Generator\AbstractMemberGenerator::VISIBILITY_PUBLIC); $method->setDocBlock($docblock); $method->setBody(<<<EOS \${$tableNameAsVar}Id = null; \$result = ModelResult(); \${$tableNameAsVar} = new \${$tableNameAsVar}Entity(\${$tableNameAsVar}); \${$tableNameAsVar}Data = \${$tableNameAsVar}->toArray(); // Фильтруем входные данные \${$tableNameAsVar}Data = \$this->addFilter(\${$tableNameAsVar}Data); // Валидируем данные \$validator = \$this->addValidate(\${$tableNameAsVar}Data); EOS ); $file->getClass()->addMethodFromGenerator($method); }
public function postRun(PartInterface $part) { /** * @var $part \Model\Generator\Part\Entity */ /** * @var $file \Model\Code\Generator\FileGenerator */ $file = $part->getFile(); $tableName = $part->getTable()->getName(); $tableNameAsCamelCase = $part->getTable()->getNameAsCamelCase(); $tags = array(array('name' => 'return', 'description' => 'void')); $docblock = new \Zend\Code\Generator\DocBlockGenerator('Настраиваем текущую сущность'); $docblock->setTags($tags); $method = new \Zend\Code\Generator\MethodGenerator(); $method->setName('setupEntity'); $method->setVisibility(\Zend\Code\Generator\AbstractMemberGenerator::VISIBILITY_PUBLIC); $method->setDocBlock($docblock); $method->setBody(<<<EOS \$this->setName('{$tableName}'); \$this->setEntityName('{$tableName}'); \$this->setEntityClassName('\\Model\\Entity\\{$tableNameAsCamelCase}Entity'); \$this->setCollectionClassName('\\Model\\Collection\\{$tableNameAsCamelCase}Collection'); EOS ); $file->getClass()->addMethodFromGenerator($method); }
/** * @param 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(); $tableNameAsCamelCase = $table->getNameAsCamelCase(); $tags = array(array('name' => 'see', 'description' => 'parent::setupDefaultEntityType()')); $docblock = new \Zend\Code\Generator\DocBlockGenerator('Устанавливаем тип entity по-умолчанию'); $docblock->setTags($tags); $method = new \Zend\Code\Generator\MethodGenerator(); $method->setName('setupDefaultEntityType'); $method->setVisibility(\Zend\Code\Generator\AbstractMemberGenerator::VISIBILITY_PROTECTED); $method->setDocBlock($docblock); $method->setBody(<<<EOS \$this->defaultEntityType = '\\Model\\Entity\\{$tableNameAsCamelCase}Entity'; EOS ); $file->getClass()->addMethodFromGenerator($method); }
/** * @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(); $maxColNameLen = $table->getMaxColumnNameLength(); $columnList = $table->getColumn(); $docblock = new DocBlockGenerator('Настраиваем типы данных'); $method = new \Zend\Code\Generator\MethodGenerator(); $method->setName('setupDataTypes'); $method->setVisibility(\Zend\Code\Generator\AbstractMemberGenerator::VISIBILITY_PROTECTED); $method->setDocBlock($docblock); $dataTypesText = ''; foreach ($columnList as $column) { $columnName = str_pad('\'' . $column->getName() . '\'', $maxColNameLen + 2, ' ', STR_PAD_RIGHT); $columnType = $column->getTypeAsDataTypeConstant(); $dataTypesText .= " {$columnName} => {$columnType},\n"; } $linkList = $table->getLink(); foreach ($linkList as $link) { $foreignName = $link->getForeignTable()->getName(); $columnComment = $link->getForeignColumn()->getComment(); switch ($link->getLinkType()) { case AbstractLink::LINK_TYPE_ONE_TO_MANY: case AbstractLink::LINK_TYPE_MANY_TO_MANY: $columnName = str_pad('\'_' . $foreignName . '_collection' . '\'', $maxColNameLen + 2, ' ', STR_PAD_RIGHT); $dataTypesText .= " {$columnName} => 'Model\\\\Collection\\\\" . $link->getForeignColumn()->getTable()->getNameAsCamelCase() . "Collection',\n"; case AbstractLink::LINK_TYPE_ONE_TO_ONE: case AbstractLink::LINK_TYPE_MANY_TO_ONE: $columnName = str_pad('\'_' . $foreignName . '\'', $maxColNameLen + 2, ' ', STR_PAD_RIGHT); $dataTypesText .= " {$columnName} => 'Model\\\\Entity\\\\" . $link->getForeignColumn()->getTable()->getNameAsCamelCase() . "Entity',\n"; } } $dataTypesText = rtrim($dataTypesText, "\r\n ,"); $method->setBody(<<<EOS \$this->dataTypes = array( {$dataTypesText}); EOS ); $file->getClass()->addMethodFromGenerator($method); }
/** * @param 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(); // $tableNameAsCamelCase = $table->getNameAsCamelCase(); /** @var $columnList Column[] */ $columnList = $table->getColumn(); foreach ($columnList as $column) { if ($column->getColumnType() != 'enum' || substr($column->getName(), 0, 3) == 'is_' || count($column->getEnumValuesAsArray()) < 1) { continue; } $columnName = $column->getName(); foreach ($column->getEnumValuesAsArray() as $enumValue) { $shortDescr = 'Проверить на соответствие ' . $column->getTable()->getName() . '.' . $columnName . ' значению ' . $enumValue; $docblock = new \Zend\Code\Generator\DocBlockGenerator($shortDescr); $docblock->setTags(array(array('name' => 'return', 'description' => 'bool'))); $method = new \Zend\Code\Generator\MethodGenerator(); $method->setName('is' . $column->getNameAsCamelCase() . implode('', array_map('ucfirst', explode('_', $enumValue)))); $method->setVisibility(\Zend\Code\Generator\AbstractMemberGenerator::VISIBILITY_PUBLIC); $method->setDocBlock($docblock); if ($part->hasPlugin('ConstantList', AbstractPart::PART_MODEL)) { $modelName = $table->getNameAsCamelCase() . 'Model'; $file->addUse("\\Model\\{$modelName}"); $enumValue = $modelName . '::' . strtoupper($column->getName() . '_' . $enumValue); } else { $enumValue = "'{$enumValue}'"; } $method->setBody(<<<EOS return \$this->get('{$columnName}') == {$enumValue}; EOS ); $part->getFile()->getClass()->addMethodFromGenerator($method); } } foreach ($columnList as $column) { if ($column->getColumnType() != 'enum' || substr($column->getName(), 0, 3) != 'is_' || count($column->getEnumValuesAsArray()) != 2) { continue; } $inc = 0; foreach ($column->getEnumValuesAsArray() as $enumValue) { if (in_array($enumValue, array('y', 'n'))) { $inc++; } } if ($inc != 2) { continue; } $columnName = $column->getName(); $shortDescr = 'Проверить флаг ' . $column->getTable()->getName() . '.' . $columnName; $docblock = new \Zend\Code\Generator\DocBlockGenerator($shortDescr); $docblock->setTags(array(array('name' => 'return', 'description' => 'bool'))); $method = new \Zend\Code\Generator\MethodGenerator(); $method->setName('is' . substr($column->getNameAsCamelCase(), 2)); $method->setVisibility(\Zend\Code\Generator\AbstractMemberGenerator::VISIBILITY_PUBLIC); $method->setDocBlock($docblock); $method->setBody(<<<EOS return \$this->get('{$columnName}') == 'y'; EOS ); $part->getFile()->getClass()->addMethodFromGenerator($method); } }
/** * @param 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(); $linkList = $table->getLink(); foreach ($linkList as $link) { $foreignEntity = $link->getForeignEntity(); $columnName = $link->getForeignColumn()->getName(); $columnComment = $link->getForeignColumn()->getComment(); switch ($link->getLinkType()) { case AbstractLink::LINK_TYPE_ONE_TO_MANY: case AbstractLink::LINK_TYPE_MANY_TO_MANY: if ($columnComment) { $shortDescr = "Получить список " . mb_strtolower($columnComment, 'UTF-8') . ' (' . $table->getName() . '.' . $columnName . ')'; } else { $shortDescr = 'Получить список ' . $link->getForeignEntity(); } $docblock = new \Zend\Code\Generator\DocBlockGenerator($shortDescr); $docblock->setTags(array(array('name' => 'return', 'description' => '\\Model\\Collection\\' . $link->getForeignColumn()->getTable()->getNameAsCamelCase() . 'Collection'))); $method = new \Zend\Code\Generator\MethodGenerator(); $method->setName('get' . $link->getForeignEntityAsCamelCase() . 'Collection'); $method->setVisibility(\Zend\Code\Generator\AbstractMemberGenerator::VISIBILITY_PUBLIC); $method->setDocBlock($docblock); $method->setBody(<<<EOS return \$this->get('_{$foreignEntity}_collection'); EOS ); try { $part->getFile()->getClass()->addMethodFromGenerator($method); } catch (\Exception $e) { } case AbstractLink::LINK_TYPE_ONE_TO_ONE: case AbstractLink::LINK_TYPE_MANY_TO_ONE: if ($columnComment) { $shortDescr = "Получить связанную сущность" . mb_strtolower($columnComment, 'UTF-8') . ' (' . $table->getName() . '.' . $columnName . ')'; } else { $shortDescr = 'Получить связанную сущность ' . $link->getForeignEntity(); } $docblock = new \Zend\Code\Generator\DocBlockGenerator($shortDescr); $docblock->setTags(array(array('name' => 'return', 'description' => '\\Model\\Entity\\' . $link->getForeignColumn()->getTable()->getNameAsCamelCase() . 'Entity'))); $method = new \Zend\Code\Generator\MethodGenerator(); $method->setName('get' . $link->getForeignEntityAsCamelCase()); $method->setVisibility(\Zend\Code\Generator\AbstractMemberGenerator::VISIBILITY_PUBLIC); $method->setDocBlock($docblock); $method->setBody(<<<EOS return \$this->get('_{$foreignEntity}'); EOS ); try { $part->getFile()->getClass()->addMethodFromGenerator($method); } catch (\Exception $e) { } } } }