Completed
Push — master ( 32467f...42bb8a )
by James Ekow Abaka
01:22
created

Text   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 59.38%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 0
dl 0
loc 99
ccs 19
cts 32
cp 0.5938
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A ucamelize() 0 4 1
A deCamelize() 0 11 1
A pluralize() 0 10 3
A singularize() 0 9 3
A camelize() 0 19 2
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
    /**
35
     * Converts text separated by a specified separator to camel case. 
36
     * This function converts the entire text into lower case before performing the
37
     * camel case conversion. Due to this the first character would be lowercased.
38
     * 
39
     * @param string $string The text to be converted.
40
     * @param string $separator The separator to consider for camel casing
41
     * @return string
42
     */
43 1
    public static function camelize($string, $separator = '_') : string
44
    {
45 1
        if(is_array($separator))
46
        {
47 1
            $separator = "(\\" . implode("|\\", $separator) . ")";
48
        }
49
        else
50
        {
51 1
            $separator = '\\' . $separator;
52
        }
53 1
        return preg_replace_callback(
54 1
            "/{$separator}[a-zA-Z]/", 
55 1
            function ($matches) 
56
            {
57 1
                return strtoupper($matches[0][1]);
58 1
            }, 
59 1
            strtolower($string)
60
        );
61
    }
62
    
63
    /**
64
     * Converts text separated by a specified separator to camel case. 
65
     * This method works just as the Text::camelize method except that it converts
66
     * the first character to uppercase.
67
     * 
68
     * @param string $string The text to be converted.
69
     * @param string $separator The separator to consider for camel casing
70
     * @return string
71
     */
72 1
    public static function ucamelize($string, $separator = '_') : string
73
    {
74 1
        return ucfirst(self::camelize($string, $separator));
75
    }
76
    
77
    /**
78
     * Converts camel case text into regular text separated with an arbitrary separator.
79
     * By default the seperator is an underscore. A space can also be used as the 
80
     * seperator in cases where the conversion is to an English sentence.
81
     * 
82
     * @param string $string The text to be converted.
83
     * @param string $separator The separator to be used.
84
     * @return string
85
     */    
86 1
    public static function deCamelize($string, $separator = '_') : string
87
    {
88 1
        return preg_replace_callback(
89 1
            "/[A-Z][a-z]/", 
90 1
            function ($matches) use($separator) 
91
            {
92 1
                return $separator . strtolower($matches[0]);
93 1
            }, 
94 1
            lcfirst($string)
95
        );        
96
    }
97
    
98
    /**
99
     * Generates the english plural of a given word.
100
     *
101
     * @param string $text
102
     * @return string
103
     */
104
    public static function pluralize($text) : string
105
    {
106
        $lastLetter = substr($text, -1);
107
        if($lastLetter == 'y') {
108
            return substr($text, 0, -1) . 'ies';
109
        } elseif ( $lastLetter != 's' ) {
110
            return $text . 's';
111
        }
112
        return $text;
113
    }
114
    
115
    /**
116
     * Generates the english singular of a given word.
117
     *
118
     * @param string $text
119
     * @return string
120
     */
121
    public static function singularize($text) : string
122
    {
123
        if(substr($text, -1) == 's') {
124
            return substr($text, 0, -1);
125
        } elseif (substr($text, -3) == 'ies') {
126
            return substr($text, 0, -3) . 'y';
127
        }
128
        return $text;
129
    }
130
}
131