index.rst 6.4 KB

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