Completed
Push — dev ( 969154...6ffd37 )
by James Ekow Abaka
01:43
created

Text   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 96.67%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 177
ccs 29
cts 30
cp 0.9667
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A camelize() 0 19 2
A ucamelize() 0 4 1
A deCamelize() 0 11 1
A runInflection() 0 11 4
A pluralize() 0 4 1
A singularize() 0 4 1
1
<?php
2
/*
3
 * Ntentan Framework
4
 * Copyright (c) 2008-2015 James Ekow Abaka Ainooson
5
 * 
6
 * Permission is hereby granted, free of charge, to any person obtaining
7
 * a copy of this software and associated documentation files (the
8
 * "Software"), to deal in the Software without restriction, including
9
 * without limitation the rights to use, copy, modify, merge, publish,
10
 * distribute, sublicense, and/or sell copies of the Software, and to
11
 * permit persons to whom the Software is furnished to do so, subject to
12
 * the following conditions:
13
 * 
14
 * The above copyright notice and this permission notice shall be
15
 * included in all copies or substantial portions of the Software.
16
 * 
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
24
 * 
25
 */
26
27
namespace ntentan\utils;
28
29
/**
30
 * A couple of utility functions for manipulating strings.
31
 */
32
class Text
33
{
34
    private static $pluralRules = [
35
        ['/child/', 'ren'],
36
        ['/^ox$/', 'en'],
37
        ['/(.*)(a|e|i|o|u)(?<remove>y)$/', 'ys'],
38
        ['/(.*)(?<remove>y)$/', 'ies'],
39
        ['/(foc|alumn|fung|nucle|octop|radi|syllab)(?<remove>us)$/', 'i'],
40
        ['/(.*)(d|r)(?<remove>ex|ix)$/', 'ices'],
41
        ['/(.*)(s|x)(?<remove>is)$/', 'es'],
42
        ['/(.*)(?<remove>sh)$/', 'shes'],
43
        ['/(.*)(?<remove>eau)$/', 'eaux'],
44
        ['/(.*)(?<remove>um)$/', 'a'],
45
        ['/(.*)(?<remove>tooth)$/', 'teeth'],
46
        ['/(.*)(?<remove>h)$/', 'hes'],
47
        ['/(formul|alumn|nebul)(?<remove>a)$/', 'ae'],
48
        ['/(.*)(?<remove>x)$/', 'xes'],
49
        ['/(.+)(?<remove>ion)$/', 'ia'],
50
        ['/(.*)(?<remove>roof)$/', 'roofs'],
51
        ['/(.*)[^f](?<remove>f|fe)$/', 'ves'],
52
        ['/(.*)(m|l)(?<remove>ouse)$/', 'ice'],
53
        ['/(.*)(?<remove>man)$/', 'men'],
54
        ['/(.*)(?<remove>foot)$/', 'feet'],
55
        ['/(.*)(disc|phot|pian)(?<remove>o)$/', 'os'],
56
        ['/(.*)(?<remove>goose)$/', 'geese'],
57
        ['/(.*)(?<remove>person)$/', 'people'],
58
        ['/(.*)(?<remove>quiz)$/', 'quizzes'],
59
        ['/.*(s|o|z)$/', 'es'],
60
        ['/.*/', 's']
61
    ];
62
63
    private static $singularRules = [
64
        ['/^axe(?<remove>s)$/', ''],
65
        ['/(.*)(?<remove>a)$/', 'um'],
66
        ['/(.*)(dev|v|pr)(?<remove>ices)$/', 'ice'],
67
        ['/(.*)(?<remove>ices)$/', 'ix'],
68
        ['/(.*)(?<remove>movies)$/', 'movie'],
69
        ['/(.*)(?<remove>ies)$/', 'y'],
70
        ['/(.*)(?<remove>shoes)$/', 'shoe'],
71
        ['/(.*)(?<remove>oes)$/', 'o'],
72
        ['/(.*)(?<remove>bases)$/', 'base'],
73
        ['/(.*)(?<remove>cheeses)$/', 'cheese'],
74
        ['/(.*)(?<remove>children)$/', 'child'],
75
        ['/(.*)(?<remove>men)$/', 'man'],
76
        ['/(.*)(?<remove>feet)$/', 'foot'],
77
        ['/(.*)(?<remove>geese)$/', 'goose'],
78
        ['/(.*)(?<remove>atlases)$/', 'atlas'],
79
        ['/(.*)(?<remove>people)$/', 'person'],
80
        ['/(.*)(?<remove>teeth)$/', 'tooth'],
81
        ['/(.*)(iri)(?<remove>ses)$/', 's'],
82
        ['/(.*)(h|l|p)(ou)(?<remove>ses)$/', 'se'],
83
        ['/(.*)(ro|po|ca)(?<remove>ses)$/', 'se'],
84
        ['/(.*)(?<remove>quizzes)$/', 'quiz'],
85
        ['/(.*)(?<remove>zes)$/', 'z'],
86
        ['/(.*)(y|i|a|o|e)(?<remove>ses)$/', 'sis'],
87
        ['/(.*)(?<remove>ses)$/', 's'],
88
        ['/(.*)(?<remove>ice)$/', 'ouse'],
89
        ['/(.*)(?<remove>xes)$/', 'x'],
90
        ['/(.*)(?<remove>eaux)$/', 'eau'],
91
        ['/(formul|alumn|nebul)(?<remove>ae)$/', 'a'],
92
        ['/(foc|alumn|fung|nucle|octop|radi|syllab)(?<remove>i)$/', 'us'],
93
        ['/(.*)(?<remove>hes)$/', 'h'],
94
        ['/(.*)(ca|mo|lo)(?<remove>ves)$/', 've'],
95
        ['/(.*)(l|r|o|a|e)(?<remove>ves)$/', 'f'],
96
        ['/(.*)(li|ni|wi)(?<remove>ves)$/', 'fe'],
97
        ['/(.*)(?<remove>s)$/', ''],
98
    ];
99
100
    private static $noPlurals = [
101
        'cod', 'deer', 'feedback', 'fish', 'moose', 'news', 'species', 'series', 'sheep', 'rice'
102
    ];
103
104
    /**
105
     * Converts text separated by a specified separator to camel case. 
106
     * This function converts the entire text into lower case before performing the
107
     * camel case conversion. Due to this the first character would be lowercased.
108
     * 
109
     * @param string $string The text to be converted.
110
     * @param string $separator The separator to consider for camel casing
111
     * @return string
112
     */
113 1
    public static function camelize($string, $separator = '_') : string
114
    {
115 1
        if(is_array($separator))
116
        {
117 1
            $separator = "(\\" . implode("|\\", $separator) . ")";
118
        }
119
        else
120
        {
121 1
            $separator = '\\' . $separator;
122
        }
123 1
        return preg_replace_callback(
124 1
            "/{$separator}[a-zA-Z]/", 
125 1
            function ($matches) 
126
            {
127 1
                return strtoupper($matches[0][1]);
128 1
            }, 
129 1
            strtolower($string)
130
        );
131
    }
132
    
133
    /**
134
     * Converts text separated by a specified separator to camel case. 
135
     * This method works just as the Text::camelize method except that it converts
136
     * the first character to uppercase.
137
     * 
138
     * @param string $string The text to be converted.
139
     * @param string $separator The separator to consider for camel casing
140
     * @return string
141
     */
142 1
    public static function ucamelize($string, $separator = '_') : string
143
    {
144 1
        return ucfirst(self::camelize($string, $separator));
145
    }
146
    
147
    /**
148
     * Converts camel case text into regular text separated with an arbitrary separator.
149
     * By default the seperator is an underscore. A space can also be used as the 
150
     * seperator in cases where the conversion is to an English sentence.
151
     * 
152
     * @param string $string The text to be converted.
153
     * @param string $separator The separator to be used.
154
     * @return string
155
     */    
156 1
    public static function deCamelize($string, $separator = '_') : string
157
    {
158 1
        return preg_replace_callback(
159 1
            "/[A-Z][a-z]/", 
160 1
            function ($matches) use($separator) 
161
            {
162 1
                return $separator . strtolower($matches[0]);
163 1
            }, 
164 1
            lcfirst($string)
165
        );        
166
    }
167
168
    /**
169
     * Run through the rules and generate a text transformation.
170
     *
171
     * @param string $text
172
     * @param array $rules
173
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
174
     */
175 320
    private static function runInflection($text, $rules)
176
    {
177 320
        if(in_array($text, self::$noPlurals)) {
178 18
            return $text;
179
        }
180 302
        foreach($rules as $rule) {
181 302
            if(preg_match($rule[0], $text, $matches)) {
182 302
                return substr($text, 0, strlen($text) - strlen($matches['remove'] ?? '')) . $rule[1];
183
            }
184
        }
185
    }
186
    
187
    /**
188
     * Generates the english plural of a given word.
189
     *
190
     * @param string $text
191
     * @return string
192
     */
193 166
    public static function pluralize($text) : string
194
    {
195 166
        return self::runInflection($text, self::$pluralRules);
196
    }
197
    
198
    /**
199
     * Generates the english singular of a given word.
200
     *
201
     * @param string $text
202
     * @return string
203
     */
204 154
    public static function singularize($text) : string
205
    {
206 154
        return self::runInflection($text, self::$singularRules) ?? $text;
207
    }
208
}
209