sfInflector.class.php 3.6 KB

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