PostfixAdmin - web based virtual user administration interface for Postfix mail servers https://postfixadmin.github.io/postfixadmin/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
5.3 KiB

  1. <?php
  2. /**
  3. * -.
  4. *
  5. * Used by Cake's naming conventions throughout the framework.
  6. *
  7. * PHP versions 4 and 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
  10. * Copyright 2005-2008, Cake Software Foundation, Inc.
  11. * 1785 E. Sahara Avenue, Suite 490-204
  12. * Las Vegas, Nevada 89104
  13. * Modified for Postfixadmin by Valkum
  14. *
  15. * Copyright 2010
  16. *
  17. * Licensed under The MIT License
  18. * Redistributions of files must retain the above copyright notice.
  19. *
  20. * @filesource
  21. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
  22. * @link http://postfixadmin.sourceforge.net/ Postfixadmin on Sourceforge
  23. * @package postfixadmin
  24. * @subpackage -
  25. * @since -
  26. * @version $Revision$
  27. * @modifiedby $LastChangedBy$
  28. * @lastmodified $Date$
  29. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  30. */
  31. class Inflector {
  32. /**
  33. * Returns given $lower_case_and_underscored_word as a CamelCased word.
  34. *
  35. * @param string $lower_case_and_underscored_word Word to camelize
  36. * @return string Camelized word. LikeThis.
  37. * @access public
  38. * @static
  39. */
  40. function camelize($lowerCaseAndUnderscoredWord) {
  41. $replace = str_replace(" ", "", ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord)));
  42. return $replace;
  43. }
  44. /**
  45. * Returns an underscore-syntaxed ($like_this_dear_reader) version of the $camel_cased_word.
  46. *
  47. * @param string $camel_cased_word Camel-cased word to be "underscorized"
  48. * @return string Underscore-syntaxed version of the $camel_cased_word
  49. * @access public
  50. * @static
  51. */
  52. function underscore($camelCasedWord) {
  53. $replace = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
  54. return $replace;
  55. }
  56. /**
  57. * Returns a human-readable string from $lower_case_and_underscored_word,
  58. * by replacing underscores with a space, and by upper-casing the initial characters.
  59. *
  60. * @param string $lower_case_and_underscored_word String to be made more readable
  61. * @return string Human-readable string
  62. * @access public
  63. * @static
  64. */
  65. function humanize($lowerCaseAndUnderscoredWord) {
  66. $replace = ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord));
  67. return $replace;
  68. }
  69. /**
  70. * Returns corresponding table name for given $class_name. ("posts" for the model class "Post").
  71. *
  72. * @param string $class_name Name of class to get database table name for
  73. * @return string Name of the database table for given class
  74. * @access public
  75. * @static
  76. */
  77. function tableize($className) {
  78. $replace = Inflector::pluralize(Inflector::underscore($className));
  79. return $replace;
  80. }
  81. /**
  82. * Returns Cake model class name ("Post" for the database table "posts".) for given database table.
  83. *
  84. * @param string $tableName Name of database table to get class name for
  85. * @return string Class name
  86. * @access public
  87. * @static
  88. */
  89. function classify($tableName) {
  90. $replace = Inflector::camelize(Inflector::singularize($tableName));
  91. return $replace;
  92. }
  93. /**
  94. * Returns camelBacked version of a string.
  95. *
  96. * @param string $string
  97. * @return string in variable form
  98. * @access public
  99. * @static
  100. */
  101. function variable($string) {
  102. $string = Inflector::camelize(Inflector::underscore($string));
  103. $replace = strtolower(substr($string, 0, 1));
  104. $variable = preg_replace('/\\w/', $replace, $string, 1);
  105. return $variable;
  106. }
  107. /**
  108. * Returns a string with all spaces converted to $replacement and non word characters removed.
  109. *
  110. * @param string $string
  111. * @param string $replacement
  112. * @return string
  113. * @access public
  114. * @static
  115. */
  116. function slug($string, $replacement = '_') {
  117. if (!class_exists('String')) {
  118. require LIBS . 'string.php';
  119. }
  120. $map = array(
  121. '/à|á|å|â/' => 'a',
  122. '/è|é|ê|ẽ|ë/' => 'e',
  123. '/ì|í|î/' => 'i',
  124. '/ò|ó|ô|ø/' => 'o',
  125. '/ù|ú|ů|û/' => 'u',
  126. '/ç/' => 'c',
  127. '/ñ/' => 'n',
  128. '/ä|æ/' => 'ae',
  129. '/ö/' => 'oe',
  130. '/ü/' => 'ue',
  131. '/Ä/' => 'Ae',
  132. '/Ü/' => 'Ue',
  133. '/Ö/' => 'Oe',
  134. '/ß/' => 'ss',
  135. '/[^\w\s]/' => ' ',
  136. '/\\s+/' => $replacement,
  137. String::insert('/^[:replacement]+|[:replacement]+$/', array('replacement' => preg_quote($replacement, '/'))) => '',
  138. );
  139. $string = preg_replace(array_keys($map), array_values($map), $string);
  140. return $string;
  141. }
  142. }
  143. /**
  144. * Enclose a string for preg matching.
  145. *
  146. * @param string $string String to enclose
  147. * @return string Enclosed string
  148. */
  149. function __enclose($string) {
  150. return '(?:' . $string . ')';
  151. }