Skip to content

Commit

Permalink
Fixes #366: Better class and file names for uppercase tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidney Lins authored and samdark committed Jun 1, 2018
1 parent c544316 commit ab8ba3b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Yii Framework 2 gii extension Change Log
2.0.8 under development
-----------------------

- no changes in this release.
- Enh #366: Better class and file names for uppercase tables (slinstj)


2.0.7 May 3, 2018
Expand Down
4 changes: 3 additions & 1 deletion src/generators/model/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,9 @@ protected function generateClassName($tableName, $useSchemaName = null)
}
}

return $this->classNames[$fullTableName] = Inflector::id2camel($schemaName.$className, '_');
$schemaName = ctype_upper(strtr($schemaName, ['_' => '', '-' => ''])) ? strtolower($schemaName) : $schemaName;
$className = ctype_upper(strtr($className, ['_' => '', '-' => ''])) ? strtolower($className) : $className;
return $this->classNames[$fullTableName] = strtr(ucwords(implode(' ', explode('_', strtolower(strtr(Inflector::camel2words($schemaName.$className), [' ' => '_']))))), [' ' => '']);
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/generators/ModelGeneratorMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace yiiunit\gii\generators;

use yii\gii\generators\model\Generator;

/**
* Just a mock for testing porpouses.
*
* @author Sidney Lins slinstj@gmail.com
*/
class ModelGeneratorMock extends Generator
{
public function publicGenerateClassName($tableName, $useSchemaName = null)
{
return $this->generateClassName($tableName, $useSchemaName);
}
}
23 changes: 23 additions & 0 deletions tests/generators/ModelGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,27 @@ public function testRules($tableName, $fileName, $rules)
);
}
}

public function testGenerateClassName()
{
$modelGenerator = new ModelGeneratorMock;

$tableNames = [
'lower_underline_name' => 'LowerUnderlineName',
'Ucwords_Underline_Name' => 'UcwordsUnderlineName',
'UPPER_UNDERLINE_NAME' => 'UpperUnderlineName',
'lower-hyphen-name' => 'LowerHyphenName',
'Ucwords-Hyphen-Name' => 'UcwordsHyphenName',
'UPPER-HYPHEN-NAME' => 'UpperHyphenName',
'CamelCaseName' => 'CamelCaseName',
'lowerUcwordsName' => 'LowerUcwordsName',
'lowername' => 'Lowername',
'UPPERNAME' => 'Uppername',
];

foreach ($tableNames as $tableName => $expectedClassName) {
$generatedClassName = $modelGenerator->publicGenerateClassName($tableName);
$this->assertEquals($generatedClassName, $expectedClassName);
}
}
}

0 comments on commit ab8ba3b

Please sign in to comment.