/** * @param string $tableName * @param array $columns * @return void */ protected function generateTableGeneratedHyperRow($tableName, $columns) { $classFqn = $this->config['classes']['row']['generated']; $classFqn = Helpers::substituteClassWildcard($classFqn, $tableName); $className = Helpers::extractClassName($classFqn); $classNamespace = Helpers::extractNamespace($classFqn); $extendsFqn = $this->config['classes']['row']['base']; $extends = Helpers::formatClassName($extendsFqn, $classNamespace); $class = new ClassType($className); $class->setExtends($extends); // Add property annotations based on columns foreach ($columns as $column => $type) { if (in_array($type, [IStructure::FIELD_DATETIME, IStructure::FIELD_TIME, IStructure::FIELD_DATE, IStructure::FIELD_UNIX_TIMESTAMP])) { $type = '\\Nette\\Utils\\DateTime'; } $class->addComment("@property-read {$type} \${$column}"); } // Generate methods.row.getter foreach ((array) $this->config['methods']['row']['getter'] as $methodTemplate) { // Generate column getters foreach ($columns as $column => $type) { if (in_array($type, [IStructure::FIELD_DATETIME, IStructure::FIELD_TIME, IStructure::FIELD_DATE, IStructure::FIELD_UNIX_TIMESTAMP])) { $type = '\\Nette\\Utils\\DateTime'; } $methodName = Helpers::substituteMethodWildcard($methodTemplate, $column); $returnType = $type; $class->addMethod($methodName)->addBody('return $this->activeRow->?;', [$column])->addComment("@return {$returnType}"); // Add property annotation if (Strings::startsWith($methodName, 'get')) { $property = Strings::firstLower(Strings::substring($methodName, 3)); if ($property != $column) { $class->addComment("@property-read {$type} \${$property}"); } } } } // Generate methods.row.ref foreach ((array) $this->config['methods']['row']['ref'] as $methodTemplate) { // Generate 'ref' methods foreach ($this->structure->getBelongsToReference($tableName) as $referencingColumn => $referencedTable) { if (is_array($this->config['tables']) && !in_array($referencedTable, $this->config['tables'])) { continue; } $result = Helpers::underscoreToCamelWithoutPrefix(Strings::replace($referencingColumn, '~_id$~'), $tableName); $methodName = Helpers::substituteMethodWildcard($methodTemplate, $result); $returnType = $this->getTableClass('row', $referencedTable, $classNamespace); $class->addMethod($methodName)->addBody('return $this->ref(?, ?);', [$referencedTable, $referencingColumn])->addComment("@return {$returnType}"); // Add property annotations if (Strings::startsWith($methodName, 'get')) { $property = Strings::firstLower(Strings::substring($methodName, 3)); $class->addComment("@property-read {$returnType} \${$property}"); } } } // Generate methods.row.related foreach ((array) $this->config['methods']['row']['related'] as $methodTemplate) { // Generate 'related' methods foreach ($this->structure->getHasManyReference($tableName) as $relatedTable => $referencingColumns) { if (is_array($this->config['tables']) && !in_array($relatedTable, $this->config['tables'])) { continue; } foreach ($referencingColumns as $referencingColumn) { // Omit longest common prefix between $relatedTable and (this) $tableName $result = Helpers::underscoreToCamelWithoutPrefix($relatedTable, $tableName); if (count($referencingColumns) > 1) { $suffix = 'As' . Helpers::underscoreToCamel(Strings::replace($referencingColumn, '~_id$~')); } else { $suffix = NULL; } $methodName = Helpers::substituteMethodWildcard($methodTemplate, $result, $suffix); $returnType = $this->getTableClass('selection', $relatedTable, $classNamespace); $class->addMethod($methodName)->addBody('return $this->related(?, ?);', [$relatedTable, $referencingColumn])->addComment("@return {$returnType}"); // Add property annotations if (Strings::startsWith($methodName, 'get')) { $property = Strings::firstLower(Strings::substring($methodName, 3)); $class->addComment("@property-read {$returnType} \${$property}"); } } } } $code = implode("\n\n", ['<?php', "/**\n * This is a generated file. DO NOT EDIT. It will be overwritten.\n */", "namespace {$classNamespace};", $class]); $dir = $this->config['dir'] . '/' . 'tables' . '/' . $tableName; $file = $dir . '/' . $className . '.php'; $this->writeIfChanged($file, $code); }