Completed
Push — master ( 245fbf...00e192 )
by Adeniyi
04:40 queued 02:23
created

src/Helper/Inflector.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5
// +----------------------------------------------------------------------+
6
// | Akelos PHP Application Framework                                     |
7
// +----------------------------------------------------------------------+
8
// | Copyright (c) 2002-2006, Akelos Media, S.L.  http://www.akelos.com/  |
9
// | Released under the GNU Lesser General Public License                 |
10
// +----------------------------------------------------------------------+
11
// | You should have received the following files along with this library |
12
// | - COPYRIGHT (Additional copyright notice)                            |
13
// | - DISCLAIMER (Disclaimer of warranty)                                |
14
// | - README (Important information regarding this library)              |
15
// +----------------------------------------------------------------------+
16
17
/**
18
* Inflector for pluralize and singularize English nouns.
19
*
20
* This Inflector is a port of Ruby on Rails Inflector.
21
*
22
* It can be really helpful for developers that want to
23
* create frameworks based on naming conventions rather than
24
* configurations.
25
*
26
* It was ported to PHP for the Akelos Framework, a
27
* multilingual Ruby on Rails like framework for PHP that will
28
* be launched soon.
29
*
30
* @author Bermi Ferrer Martinez
31
* @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
32
* @license GNU Lesser General Public License
33
* @since 0.1
34
* @version $Revision 0.1 $
35
*/
36
37
namespace Ibonly\PotatoORM;
38
39
trait Inflector
40
{
41
    /**
42
    * Pluralizes English nouns.
43
    *
44
    * @access public
45
    * @static
46
    * @param    string    $word    English noun to pluralize
47
    * @return string Plural noun
48
    */
49
    public static function pluralize($word)
50
    {
51
        $plural = array(
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
73
        $uncountable = array('equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep');
74
75
        $irregular = array(
76
        'person' => 'people',
77
        'man' => 'men',
78
        'child' => 'children',
79
        'sex' => 'sexes',
80
        'move' => 'moves');
81
82
        $lowercased_word = strtolower($word);
83
84 View Code Duplication
        foreach ($uncountable as $_uncountable){
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
            if(substr($lowercased_word,(-1*strlen($_uncountable))) == $_uncountable){
86
                return $word;
87
            }
88
        }
89
90 View Code Duplication
        foreach ($irregular as $_plural=> $_singular){
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
            if (preg_match('/('.$_plural.')$/i', $word, $arr)) {
92
                return preg_replace('/('.$_plural.')$/i', substr($arr[0],0,1).substr($_singular,1), $word);
93
            }
94
        }
95
96
        foreach ($plural as $rule => $replacement) {
97
            if (preg_match($rule, $word)) {
98
                return preg_replace($rule, $replacement, $word);
99
            }
100
        }
101
        return false;
102
    }
103
104
    /**
105
    * Singularizes English nouns.
106
    *
107
    * @access public
108
    * @static
109
    * @param    string    $word    English noun to singularize
110
    * @return string Singular noun.
111
    */
112
    public function singularize($word)
113
    {
114
        $singular = array (
115
            '/(quiz)zes$/i'             => "$1",
116
            '/(matr)ices$/i'            => "$1ix",
117
            '/(vert|ind)ices$/i'        => "$1ex",
118
            '/^(ox)en$/i'               => "$1",
119
            '/(alias)es$/i'             => "$1",
120
            '/(octop|vir)i$/i'          => "$1us",
121
            '/(cris|ax|test)es$/i'      => "$1is",
122
            '/(shoe)s$/i'               => "$1",
123
            '/(o)es$/i'                 => "$1",
124
            '/(bus)es$/i'               => "$1",
125
            '/([m|l])ice$/i'            => "$1ouse",
126
            '/(x|ch|ss|sh)es$/i'        => "$1",
127
            '/(m)ovies$/i'              => "$1ovie",
128
            '/(s)eries$/i'              => "$1eries",
129
            '/([^aeiouy]|qu)ies$/i'     => "$1y",
130
            '/([lr])ves$/i'             => "$1f",
131
            '/(tive)s$/i'               => "$1",
132
            '/(hive)s$/i'               => "$1",
133
            '/(li|wi|kni)ves$/i'        => "$1fe",
134
            '/(shea|loa|lea|thie)ves$/i'=> "$1f",
135
            '/(^analy)ses$/i'           => "$1sis",
136
            '/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i'  => "$1$2sis",
137
            '/([ti])a$/i'               => "$1um",
138
            '/(n)ews$/i'                => "$1ews",
139
            '/(h|bl)ouses$/i'           => "$1ouse",
140
            '/(corpse)s$/i'             => "$1",
141
            '/(us)es$/i'                => "$1",
142
            '/s$/i'                     => ""
143
        );
144
145
        $uncountable = array('equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep');
146
147
        $irregular = array(
148
        'person' => 'people',
149
        'man' => 'men',
150
        'child' => 'children',
151
        'sex' => 'sexes',
152
        'move' => 'moves');
153
154
        $lowercased_word = strtolower($word);
155 View Code Duplication
        foreach ($uncountable as $_uncountable){
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
            if(substr($lowercased_word,(-1*strlen($_uncountable))) == $_uncountable){
157
                return $word;
158
            }
159
        }
160
161 View Code Duplication
        foreach ($irregular as $_plural=> $_singular){
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
162
            if (preg_match('/('.$_singular.')$/i', $word, $arr)) {
163
                return preg_replace('/('.$_singular.')$/i', substr($arr[0],0,1).substr($_plural,1), $word);
164
            }
165
        }
166
167
        foreach ($singular as $rule => $replacement) {
168
            if (preg_match($rule, $word)) {
169
                return preg_replace($rule, $replacement, $word);
170
            }
171
        }
172
173
        return $word;
174
    }
175
}