Skip to content

Commit

Permalink
Merge pull request #145 from moufmouf/4.3
Browse files Browse the repository at this point in the history
Adding exceptions in naming strategy
  • Loading branch information
moufmouf committed May 3, 2017
2 parents 66bb881 + 90a2a5a commit 1c6faa3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
10 changes: 10 additions & 0 deletions doc/configuring_naming.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ $strategy->setBaseDaoPrefix('');
$strategy->setBaseDaoSuffix('BaseDao');
```

Furthermore, you can configure a set of exceptions. This can be useful if your table names are not in English or not in plural form.
Let's assume you have a table named `chevaux` ('horses' in French). The singular form is 'cheval', so you would want a 'Cheval' bean and a 'ChevalDao'. That easy with the `setExceptions` method:

```php
$strategy->setExceptions([
'chevaux' => 'Cheval'
]);
```


Implementing your own naming strategy
-------------------------------------

Expand Down
35 changes: 30 additions & 5 deletions src/Mouf/Database/TDBM/Utils/DefaultNamingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class DefaultNamingStrategy implements NamingStrategyInterface
private $daoSuffix = 'Dao';
private $baseDaoPrefix = 'Abstract';
private $baseDaoSuffix = 'Dao';
private $exceptions = [];

/**
* Sets the string prefix to any bean class name.
Expand Down Expand Up @@ -105,7 +106,7 @@ public function setBaseDaoSuffix(string $baseDaoSuffix)
*/
public function getBeanClassName(string $tableName): string
{
return $this->beanPrefix.self::toSingularCamelCase($tableName).$this->beanSuffix;
return $this->beanPrefix.$this->toSingularCamelCase($tableName).$this->beanSuffix;
}

/**
Expand All @@ -116,7 +117,7 @@ public function getBeanClassName(string $tableName): string
*/
public function getBaseBeanClassName(string $tableName): string
{
return $this->baseBeanPrefix.self::toSingularCamelCase($tableName).$this->baseBeanSuffix;
return $this->baseBeanPrefix.$this->toSingularCamelCase($tableName).$this->baseBeanSuffix;
}

/**
Expand All @@ -127,7 +128,7 @@ public function getBaseBeanClassName(string $tableName): string
*/
public function getDaoClassName(string $tableName): string
{
return $this->daoPrefix.self::toSingularCamelCase($tableName).$this->daoSuffix;
return $this->daoPrefix.$this->toSingularCamelCase($tableName).$this->daoSuffix;
}

/**
Expand All @@ -138,7 +139,7 @@ public function getDaoClassName(string $tableName): string
*/
public function getBaseDaoClassName(string $tableName): string
{
return $this->baseDaoPrefix.self::toSingularCamelCase($tableName).$this->baseDaoSuffix;
return $this->baseDaoPrefix.$this->toSingularCamelCase($tableName).$this->baseDaoSuffix;
}

/**
Expand All @@ -149,8 +150,13 @@ public function getBaseDaoClassName(string $tableName): string
*
* @return string
*/
private static function toSingularCamelCase(string $str): string
private function toSingularCamelCase(string $str): string
{
// Let's first check if this is not in the exceptions directory.
if (isset($this->exceptions[$str])) {
return $this->exceptions[$str];
}

$tokens = preg_split("/[_ ]+/", $str);
$tokens = array_map([Inflector::class, 'singularize'], $tokens);

Expand All @@ -171,4 +177,23 @@ public function getDaoFactoryClassName(): string
{
return 'DaoFactory';
}

/**
* Sets exceptions in the naming of classes.
* The key is the name of the table, the value the "base" name of beans and DAOs.
*
* This is very useful for dealing with plural to singular translations in non english table names.
*
* For instance if you are dealing with a table containing horses in French ("chevaux" that has a singular "cheval"):
*
* [
* "chevaux" => "Cheval"
* ]
*
* @param array<string,string> $exceptions
*/
public function setExceptions(array $exceptions)
{
$this->exceptions = $exceptions;
}
}
9 changes: 9 additions & 0 deletions tests/Mouf/Database/TDBM/Utils/DefaultNamingStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,13 @@ public function testGetDaoFactory()
$strategy = new DefaultNamingStrategy();
$this->assertSame('DaoFactory', $strategy->getDaoFactoryClassName());
}

public function testExceptions()
{
$strategy = new DefaultNamingStrategy();
$strategy->setExceptions([
'chevaux' => 'Cheval'
]);
$this->assertSame('ChevalDao', $strategy->getDaoClassName('chevaux'));
}
}

0 comments on commit 1c6faa3

Please sign in to comment.