StringUtil.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\PropertyAccess;
  11. /**
  12. * Creates singulars from plurals.
  13. *
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. */
  16. class StringUtil
  17. {
  18. /**
  19. * Map english plural to singular suffixes.
  20. *
  21. * @var array
  22. *
  23. * @see http://english-zone.com/spelling/plurals.html
  24. */
  25. private static $pluralMap = array(
  26. // First entry: plural suffix, reversed
  27. // Second entry: length of plural suffix
  28. // Third entry: Whether the suffix may succeed a vocal
  29. // Fourth entry: Whether the suffix may succeed a consonant
  30. // Fifth entry: singular suffix, normal
  31. // bacteria (bacterium), criteria (criterion), phenomena (phenomenon)
  32. array('a', 1, true, true, array('on', 'um')),
  33. // nebulae (nebula)
  34. array('ea', 2, true, true, 'a'),
  35. // services (service)
  36. array('secivres', 8, true, true, 'service'),
  37. // mice (mouse), lice (louse)
  38. array('eci', 3, false, true, 'ouse'),
  39. // geese (goose)
  40. array('esee', 4, false, true, 'oose'),
  41. // fungi (fungus), alumni (alumnus), syllabi (syllabus), radii (radius)
  42. array('i', 1, true, true, 'us'),
  43. // men (man), women (woman)
  44. array('nem', 3, true, true, 'man'),
  45. // children (child)
  46. array('nerdlihc', 8, true, true, 'child'),
  47. // oxen (ox)
  48. array('nexo', 4, false, false, 'ox'),
  49. // indices (index), appendices (appendix), prices (price)
  50. array('seci', 4, false, true, array('ex', 'ix', 'ice')),
  51. // selfies (selfie)
  52. array('seifles', 7, true, true, 'selfie'),
  53. // movies (movie)
  54. array('seivom', 6, true, true, 'movie'),
  55. // feet (foot)
  56. array('teef', 4, true, true, 'foot'),
  57. // geese (goose)
  58. array('eseeg', 5, true, true, 'goose'),
  59. // teeth (tooth)
  60. array('hteet', 5, true, true, 'tooth'),
  61. // news (news)
  62. array('swen', 4, true, true, 'news'),
  63. // series (series)
  64. array('seires', 6, true, true, 'series'),
  65. // babies (baby)
  66. array('sei', 3, false, true, 'y'),
  67. // accesses (access), addresses (address), kisses (kiss)
  68. array('sess', 4, true, false, 'ss'),
  69. // analyses (analysis), ellipses (ellipsis), funguses (fungus),
  70. // neuroses (neurosis), theses (thesis), emphases (emphasis),
  71. // oases (oasis), crises (crisis), houses (house), bases (base),
  72. // atlases (atlas)
  73. array('ses', 3, true, true, array('s', 'se', 'sis')),
  74. // objectives (objective), alternative (alternatives)
  75. array('sevit', 5, true, true, 'tive'),
  76. // drives (drive)
  77. array('sevird', 6, false, true, 'drive'),
  78. // lives (life), wives (wife)
  79. array('sevi', 4, false, true, 'ife'),
  80. // moves (move)
  81. array('sevom', 5, true, true, 'move'),
  82. // hooves (hoof), dwarves (dwarf), elves (elf), leaves (leaf), caves (cave), staves (staff)
  83. array('sev', 3, true, true, array('f', 've', 'ff')),
  84. // axes (axis), axes (ax), axes (axe)
  85. array('sexa', 4, false, false, array('ax', 'axe', 'axis')),
  86. // indexes (index), matrixes (matrix)
  87. array('sex', 3, true, false, 'x'),
  88. // quizzes (quiz)
  89. array('sezz', 4, true, false, 'z'),
  90. // bureaus (bureau)
  91. array('suae', 4, false, true, 'eau'),
  92. // roses (rose), garages (garage), cassettes (cassette),
  93. // waltzes (waltz), heroes (hero), bushes (bush), arches (arch),
  94. // shoes (shoe)
  95. array('se', 2, true, true, array('', 'e')),
  96. // tags (tag)
  97. array('s', 1, true, true, ''),
  98. // chateaux (chateau)
  99. array('xuae', 4, false, true, 'eau'),
  100. // people (person)
  101. array('elpoep', 6, true, true, 'person'),
  102. );
  103. /**
  104. * This class should not be instantiated.
  105. */
  106. private function __construct()
  107. {
  108. }
  109. /**
  110. * Returns the singular form of a word.
  111. *
  112. * If the method can't determine the form with certainty, an array of the
  113. * possible singulars is returned.
  114. *
  115. * @param string $plural A word in plural form
  116. *
  117. * @return string|array The singular form or an array of possible singular
  118. * forms
  119. */
  120. public static function singularify($plural)
  121. {
  122. $pluralRev = strrev($plural);
  123. $lowerPluralRev = strtolower($pluralRev);
  124. $pluralLength = strlen($lowerPluralRev);
  125. // The outer loop iterates over the entries of the plural table
  126. // The inner loop $j iterates over the characters of the plural suffix
  127. // in the plural table to compare them with the characters of the actual
  128. // given plural suffix
  129. foreach (self::$pluralMap as $map) {
  130. $suffix = $map[0];
  131. $suffixLength = $map[1];
  132. $j = 0;
  133. // Compare characters in the plural table and of the suffix of the
  134. // given plural one by one
  135. while ($suffix[$j] === $lowerPluralRev[$j]) {
  136. // Let $j point to the next character
  137. ++$j;
  138. // Successfully compared the last character
  139. // Add an entry with the singular suffix to the singular array
  140. if ($j === $suffixLength) {
  141. // Is there any character preceding the suffix in the plural string?
  142. if ($j < $pluralLength) {
  143. $nextIsVocal = false !== strpos('aeiou', $lowerPluralRev[$j]);
  144. if (!$map[2] && $nextIsVocal) {
  145. // suffix may not succeed a vocal but next char is one
  146. break;
  147. }
  148. if (!$map[3] && !$nextIsVocal) {
  149. // suffix may not succeed a consonant but next char is one
  150. break;
  151. }
  152. }
  153. $newBase = substr($plural, 0, $pluralLength - $suffixLength);
  154. $newSuffix = $map[4];
  155. // Check whether the first character in the plural suffix
  156. // is uppercased. If yes, uppercase the first character in
  157. // the singular suffix too
  158. $firstUpper = ctype_upper($pluralRev[$j - 1]);
  159. if (is_array($newSuffix)) {
  160. $singulars = array();
  161. foreach ($newSuffix as $newSuffixEntry) {
  162. $singulars[] = $newBase.($firstUpper ? ucfirst($newSuffixEntry) : $newSuffixEntry);
  163. }
  164. return $singulars;
  165. }
  166. return $newBase.($firstUpper ? ucfirst($newSuffix) : $newSuffix);
  167. }
  168. // Suffix is longer than word
  169. if ($j === $pluralLength) {
  170. break;
  171. }
  172. }
  173. }
  174. // Assume that plural and singular is identical
  175. return $plural;
  176. }
  177. }