Skip to content

Commit

Permalink
Create MesaVolt\Inflector helper class
Browse files Browse the repository at this point in the history
  • Loading branch information
quentin-st committed Sep 30, 2019
0 parents commit efc1834
Show file tree
Hide file tree
Showing 11 changed files with 2,797 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# thanks to
# https://www.tomasvotruba.cz/blog/2017/06/12/how-to-require-minimal-code-coverage-for-github-pull-requests-with-coveralls/

service_name: travis-ci
coverage_clover: coverage.xml # file generated by phpunit
json_path: coverage.json # file generated by php-coveralls
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.idea
/vendor
coverage.xml
coverage
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: php
php:
- '7.1'

install: composer install
script: vendor/bin/phpunit --coverage-clover coverage.xml

after_script:
# upload coverage.xml file to Coveralls to analyze it
- travis_retry vendor/bin/php-coveralls --verbose
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 MesaVolt, SARL

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Named enum for PHP 7.1 +

[![Latest Stable Version](https://poser.pugx.org/mesavolt/inflector/v/stable)](https://packagist.org/packages/mesavolt/inflector)
[![Build Status](https://travis-ci.org/MesaVolt/inflector.svg)](https://travis-ci.org/MesaVolt/inflector)
[![Coverage Status](https://coveralls.io/repos/github/MesaVolt/inflector/badge.svg)](https://coveralls.io/github/MesaVolt/inflector)
[![License](https://poser.pugx.org/mesavolt/inflector/license)](https://packagist.org/packages/mesavolt/inflector)

## Usage

Add the package to your project :

```bash
composer require mesavolt/inflector
```

```php
// Use it with any number or countable value:
Inflector::plural('cheval', 2); // Returns 'chevaux'
Inflector::plural('cheval', 1); // Returns 'chevaux'
Inflector::plural('cheval', [$horse1, $horse2]); // Returns 'chevaux'

// Specify the plural form if you want:
Inflector::plural('cheval', 2, 'chevals'); // Returns 'chevals'
Inflector::plural('un petit cheval', 2, 'des petits chevaux'); // Returns 'des petits chevaux'
```

## Integration

### Symfony >=3 with Twig >1.26

If you use the default
[auto-configuring feature of Symfony introduced in Symfony 3.3](https://symfony.com/doc/current/service_container/3.3-di-changes.html),
you only need to register the `MesaVolt\Twig\InflectorExtension` as a service in your `services.yml` file.
Symfony will tag it properly to register it in the twig environment used by your app.

If you don't use the auto-configuring feature or if it's not available in your version,
you need to apply the tags manually when you register the extension as a service.

```yaml
# Symfony 3: app/config/services.yml
# Symfony 4: config/services.yaml
services:

# Use this if you use the default auto-configuring feature of Symfony >=3.3 DI container
MesaVolt\Twig\InflectorExtension: ~

# Use this if you **don't** use the auto-configuring feature of Symfony >=3.3 DI container
app.inflector_extension:
class: MesaVolt\Twig\InflectorExtension
tags: { name: twig.extension }
```
Then, you can use the `plural` filter provided by the extension in your templates :

```twig
{# templates/my-template.html.twig #}
<p>Il y a {{ 'utilisateur'|plural(users) }} utilisateurs dans la base de données.</p>
```


## Testing

```bash
composer dump-autoload # make sure vendor/autoload.php exists
./vendor/bin/phpunit
```
34 changes: 34 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "mesavolt/inflector",
"description": "Easy-to-use French plural generator",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "MesaVolt",
"email": "[email protected]"
}
],
"autoload" : {
"psr-4" : {
"MesaVolt\\" : "src"
}
},
"autoload-dev": {
"psr-4": {
"MesaVolt\\Tests\\": "tests/"
}
},
"config": {
"sort-packages": true
},
"require": {
"php": ">=7.1",
"ext-json": "*"
},
"require-dev": {
"php-coveralls/php-coveralls": "^2.0",
"phpunit/phpunit": "^7",
"twig/twig": "^2.4"
}
}
Loading

0 comments on commit efc1834

Please sign in to comment.