Completed
Push — master ( 39ca89...64a0db )
by James Ekow Abaka
03:49
created

Text::ucamelize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 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
    /**
35
     * Converts text separated by a specified separator to camel case. This 
36
     * function converts the entire text into lower case before performing the
37
     * camel case conversion. Due to this the first character would be
38
     * lower cased.
39
     * 
40
     * @param string $string The text to be converted.
41
     * @param string $separator The separator to consider for camel casing
42
     * @return string
43
     */
44 1
    public static function camelize($string, $separator = '_')
45
    {
46 1
        if(is_array($separator))
47
        {
48 1
            $separator = "(\\" . implode("|\\", $separator) . ")";
49
        }
50
        else
51
        {
52 1
            $separator = '\\' . $separator;
53
        }
54 1
        return preg_replace_callback(
55 1
            "/{$separator}[a-zA-Z]/", 
56
            function ($matches) 
57
            {
58 1
                return strtoupper($matches[0][1]);
59 1
            }, 
60 1
            strtolower($string)
61
        );
62
    }
63
    
64
    /**
65
     * Converts text separated by a specified separator to camel case. This 
66
     * method works just as the Text::camelize method except that it converts
67
     * the first character to uppercase.
68
     * 
69
     * @param string $string The text to be converted.
70
     * @param string $separator The separator to consider for camel casing
71
     * @return string
72
     */
73 1
    public static function ucamelize($string, $separator = '_')
74
    {
75 1
        return ucfirst(self::camelize($string, $separator));
76
    }
77
    
78
    /**
79
     * Converts camel case text into text separated with an arbitrary separator.
80
     * 
81
     * @param string $string The text to be converted.
82
     * @param string $separator The separator to be used.
83
     * @return string
84
     */    
85 1
    public static function deCamelize($string, $separator = '_')
86
    {
87 1
        return preg_replace_callback(
88 1
            "/[A-Z][a-z]/", 
89 1
            function ($matches) use($separator) 
90
            {
91 1
                return $separator . strtolower($matches[0]);
92 1
            }, 
93 1
            lcfirst($string)
94
        );        
95
    }
96
    
97
    public static function pluralize($text)
98
    {
99
        return $text . 's';
100
    }
101
    
102
    public static function singularize($text)
103
    {
104
        return substr($text, 0, -1);
105
    }
106
}
107