|
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
|
|
View Code Duplication |
if(substr($text, -1) == 'y') { |
|
|
|
|
|
|
100
|
|
|
return substr($text, 0, -1) . 'ies'; |
|
101
|
|
|
} else { |
|
102
|
|
|
return $text . 's'; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public static function singularize($text) |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
|
|
if(substr($text, -1) == 's') { |
|
109
|
|
|
return substr($text, 0, -1); |
|
110
|
|
View Code Duplication |
} elseif (substr($text, -3) == 'ies') { |
|
|
|
|
|
|
111
|
|
|
return substr($text, 0, -3) . 'y'; |
|
112
|
|
|
} |
|
113
|
|
|
return $text; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
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.