Advertisement
Guest User

LeanMapper - standard convetions mapper

a guest
Aug 1st, 2013
808
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.86 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Model\Mapper;
  4.  
  5. /**
  6.  * Standard mapper for conventions:
  7.  * - underdash separated names of tables and cols
  8.  * - PK and FK is in [table]_id format
  9.  * - entity repository is named [Entity]Repository
  10.  * - M:N relations are stored in [table1]_[table2] tables
  11.  *
  12.  * @author Jan Nedbal
  13.  */
  14. class StandardMapper extends \LeanMapper\DefaultMapper {
  15.  
  16.     /**
  17.      * PK format [table]_id
  18.      * @param string $table
  19.      * @return string
  20.      */
  21.     public function getPrimaryKey($table) {
  22.         return $table . "_id";
  23.     }
  24.  
  25.     /**
  26.      * @param string $sourceTable
  27.      * @param string $targetTable
  28.      * @return string
  29.      */
  30.     public function getRelationshipColumn($sourceTable, $targetTable) {
  31.         return $this->getPrimaryKey($targetTable);
  32.     }
  33.    
  34.     /**
  35.      * some_entity -> Model\Entity\SomeEntity
  36.      * @param string $table
  37.      * @param \LeanMapper\Row $row
  38.      * @return string
  39.      */
  40.     public function getEntityClass($table, \LeanMapper\Row $row = null) {
  41.         return $this->defaultEntityNamespace . '\\' . ucfirst($this->underdashToCamel($table));
  42.     }
  43.  
  44.     /**
  45.      * Model\Entity\SomeEntity -> some_entity
  46.      * @param string $entityClass
  47.      * @return string
  48.      */
  49.     public function getTable($entityClass) {
  50.         return $this->camelToUnderdash($this->trimNamespace($entityClass));
  51.     }
  52.  
  53.     /**
  54.      * someField -> some_field
  55.      * @param string $entityClass
  56.      * @param string $field
  57.      * @return string
  58.      */
  59.     public function getColumn($entityClass, $field) {
  60.         return $this->camelToUnderdash($field);
  61.     }
  62.  
  63.     /**
  64.      * some_field -> someField
  65.      * @param string $table
  66.      * @param string $column
  67.      * @return string
  68.      */
  69.     public function getEntityField($table, $column) {        
  70.         return $this->underdashToCamel($column);
  71.     }
  72.  
  73.     /**
  74.      * Model\Repository\SomeEntityRepository -> some_entity
  75.      * @param string $repositoryClass
  76.      * @return string
  77.      */
  78.     public function getTableByRepositoryClass($repositoryClass) {
  79.         $class = preg_replace('#([a-z0-9]+)Repository$#', '$1', $repositoryClass);
  80.         return $this->camelToUnderdash($this->trimNamespace($class));
  81.     }
  82.  
  83.     /**
  84.      * camelCase -> underdash_separated.
  85.      * @param  string
  86.      * @return string
  87.      */
  88.     protected function camelToUnderdash($s) {
  89.         $s = preg_replace('#(.)(?=[A-Z])#', '$1_', $s);
  90.         $s = strtolower($s);
  91.         $s = rawurlencode($s);
  92.         return $s;
  93.     }
  94.  
  95.     /**
  96.      * underdash_separated -> camelCase
  97.      * @param  string
  98.      * @return string
  99.      */
  100.     protected function underdashToCamel($s) {
  101.         $s = strtolower($s);
  102.         $s = preg_replace('#_(?=[a-z])#', ' ', $s);
  103.         $s = substr(ucwords('x' . $s), 1);
  104.         $s = str_replace(' ', '', $s);
  105.         return $s;
  106.     }
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement