1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85:
<?php
namespace Inlm\Mappers;
use LeanMapper;
class UnderScoreMapper extends DefaultMapper
{
public function getTable($entityClass)
{
return self::toUnderScore($this->trimNamespace($entityClass));
}
public function getEntityClass($table, LeanMapper\Row $row = NULL)
{
return ($this->defaultEntityNamespace !== NULL ? $this->defaultEntityNamespace . '\\' : '')
. ucfirst(self::toCamelCase($table));
}
public function getColumn($entityClass, $field)
{
return self::toUnderScore($field);
}
public function getEntityField($table, $column)
{
return self::toCamelCase($column);
}
public function getTableByRepositoryClass($repositoryClass)
{
$matches = array();
if (preg_match('#([a-z0-9]+)repository$#i', $repositoryClass, $matches)) {
return self::toUnderScore($matches[1]);
}
throw new InvalidArgumentException('Cannot determine table name.');
}
protected static function toUnderScore($s)
{
return lcfirst(preg_replace_callback('#(?<=.)([A-Z])#', function ($m) {
return '_' . strtolower($m[1]);
}, $s));
}
protected static function toCamelCase($s)
{
return preg_replace_callback('#_(.)#', function ($m) {
return strtoupper($m[1]);
}, $s);
}
}