Completed
Push — dev ( a4bff0...bd8529 )
by James Ekow Abaka
03:29
created

Text   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 78.13%

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 133
ccs 25
cts 32
cp 0.7813
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A camelize() 0 19 2
A ucamelize() 0 4 1
A deCamelize() 0 11 1
A pluralize() 0 11 4
A singularize() 0 9 3
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 $noPlurals = [
64
        'cod', 'deer', 'feedback', 'fish', 'moose', 'news', 'species', 'series', 'sheep'
65
    ];
66
67
    /**
68
     * Converts text separated by a specified separator to camel case. 
69
     * This function converts the entire text into lower case before performing the
70
     * camel case conversion. Due to this the first character would be lowercased.
71
     * 
72
     * @param string $string The text to be converted.
73
     * @param string $separator The separator to consider for camel casing
74
     * @return string
75
     */
76 1
    public static function camelize($string, $separator = '_') : string
77
    {
78 1
        if(is_array($separator))
79
        {
80 1
            $separator = "(\\" . implode("|\\", $separator) . ")";
81
        }
82
        else
83
        {
84 1
            $separator = '\\' . $separator;
85
        }
86 1
        return preg_replace_callback(
87 1
            "/{$separator}[a-zA-Z]/", 
88 1
            function ($matches) 
89
            {
90 1
                return strtoupper($matches[0][1]);
91 1
            }, 
92 1
            strtolower($string)
93
        );
94
    }
95
    
96
    /**
97
     * Converts text separated by a specified separator to camel case. 
98
     * This method works just as the Text::camelize method except that it converts
99
     * the first character to uppercase.
100
     * 
101
     * @param string $string The text to be converted.
102
     * @param string $separator The separator to consider for camel casing
103
     * @return string
104
     */
105 1
    public static function ucamelize($string, $separator = '_') : string
106
    {
107 1
        return ucfirst(self::camelize($string, $separator));
108
    }
109
    
110
    /**
111
     * Converts camel case text into regular text separated with an arbitrary separator.
112
     * By default the seperator is an underscore. A space can also be used as the 
113
     * seperator in cases where the conversion is to an English sentence.
114
     * 
115
     * @param string $string The text to be converted.
116
     * @param string $separator The separator to be used.
117
     * @return string
118
     */    
119 1
    public static function deCamelize($string, $separator = '_') : string
120
    {
121 1
        return preg_replace_callback(
122 1
            "/[A-Z][a-z]/", 
123 1
            function ($matches) use($separator) 
124
            {
125 1
                return $separator . strtolower($matches[0]);
126 1
            }, 
127 1
            lcfirst($string)
128
        );        
129
    }
130
    
131
    /**
132
     * Generates the english plural of a given word.
133
     *
134
     * @param string $text
135
     * @return string
136
     */
137 192
    public static function pluralize($text) : string
138
    {
139 192
        if(in_array($text, self::$noPlurals)) {
140 9
            return $text;
141
        }
142 183
        foreach(self::$pluralRules as $rule) {
143 183
            if(preg_match($rule[0], $text, $matches)) {
144 183
                return substr($text, 0, strlen($text) - strlen($matches['remove'] ?? '')) . $rule[1];
145
            }
146
        }
147
    }
148
    
149
    /**
150
     * Generates the english singular of a given word.
151
     *
152
     * @param string $text
153
     * @return string
154
     */
155
    public static function singularize($text) : string
156
    {
157
        if (substr($text, -3) == 'ies') {
158
            return substr($text, 0, -3) . 'y';
159
        } elseif(substr($text, -1) == 's') {
160
            return substr($text, 0, -1);
161
        }
162
        return $text;
163
    }
164
}
165