Inflector::pluralize()   C
last analyzed

Complexity

Conditions 7
Paths 15

Size

Total Lines 49
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 49
rs 6.7272
cc 7
eloc 39
nc 15
nop 1
1
<?php
2
3
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
// +----------------------------------------------------------------------+
5
// | Akelos PHP Application Framework                                     |
6
// +----------------------------------------------------------------------+
7
// | Copyright (c) 2002-2006, Akelos Media, S.L.  http://www.akelos.com/  |
8
// | Released under the GNU Lesser General Public License                 |
9
// +----------------------------------------------------------------------+
10
// | You should have received the following files along with this library |
11
// | - COPYRIGHT (Additional copyright notice)                            |
12
// | - DISCLAIMER (Disclaimer of warranty)                                |
13
// | - README (Important information regarding this library)              |
14
// +----------------------------------------------------------------------+
15
/**
16
 * Inflector for pluralize and singularize English nouns.
17
 *
18
 * This Inflector is a port of Ruby on Rails Inflector.
19
 *
20
 * It can be really helpful for developers that want to
21
 * create frameworks based on naming conventions rather than
22
 * configurations.
23
 *
24
 * It was ported to PHP for the Akelos Framework, a
25
 * multilingual Ruby on Rails like framework for PHP that will
26
 * be launched soon.
27
 *
28
 * @author Bermi Ferrer Martinez
29
 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
30
 * @license GNU Lesser General Public License
31
 *
32
 * @since 0.1
33
 *
34
 * @version $Revision 0.1 $
35
 */
36
namespace Laztopaz\PotatoORM;
37
38
trait Inflector
39
{
40
    /**
41
     * Pluralizes English nouns.
42
     *
43
     * @static
44
     *
45
     * @param string $word English noun to pluralize
46
     *
47
     * @return string Plural noun
48
     */
49
    public static function pluralize($word)
50
    {
51
        $plural = [
52
            '/(quiz)$/i'                     => '$1zes',
53
            '/^(ox)$/i'                      => '$1en',
54
            '/([m|l])ouse$/i'                => '$1ice',
55
            '/(matr|vert|ind)ix|ex$/i'       => '$1ices',
56
            '/(x|ch|ss|sh)$/i'               => '$1es',
57
            '/([^aeiouy]|qu)y$/i'            => '$1ies',
58
            '/(hive)$/i'                     => '$1s',
59
            '/(?:([^f])fe|([lr])f)$/i'       => '$1$2ves',
60
            '/(shea|lea|loa|thie)f$/i'       => '$1ves',
61
            '/sis$/i'                        => 'ses',
62
            '/([ti])um$/i'                   => '$1a',
63
            '/(tomat|potat|ech|her|vet)o$/i' => '$1oes',
64
            '/(bu)s$/i'                      => '$1ses',
65
            '/(alias)$/i'                    => '$1es',
66
            '/(octop)us$/i'                  => '$1i',
67
            '/(ax|test)is$/i'                => '$1es',
68
            '/(us)$/i'                       => '$1es',
69
            '/s$/i'                          => 's',
70
            '/$/'                            => 's',
71
        ];
72
        $uncountable = ['equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep'];
73
        $irregular = [
74
            'person' => 'people',
75
            'man'    => 'men',
76
            'child'  => 'children',
77
            'sex'    => 'sexes',
78
            'move'   => 'moves', ];
79
        $lowercased_word = strtolower($word);
80
        foreach ($uncountable as $_uncountable) {
81
            if (substr($lowercased_word, (-1 * strlen($_uncountable))) == $_uncountable) {
82
                return $word;
83
            }
84
        }
85
        foreach ($irregular as $_plural => $_singular) {
86
            if (preg_match('/('.$_plural.')$/i', $word, $arr)) {
87
                return preg_replace('/('.$_plural.')$/i', substr($arr[0], 0, 1).substr($_singular, 1), $word);
88
            }
89
        }
90
        foreach ($plural as $rule => $replacement) {
91
            if (preg_match($rule, $word)) {
92
                return preg_replace($rule, $replacement, $word);
93
            }
94
        }
95
96
        return false;
97
    }
98
}
99