├── tests └── PHPInflectTest.php ├── .gitignore ├── CONTRIBUTING.md ├── composer.json ├── .editorconfig ├── LICENSE ├── src └── Inflect │ ├── Facade.php │ └── Inflect.php └── README.md /tests/PHPInflectTest.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /bootstrap/compiled.php 2 | .env.*.php 3 | .env.php 4 | vendor/ 5 | composer.lock 6 | composer.phar 7 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | 1. Fork it! 4 | 2. Create your feature branch: `git checkout -b my-new-feature` 5 | 3. Commit your changes: `git commit -am 'Add some feature'` 6 | 4. Push to the branch: `git push origin my-new-feature` 7 | 5. Submit a pull request :D 8 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "randsonjs/inflect", 3 | "description": "A PHP class to handle words in Portuguese", 4 | "type": "package", 5 | "require-dev": { 6 | "phpunit/phpunit": "~4.3" 7 | }, 8 | "license": "MIT License", 9 | "homepage": "https://github.com/rands0n/inflect", 10 | "authors": [{ 11 | "name": "Randson Oliveira", 12 | "email": "me@rands0n.com" 13 | }], 14 | "require": {}, 15 | "autoload": { 16 | "psr-0": { 17 | "Inflect": "src/" 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | ; EditorConfig is awesome: http://EditorConfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | charset = utf-8 7 | end_of_line = lf 8 | indent_size = 4 9 | indent_style = space 10 | insert_final_newline = false 11 | trim_trailing_whitespace = true 12 | 13 | ; The default indent on package.json is 2 spaces, better to keep it so we can 14 | ; use `npm install --save` and other features that rewrites the package.json 15 | ; file automatically 16 | [package.json] 17 | indent_style = space 18 | indent_size = 2 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Randson Oliveira (randsonjs.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /src/Inflect/Facade.php: -------------------------------------------------------------------------------- 1 | $method(); 48 | 49 | case 1: 50 | return $instance->$method($args[0]); 51 | 52 | default: 53 | return call_user_func_array(array($instance, $method), $args); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Inflect 2 | 3 | [![MIT License](http://img.shields.io/badge/license-MIT-red.svg)](./LICENSE) 4 | [![Composer Version](http://img.shields.io/badge/composer-v1.0--dev-orange.svg)](http://getcomposer.org) 5 | 6 | A PHP class to handle words in Portuguese 7 | 8 | ## Dependencies 9 | 10 | 1. Install [PHP](http://php.net/downloads) if you don't have it yet. 11 | 2. Install [Composer](http://getcomposer.org) if you don't have it yet. 12 | 13 | ## Usage 14 | 15 | Examples of usage of the Inflect(i.e portuguese). 16 | 17 | ```php 18 | pluralize('pão'); // pães 25 | $inflector->pluralize('carro'); // carros 26 | 27 | $inflector->singularize('carros'); // carro 28 | $inflector->singularize('pães'); // pão 29 | 30 | $inflector->camelize('tablename'); // TableName 31 | $inflector->camelize('tablename', true); // tableName 32 | ``` 33 | 34 | You can also use it without instantiating the object trought the `Facade` class 35 | 36 | ```php 37 | Check [editorconfig.org](http://editorconfig.org/) if you haven't heard about this project. 90 | 91 | #### [.gitignore](.gitignore) 92 | 93 | List of files that we don't want Git to track. 94 | > Check this [Git Ignoring Files Guide](https://help.github.com/articles/ignoring-files) for more details. 95 | 96 | #### [composer.json](composer.json) 97 | 98 | Specify all dependencies loaded via Composer. 99 | > Check [composer.json](https://getcomposer.org/doc/01-basic-usage.md#composer-json-project-setup) Project Setup for more details. 100 | 101 | ## Contributing 102 | 103 | 1. Fork it! 104 | 2. Create your feature branch: `git checkout -b my-new-feature` 105 | 3. Commit your changes: `git commit -am 'Add some feature'` 106 | 4. Push to the branch: `git push origin my-new-feature` 107 | 5. Submit a pull request :D 108 | 109 | ## License 110 | [MIT License](./LICENSE) © Randson Oliveira 111 | -------------------------------------------------------------------------------- /src/Inflect/Inflect.php: -------------------------------------------------------------------------------- 1 | '$1', 20 | '/^(paí)s$/i' => '$1ses', 21 | '/(z|r)$/i' => '$1es', 22 | '/al$/i' => 'ais', 23 | '/el$/i' => 'eis', 24 | '/ol$/i' => 'ois', 25 | '/ul$/i' => 'uis', 26 | '/([^aeou])il$/i' => '$1is', 27 | '/m$/i' => 'ns', 28 | '/^(japon|escoc|ingl|dinamarqu|fregu|portugu)ês$/i' => '$1eses', 29 | '/^(|g)ás$/i' => '$1ases', 30 | '/ão$/i' => 'ões', 31 | '/^(irm|m)ão$/i' => '$1ãos', 32 | '/^(alem|c|p)ão$/i' => '$1ães', 33 | '/ao$/i' => 'oes', 34 | '/^(irm|m)ao$/i' => '\1aos', 35 | '/^(alem|c|p)ao$/i' => '\1aes', 36 | '/$/' => 's', 37 | ); 38 | 39 | /** 40 | * Declare singular words in lowercase 41 | * 42 | * @var string 43 | **/ 44 | static $singular = array( 45 | '/^(á|gá|paí)s$/i' => '$1s', 46 | '/(r|z)es$/i' => '$1', 47 | '/([^p])ais$/i' => '$1al', 48 | '/eis$/i' => 'el', 49 | '/ois$/i' => 'ol', 50 | '/uis$/i' => 'ul', 51 | '/(r|t|f|v)is$/i' => '$1il', 52 | '/ns$/i' => 'm', 53 | '/sses$/i' => 'sse', 54 | '/^(.*[^s]s)es$/i' => '$1', 55 | '/ães$/i' => 'ão', 56 | '/aes$/i' => 'ao', 57 | '/ãos$/i' => 'ão', 58 | '/aos$/i' => 'ao', 59 | '/ões$/i' => 'ão', 60 | '/oes$/i' => 'ao', 61 | '/(japon|escoc|ingl|dinamarqu|fregu|portugu)eses$/i' => '$1ês', 62 | '/^(g|)ases$/i' => '$1ás', 63 | '/([^ê])s$/i' => '$1' 64 | ); 65 | 66 | /** 67 | * Declare irregular word 68 | * 69 | * @var string 70 | **/ 71 | static $irregular = array( 72 | 'país' => 'países' 73 | ); 74 | 75 | /** 76 | * Declare uncountable words 77 | * 78 | * @var string 79 | **/ 80 | static $uncountable = array( 81 | 'tórax', 82 | 'tênis', 83 | 'ônibus', 84 | 'lápis', 85 | 'fênix' 86 | ); 87 | 88 | /** 89 | * Pluralize word 90 | * 91 | * @param string 92 | * @return string 93 | **/ 94 | public function pluralize($string) 95 | { 96 | if (in_array(strtolower($string), self::$uncountable)) { 97 | return $string; 98 | } 99 | 100 | foreach (self::$irregular as $pattern => $result) { 101 | $pattern = '/' . $pattern . '$/i'; 102 | 103 | if (preg_match($pattern, $string)) { 104 | return preg_replace($pattern, $result, $string); 105 | } 106 | } 107 | 108 | foreach (self::$plural as $pattern => $result) { 109 | if (preg_match($pattern, $result)) { 110 | return preg_replace($pattern, $result, $string); 111 | } 112 | } 113 | 114 | return $string; 115 | } 116 | 117 | /** 118 | * Singularize word 119 | * 120 | * @param string 121 | * @return string 122 | **/ 123 | public function singularize($string) 124 | { 125 | if (in_array(strtolower($string), self::$uncountable)) { 126 | return $string; 127 | } 128 | 129 | foreach (self::$irregular as $result => $pattern) { 130 | $pattern = '/' . $pattern . '$/i'; 131 | 132 | if (preg_match($pattern, $string)) { 133 | return preg_replace($pattern, $result, $string); 134 | } 135 | } 136 | 137 | foreach (self::$singular as $pattern => $result) { 138 | if (preg_match($pattern, $string)) { 139 | return preg_replace($pattern, $result, $string); 140 | } 141 | } 142 | 143 | return $string; 144 | } 145 | 146 | /** 147 | * Camelize word 148 | * 149 | * @param string 150 | * @return string 151 | **/ 152 | public function camelize($string, $uppercase_first_letter = false) 153 | { 154 | if ($uppercase_first_letter) { 155 | return str_replace(' ', '', ucwords(strtr($string, '_-', ' '))); 156 | } 157 | 158 | return str_replace(' ', '', lcfirst(strtr($string, '_-', ' '))); 159 | } 160 | 161 | } 162 | --------------------------------------------------------------------------------