1. sfInflector.class.php
  2. /** * * @package symfony * @subpackage util * @author Fabien Potencier * @version SVN: $Id: sfInflector.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $ */
  3. class sfInflector
  4. {
  5. /**
  6. * Returns a camelized string from a lower case and underscored string by replaceing slash with
  7. * double-colon and upper-casing each letter preceded by an underscore.
  8. *
  9. * @param string $lower_case_and_underscored_word String to camelize.
  10. *
  11. * @return string Camelized string.
  12. */
  13. public static function camelize($lower_case_and_underscored_word)
  14. {
  15. $tmp = $lower_case_and_underscored_word;
  16. $tmp = sfToolkit::pregtr($tmp, array('#/(.?)#e' => "'::'.strtoupper('\\1')",
  17. '/(^|_|-)+(.)/e' => "strtoupper('\\2')"));
  18. return $tmp;
  19. }
  20. /**
  21. * Returns an underscore-syntaxed version or the CamelCased string.
  22. *
  23. * @param string $camel_cased_word String to underscore.
  24. *
  25. * @return string Underscored string.
  26. */
  27. public static function underscore($camel_cased_word)
  28. {
  29. $tmp = $camel_cased_word;
  30. $tmp = str_replace('::', '/', $tmp);
  31. $tmp = sfToolkit::pregtr($tmp, array('/([A-Z]+)([A-Z][a-z])/' => '\\1_\\2',
  32. '/([a-z\d])([A-Z])/' => '\\1_\\2'));
  33. return strtolower($tmp);
  34. }
  35. /**
  36. * Returns classname::module with classname:: stripped off.
  37. *
  38. * @param string $class_name_in_module Classname and module pair.
  39. *
  40. * @return string Module name.
  41. */
  42. public static function demodulize($class_name_in_module)
  43. {
  44. return preg_replace('/^.*::/', '', $class_name_in_module);
  45. }
  46. /**
  47. * Returns classname in underscored form, with "_id" tacked on at the end.
  48. * This is for use in dealing with foreign keys in the database.
  49. *
  50. * @param string $class_name Class name.
  51. * @param bool $separate_with_underscore Separate with underscore.
  52. *
  53. * @return strong Foreign key
  54. */
  55. public static function foreign_key($class_name, $separate_with_underscore = true)
  56. {
  57. return sfInflector::underscore(sfInflector::demodulize($class_name)).($separate_with_underscore ? "_id" : "id");
  58. }
  59. /**
  60. * Returns corresponding table name for given classname.
  61. *
  62. * @param string $class_name Name of class to get database table name for.
  63. *
  64. * @return string Name of the databse table for given class.
  65. */
  66. public static function tableize($class_name)
  67. {
  68. return sfInflector::underscore($class_name);
  69. }
  70. /**
  71. * Returns model class name for given database table.
  72. *
  73. * @param string $table_name Table name.
  74. *
  75. * @return string Classified table name.
  76. */
  77. public static function classify($table_name)
  78. {
  79. return sfInflector::camelize($table_name);
  80. }
  81. /**
  82. * Returns a human-readable string from a lower case and underscored word by replacing underscores
  83. * with a space, and by upper-casing the initial characters.
  84. *
  85. * @param string $lower_case_and_underscored_word String to make more readable.
  86. *
  87. * @return string Human-readable string.
  88. */
  89. public static function humanize($lower_case_and_underscored_word)
  90. {
  91. if (substr($lower_case_and_underscored_word, -3) === '_id')
  92. {
  93. $lower_case_and_underscored_word = substr($lower_case_and_underscored_word, 0, -3);
  94. }
  95. return ucfirst(str_replace('_', ' ', $lower_case_and_underscored_word));
  96. }
  97. }

Debug toolbar