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: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106:
<?php
/**
* TipyInflector
*
* Based on the Sho Kuwamoto's library
* http://kuwamoto.org/2007/12/17/improved-pluralizing-in-php-actionscript-and-ror/
* @package tipy
*/
require_once(__DIR__.'/../vendor/Inflect/Inflect.php');
/**
* Transforms words from singular to plural, class names to table names, camelCase to snake_case, etc...
*/
class TipyInflector extends Inflect {
/**
* Transform string in camelCase to snake_case
* @deprecated Use {@link snakeCase()} instead
* @param string $str
* @return string
*/
public static function underscore($str) {
return self::snakeCase($str);
}
/**
* Transform string in camelCase to snake_case
* @param string $str
* @return string
*/
public static function snakeCase($str) {
$str = preg_replace("/([A-Z\d]+)([A-Z][a-z])/", "$1_$2", $str);
$str = preg_replace("/([a-z\d]+)([A-Z])/", "$1_$2", $str);
return strtolower($str);
}
/**
* Transform string in snake_case to TitleCase
* @param string $str
* @return string
*/
public static function titleCase($str) {
$str = str_replace("_", " ", $str);
$str = ucwords($str);
$str = str_replace(" ", "", $str);
return $str;
}
/**
* Transform string in snake_case to camelCase
* @param string $str
* @return string
*/
public static function camelCase($str) {
$str = self::titleCase($str);
return lcfirst($str);
}
/**
* Create a model class name from a plural table name.
*
* <code>
* TipyInflector::classify('blog_posts') // => BlogPost
* </code>
* @param string $str
* @return string
*/
public static function classify($str) {
$str = self::camelCase($str);
$str = Inflect::singularize($str);
return ucfirst($str);
}
/**
* Create plural table name from a model name.
*
* <code>
* TipyInflector::tableize('BlogPost') // => blog_posts
* </code>
* @param string $str
* @return string
*/
public static function tableize($str) {
$str = self::pluralize($str);
return self::snakeCase($str);
}
/**
* Create name valid to be a part of controller name from snake_case string.
* This method does not change plural/singular form of nouns.
*
* <code>
* TipyInflector::controllerize('blog_post') // => BlogPost
* TipyInflector::controllerize('blog_posts') // => BlogPosts
* </code>
* @param string $str
* @return string
*/
public static function controllerize($str) {
$str = strtolower($str);
$str = self::titleCase($str);
return $str;
}
}