Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ICanBoogie/Inflector

A multilingual inflector that transforms words from singular to plural, underscore to camel case, and formats strings in various ways.
https://github.com/ICanBoogie/Inflector

camelcase inflector pluralization underscore

Last synced: about 2 months ago
JSON representation

A multilingual inflector that transforms words from singular to plural, underscore to camel case, and formats strings in various ways.

Awesome Lists containing this project

README

        

# Inflector

[![Release](https://img.shields.io/packagist/v/ICanBoogie/Inflector.svg)](https://packagist.org/packages/icanboogie/inflector)
[![Code Quality](https://img.shields.io/scrutinizer/g/ICanBoogie/Inflector/master.svg)](https://scrutinizer-ci.com/g/ICanBoogie/Inflector)
[![Code Coverage](https://img.shields.io/coveralls/ICanBoogie/Inflector/master.svg)](https://coveralls.io/r/ICanBoogie/Inflector)
[![Packagist](https://img.shields.io/packagist/dm/icanboogie/inflector.svg?maxAge=2592000)](https://packagist.org/packages/icanboogie/inflector)

A multilingual inflector that transforms words from singular to plural, underscore to camel case, and formats strings in
various ways. Inflections are localized, the default english inflections for pluralization, singularization, and
uncountable words are kept in [lib/Inflections/en.php](lib/Inflections/en.php).

Inflections are currently available for the following languages:

- English (`en`)
- French (`fr`)
- Norwegian Bokmal (`nb`)
- Portuguese (`pt`)
- Spanish (`es`)
- Turkish (`tr`)

#### Installation

```bash
composer require icanboogie/inflector
```

## Usage

These are some examples of the inflector with the `en` locale (default).

```php
pluralize('post'); // "posts"
$inflector->pluralize('child'); // "children"
$inflector->pluralize('sheep'); // "sheep"
$inflector->pluralize('words'); // "words"
$inflector->pluralize('CamelChild'); // "CamelChildren"

# singularize

$inflector->singularize('posts'); // "post"
$inflector->singularize('children'); // "child"
$inflector->singularize('sheep'); // "sheep"
$inflector->singularize('word'); // "word"
$inflector->singularize('CamelChildren'); // "CamelChild"

# camelize

$inflector->camelize('active_model', Inflector::UPCASE_FIRST_LETTER);
# or
$inflector->camelize('active_model');
// 'ActiveModel'

$inflector->camelize('active_model', Inflector::DOWNCASE_FIRST_LETTER);
// 'activeModel'

$inflector->camelize('active_model/errors');
// 'ActiveModel\Errors'

$inflector->camelize('active_model/errors', Inflector::DOWNCASE_FIRST_LETTER);
// 'activeModel\Errors'

# underscore

$inflector->underscore('ActiveModel'); // 'active_model'
$inflector->underscore('ActiveModel\Errors'); // 'active_model/errors'
$inflector->underscore('Less Active Phrase'); // 'less_active_phrase'
$inflector->underscore('Number 1 Test'); // 'number_1_test'
$inflector->underscore('Johnny5 Still Alive'); // 'johnny5_still_alive'
$inflector->underscore('Lots of Spaces'); // 'lots_of_spaces'

# humanize

$inflector->humanize('employee_salary'); // "Employee salary"
$inflector->humanize('author_id'); // "Author"

# titleize

$inflector->titleize('man from the boondocks'); // "Man From The Boondocks"
$inflector->titleize('x-men: the last stand'); // "X Men: The Last Stand"
$inflector->titleize('TheManWithoutAPast'); // "The Man Without A Past"
$inflector->titleize('raiders_of_the_lost_ark'); // "Raiders Of The Lost Ark"

# ordinal

$inflector->ordinal(1); // "st"
$inflector->ordinal(2); // "nd"
$inflector->ordinal(1002); // "nd"
$inflector->ordinal(1003); // "rd"
$inflector->ordinal(-11); // "th"
$inflector->ordinal(-1021); // "st"

# ordinalize

$inflector->ordinalize(1); // "1st"
$inflector->ordinalize(2); // "2nd"
$inflector->ordinalize(1002); // "1002nd"
$inflector->ordinalize(1003); // "1003rd"
$inflector->ordinalize(-11); // "-11th"
$inflector->ordinalize(-1021); // "-1021st"

# uncountable

$inflector->is_uncountable("advice"); // true
$inflector->is_uncountable("weather"); // true
$inflector->is_uncountable("cat"); // false
```

Helpers makes it easy to use default locale inflections.

```php