Inflector.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @package ActiveRecord
  4. */
  5. namespace ActiveRecord;
  6. /**
  7. * @package ActiveRecord
  8. */
  9. abstract class Inflector
  10. {
  11. /**
  12. * Get an instance of the {@link Inflector} class.
  13. *
  14. * @return object
  15. */
  16. public static function instance()
  17. {
  18. return new StandardInflector();
  19. }
  20. /**
  21. * Turn a string into its camelized version.
  22. *
  23. * @param string $s string to convert
  24. * @return string
  25. */
  26. public function camelize($s)
  27. {
  28. $s = preg_replace('/[_-]+/','_',trim($s));
  29. $s = str_replace(' ', '_', $s);
  30. $camelized = '';
  31. for ($i=0,$n=strlen($s); $i<$n; ++$i)
  32. {
  33. if ($s[$i] == '_' && $i+1 < $n)
  34. $camelized .= strtoupper($s[++$i]);
  35. else
  36. $camelized .= $s[$i];
  37. }
  38. $camelized = trim($camelized,' _');
  39. if (strlen($camelized) > 0)
  40. $camelized[0] = strtolower($camelized[0]);
  41. return $camelized;
  42. }
  43. /**
  44. * Determines if a string contains all uppercase characters.
  45. *
  46. * @param string $s string to check
  47. * @return bool
  48. */
  49. public static function is_upper($s)
  50. {
  51. return (strtoupper($s) === $s);
  52. }
  53. /**
  54. * Determines if a string contains all lowercase characters.
  55. *
  56. * @param string $s string to check
  57. * @return bool
  58. */
  59. public static function is_lower($s)
  60. {
  61. return (strtolower($s) === $s);
  62. }
  63. /**
  64. * Convert a camelized string to a lowercase, underscored string.
  65. *
  66. * @param string $s string to convert
  67. * @return string
  68. */
  69. public function uncamelize($s)
  70. {
  71. $normalized = '';
  72. for ($i=0,$n=strlen($s); $i<$n; ++$i)
  73. {
  74. if (ctype_alpha($s[$i]) && self::is_upper($s[$i]))
  75. $normalized .= '_' . strtolower($s[$i]);
  76. else
  77. $normalized .= $s[$i];
  78. }
  79. return trim($normalized,' _');
  80. }
  81. /**
  82. * Convert a string with space into a underscored equivalent.
  83. *
  84. * @param string $s string to convert
  85. * @return string
  86. */
  87. public function underscorify($s)
  88. {
  89. return preg_replace(array('/[_\- ]+/','/([a-z])([A-Z])/'),array('_','\\1_\\2'),trim($s));
  90. }
  91. public function keyify($class_name)
  92. {
  93. return strtolower($this->underscorify(denamespace($class_name))) . '_id';
  94. }
  95. abstract function variablize($s);
  96. }
  97. /**
  98. * @package ActiveRecord
  99. */
  100. class StandardInflector extends Inflector
  101. {
  102. public function tableize($s) { return Utils::pluralize(strtolower($this->underscorify($s))); }
  103. public function variablize($s) { return str_replace(array('-',' '),array('_','_'),strtolower(trim($s))); }
  104. }
  105. ?>