Inflector.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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\Inflector;
  11. use Symfony\Component\String\Inflector\EnglishInflector;
  12. trigger_deprecation('symfony/inflector', '5.1', 'The "%s" class is deprecated, use "%s" instead.', Inflector::class, EnglishInflector::class);
  13. /**
  14. * Converts words between singular and plural forms.
  15. *
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. *
  18. * @deprecated since Symfony 5.1, use Symfony\Component\String\Inflector\EnglishInflector instead.
  19. */
  20. final class Inflector
  21. {
  22. private static $englishInflector;
  23. /**
  24. * This class should not be instantiated.
  25. */
  26. private function __construct()
  27. {
  28. }
  29. /**
  30. * Returns the singular form of a word.
  31. *
  32. * If the method can't determine the form with certainty, an array of the
  33. * possible singulars is returned.
  34. *
  35. * @param string $plural A word in plural form
  36. *
  37. * @return string|array The singular form or an array of possible singular forms
  38. */
  39. public static function singularize(string $plural)
  40. {
  41. if (1 === \count($singulars = self::getEnglishInflector()->singularize($plural))) {
  42. return $singulars[0];
  43. }
  44. return $singulars;
  45. }
  46. /**
  47. * Returns the plural form of a word.
  48. *
  49. * If the method can't determine the form with certainty, an array of the
  50. * possible plurals is returned.
  51. *
  52. * @param string $singular A word in singular form
  53. *
  54. * @return string|array The plural form or an array of possible plural forms
  55. */
  56. public static function pluralize(string $singular)
  57. {
  58. if (1 === \count($plurals = self::getEnglishInflector()->pluralize($singular))) {
  59. return $plurals[0];
  60. }
  61. return $plurals;
  62. }
  63. private static function getEnglishInflector(): EnglishInflector
  64. {
  65. if (!self::$englishInflector) {
  66. self::$englishInflector = new EnglishInflector();
  67. }
  68. return self::$englishInflector;
  69. }
  70. }