Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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