Download the PHP package neunerlei/inflection without Composer
On this page you can find all versions of the php package neunerlei/inflection. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download neunerlei/inflection
More information about neunerlei/inflection
Files in neunerlei/inflection
Package inflection
Short Description An inflector with implementation agnostic inflector adapter and fancy string modifications
License Apache-2.0
Informations about the package inflection
Inflection
The package contains yet another inflector but with a twist. The implementation of the actual inflecting (pluralize and singularize a string) is agnostic to the actual implementation you want to use. By default the inflection is done by the symfony/inflector but you can simply switch to any other inflector using adapter classes.
In addition to that the inflector comes with additional string modification capabilities like toDashed, toHuman or toGetter and more.
Installation
Install this package using composer:
Methods
The inflector can be found at .
toSingular()
Returns the singular form of a word
toPlural()
Returns the plural form of a word
toSlug()
Converts a "Given string" to "given-string" or "another.String-you wouldWant" to "another-string-you-wouldwant". But in addition to that, it will convert "Annahäuser_Römertopf.jpg" into "annahaeuser-roemertopf-jpg"
NOTE: The implementation is heavily inspired (cough mostly stolen cough) from cakephp's Inflector::slug method. Kudos to them!
toFile()
Similar to toSlug() but is able to detect file extensions, and a path segment which will both be ignored while converting the file into a sluggified version.
toArray()
Converts a "Given string" to ["given", "string"] or "another.String-you wouldWant" to ["another", "string", "you", "would", "want"].
A thought on intelligent splitting: The default splitter is rather dumb when it comes to edge cases like IP, URL, and so on, because it will split them like I, P and U, R, L but stuff like HandMeAMango on the other hand will be correctly splitted like: hand, me, a, mango. If you set the second parameter to true, those edge cases will be handled more intelligently. Problems might occur when stuff like "ThisIsFAQandMore" is given, because the camelCase is broken the result will be: "this is fa qand more".
toSpacedUpper() | toHuman()
Converts a "Given string" to "Given String" or "another.String-you wouldWant" to "Another String You Would Want".
toCamelCase()
Converts a "Given string" to "GivenString" or "another.String-you wouldWant" to "AnotherStringYouWouldWant".
toCamelBack()
Converts a "Given string" to "givenString" or "another.String-you wouldWant" to "anotherStringYouWouldWant".
toDashed()
Converts a "Given string" to "given-string" or "another.String-you wouldWant" to "another-string-you-would-want".
setInflectorAdapter()
Can be used to inject a custom inflector adapter if you don't want to use the symfony inflector
toUnderscore() | toDatabase()
Converts a "Given string" to "given_string" or "another.String-you wouldWant" to "another_string_you_would_want".
toGetter()
Converts a "Given string" to "getGivenString" or "another.String-you wouldWant" to "getAnotherStringYouWouldWant". It allows you to add a custom prefix other than "get" using the second parameter. It will also sanitize the incoming string so if "getMyProperty" is given as string you will still end up with "getMyProperty" instead of "getGetMyProperty" which would not make much sense. The sanitation will remove the following prefixes "is", "has", "get" and "set".
toSetter()
Converts a "Given string" to "setGivenString" or "another.String-you wouldWant" to "setAnotherStringYouWouldWant". Sanitizes the input string like toGetter() does and has the same options.
toProperty()
This is in general an alias of toCamelBack(); But it will also strip away has/get/is/set prefixes from the given value before the camel back version is generated.
toComparable()
This method will convert the given string by unifying it. Unify means, it makes it comparable with other strings, by removing all special characters, converting everything to lowercase, counting all words and the number of their occurrence (optional) and sorting them alphabetically. This also means, that the text will no longer make sense for humans, but is easy to use for search and comparison actions.
toUuid()
Converts any given string into a UUID like: 123e4567-e89b-12d3-a456-426655440000. This is useful if you want to create, unique id's but want to combine multiple strings with different word order.
Note that "ASDF QWER" will result in the same ID as "QWER ASDF", because the values will be sorted alphabetically before the id is created. This makes sorting via first name and last name a lot easier.
Changing the Inflector implementation
As stated above you can change the actual implementation of the inflector by providing a adapter class for the inflector you would like to use the most (pull requests welcome!).
Writing an adapter is easy, create a new class:
After writing your adapter simply register it like:
After that the toSingular() and toPlural() methods will be resolved using your own adapter implementation.
Running tests
- Clone the repository
- Install the dependencies with
- Run the tests with
Postcardware
You're free to use this package, but if it makes it to your production environment I highly appreciate you sending me a postcard from your hometown, mentioning which of our package(s) you are using.
You can find my address here.
Thank you :D