index.rst 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. Introduction
  2. ============
  3. The Doctrine Inflector has static methods for inflecting text.
  4. The features include pluralization, singularization,
  5. converting between camelCase and under_score and capitalizing
  6. words.
  7. All you need to use the Inflector is the ``Doctrine\Common\Inflector\Inflector``
  8. class.
  9. Installation
  10. ============
  11. You can install the Inflector with composer:
  12. .. code-block::
  13. $ composer require doctrine/inflector
  14. Here are the available methods that you can use:
  15. Tableize
  16. ========
  17. Converts ``ModelName`` to ``model_name``:
  18. .. code-block:: php
  19. echo Inflector::tableize('ModelName'); // model_name
  20. Classify
  21. ========
  22. Converts ``model_name`` to ``ModelName``:
  23. .. code-block:: php
  24. echo Inflector::classify('model_name'); // ModelName
  25. Camelize
  26. ========
  27. This method uses `Classify`_ and then converts the first character to lowercase:
  28. .. code-block:: php
  29. echo Inflector::camelize('model_name'); // modelName
  30. ucwords
  31. =======
  32. Takes a string and capitalizes all of the words, like PHP's built-in
  33. ucwords function. This extends that behavior, however, by allowing the
  34. word delimiters to be configured, rather than only separating on
  35. whitespace.
  36. Here is an example:
  37. .. code-block:: php
  38. $string = 'top-o-the-morning to all_of_you!';
  39. echo Inflector::ucwords($string); // Top-O-The-Morning To All_of_you!
  40. echo Inflector::ucwords($string, '-_ '); // Top-O-The-Morning To All_Of_You!
  41. Pluralize
  42. =========
  43. Returns a word in plural form.
  44. .. code-block:: php
  45. echo Inflector::pluralize('browser'); // browsers
  46. Singularize
  47. ===========
  48. .. code-block:: php
  49. echo Inflector::singularize('browsers'); // browser
  50. Rules
  51. =====
  52. Customize the rules for pluralization and singularization:
  53. .. code-block:: php
  54. Inflector::rules('plural', ['/^(inflect)or$/i' => '\1ables']);
  55. Inflector::rules('plural', [
  56. 'rules' => ['/^(inflect)ors$/i' => '\1ables'],
  57. 'uninflected' => ['dontinflectme'],
  58. 'irregular' => ['red' => 'redlings']
  59. ]);
  60. The arguments for the ``rules`` method are:
  61. - ``$type`` - The type of inflection, either ``plural`` or ``singular``
  62. - ``$rules`` - An array of rules to be added.
  63. - ``$reset`` - If true, will unset default inflections for all new rules that are being defined in $rules.
  64. Reset
  65. =====
  66. Clears Inflectors inflected value caches, and resets the inflection
  67. rules to the initial values.
  68. .. code-block:: php
  69. Inflector::reset();
  70. Slugify
  71. =======
  72. You can easily use the Inflector to create a slug from a string of text
  73. by using the `tableize`_ method and replacing underscores with hyphens:
  74. .. code-block:: php
  75. public static function slugify(string $text) : string
  76. {
  77. return str_replace('_', '-', Inflector::tableize($text));
  78. }