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.

226 lines
6.4 KiB

3 years ago
  1. Introduction
  2. ============
  3. The Doctrine Inflector has methods for inflecting text. The features include pluralization,
  4. singularization, converting between camelCase and under_score and capitalizing
  5. words.
  6. Installation
  7. ============
  8. You can install the Inflector with composer:
  9. .. code-block:: console
  10. $ composer require doctrine/inflector
  11. Usage
  12. =====
  13. Using the inflector is easy, you can create a new ``Doctrine\Inflector\Inflector`` instance by using
  14. the ``Doctrine\Inflector\InflectorFactory`` class:
  15. .. code-block:: php
  16. use Doctrine\Inflector\InflectorFactory;
  17. $inflector = InflectorFactory::create()->build();
  18. By default it will create an English inflector. If you want to use another language, just pass the language
  19. you want to create an inflector for to the ``createForLanguage()`` method:
  20. .. code-block:: php
  21. use Doctrine\Inflector\InflectorFactory;
  22. use Doctrine\Inflector\Language;
  23. $inflector = InflectorFactory::createForLanguage(Language::SPANISH)->build();
  24. The supported languages are as follows:
  25. - ``Language::ENGLISH``
  26. - ``Language::FRENCH``
  27. - ``Language::NORWEGIAN_BOKMAL``
  28. - ``Language::PORTUGUESE``
  29. - ``Language::SPANISH``
  30. - ``Language::TURKISH``
  31. If you want to manually construct the inflector instead of using a factory, you can do so like this:
  32. .. code-block:: php
  33. use Doctrine\Inflector\CachedWordInflector;
  34. use Doctrine\Inflector\RulesetInflector;
  35. use Doctrine\Inflector\Rules\English;
  36. $inflector = new Inflector(
  37. new CachedWordInflector(new RulesetInflector(
  38. English\Rules::getSingularRuleset()
  39. )),
  40. new CachedWordInflector(new RulesetInflector(
  41. English\Rules::getPluralRuleset()
  42. ))
  43. );
  44. Adding Languages
  45. ----------------
  46. If you are interested in adding support for your language, take a look at the other languages defined in the
  47. ``Doctrine\Inflector\Rules`` namespace and the tests located in ``Doctrine\Tests\Inflector\Rules``. You can copy
  48. one of the languages and update the rules for your language.
  49. Once you have done this, send a pull request to the ``doctrine/inflector`` repository with the additions.
  50. Custom Setup
  51. ============
  52. If you want to setup custom singular and plural rules, you can configure these in the factory:
  53. .. code-block:: php
  54. use Doctrine\Inflector\InflectorFactory;
  55. use Doctrine\Inflector\Rules\Pattern;
  56. use Doctrine\Inflector\Rules\Patterns;
  57. use Doctrine\Inflector\Rules\Ruleset;
  58. use Doctrine\Inflector\Rules\Substitution;
  59. use Doctrine\Inflector\Rules\Substitutions;
  60. use Doctrine\Inflector\Rules\Transformation;
  61. use Doctrine\Inflector\Rules\Transformations;
  62. use Doctrine\Inflector\Rules\Word;
  63. $inflector = InflectorFactory::create()
  64. ->withSingularRules(
  65. new Ruleset(
  66. new Transformations(
  67. new Transformation(new Pattern('/^(bil)er$/i'), '\1'),
  68. new Transformation(new Pattern('/^(inflec|contribu)tors$/i'), '\1ta')
  69. ),
  70. new Patterns(new Pattern('singulars')),
  71. new Substitutions(new Substitution(new Word('spins'), new Word('spinor')))
  72. )
  73. )
  74. ->withPluralRules(
  75. new Ruleset(
  76. new Transformations(
  77. new Transformation(new Pattern('^(bil)er$'), '\1'),
  78. new Transformation(new Pattern('^(inflec|contribu)tors$'), '\1ta')
  79. ),
  80. new Patterns(new Pattern('noflect'), new Pattern('abtuse')),
  81. new Substitutions(
  82. new Substitution(new Word('amaze'), new Word('amazable')),
  83. new Substitution(new Word('phone'), new Word('phonezes'))
  84. )
  85. )
  86. )
  87. ->build();
  88. No operation inflector
  89. ----------------------
  90. The ``Doctrine\Inflector\NoopWordInflector`` may be used to configure an inflector that doesn't perform any operation for
  91. pluralization and/or singularization. If will simply return the input as output.
  92. This is an implementation of the `Null Object design pattern <https://sourcemaking.com/design_patterns/null_object>`_.
  93. .. code-block:: php
  94. use Doctrine\Inflector\Inflector;
  95. use Doctrine\Inflector\NoopWordInflector;
  96. $inflector = new Inflector(new NoopWordInflector(), new NoopWordInflector());
  97. Tableize
  98. ========
  99. Converts ``ModelName`` to ``model_name``:
  100. .. code-block:: php
  101. echo $inflector->tableize('ModelName'); // model_name
  102. Classify
  103. ========
  104. Converts ``model_name`` to ``ModelName``:
  105. .. code-block:: php
  106. echo $inflector->classify('model_name'); // ModelName
  107. Camelize
  108. ========
  109. This method uses `Classify`_ and then converts the first character to lowercase:
  110. .. code-block:: php
  111. echo $inflector->camelize('model_name'); // modelName
  112. Capitalize
  113. ==========
  114. Takes a string and capitalizes all of the words, like PHP's built-in
  115. ``ucwords`` function. This extends that behavior, however, by allowing the
  116. word delimiters to be configured, rather than only separating on
  117. whitespace.
  118. Here is an example:
  119. .. code-block:: php
  120. $string = 'top-o-the-morning to all_of_you!';
  121. echo $inflector->capitalize($string); // Top-O-The-Morning To All_of_you!
  122. echo $inflector->capitalize($string, '-_ '); // Top-O-The-Morning To All_Of_You!
  123. Pluralize
  124. =========
  125. Returns a word in plural form.
  126. .. code-block:: php
  127. echo $inflector->pluralize('browser'); // browsers
  128. Singularize
  129. ===========
  130. Returns a word in singular form.
  131. .. code-block:: php
  132. echo $inflector->singularize('browsers'); // browser
  133. Urlize
  134. ======
  135. Generate a URL friendly string from a string of text:
  136. .. code-block:: php
  137. echo $inflector->urlize('My first blog post'); // my-first-blog-post
  138. Unaccent
  139. ========
  140. You can unaccent a string of text using the ``unaccent()`` method:
  141. .. code-block:: php
  142. echo $inflector->unaccent('año'); // ano
  143. Legacy API
  144. ==========
  145. The API present in Inflector 1.x is still available, but will be deprecated in a future release and dropped for 3.0.
  146. Support for languages other than English is available in the 2.0 API only.
  147. Acknowledgements
  148. ================
  149. The language rules in this library have been adapted from several different sources, including but not limited to:
  150. - `Ruby On Rails Inflector <http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html>`_
  151. - `ICanBoogie Inflector <https://github.com/ICanBoogie/Inflector>`_
  152. - `CakePHP Inflector <https://book.cakephp.org/3.0/en/core-libraries/inflector.html>`_