CamelCase::pluralize()   C
last analyzed

Complexity

Conditions 8
Paths 8

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 6.1403
c 0
b 0
f 0
cc 8
eloc 18
nc 8
nop 1
1
<?php
2
3
namespace Scaffolder\Support;
4
5
class CamelCase
6
{
7
	/**
8
	 * Given an underscore_separated_string, this will convert the string
9
	 * to CamelCaseNotation.  Note that this will ignore any casing in the
10
	 * underscore separated string.
11
	 * 
12
	 * @param string $strString
13
	 * @return string
14
	 */
15
	public static function convertToCamelCase($strString) {
16
		return str_replace(' ', '', ucwords(str_replace('_', ' ', strtolower($strString))));
17
	}
18
    
19
	public static function underscoreFromCamelCase($strName) {
20
		if (strlen($strName) == 0) return '';
21
22
		$strToReturn = self::FirstCharacter($strName);
23
24
		for ($intIndex = 1; $intIndex < strlen($strName); $intIndex++) {
25
			$strChar = substr($strName, $intIndex, 1);
26
			if (strtoupper($strChar) == $strChar)
27
				$strToReturn .= '_' . $strChar;
28
			else
29
				$strToReturn .= $strChar;
30
		}
31
		
32
		return strtolower($strToReturn);
33
	}   
34
35
	/**
36
	 * Returns the first character of a given string, or null if the given
37
	 * string is null.
38
	 * @param string $strString 
39
	 * @return string the first character, or null
40
	 */
41
	public final static function firstCharacter($strString) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, final should precede the visibility keyword.
Loading history...
42
		if (strlen($strString) > 0)
43
			return substr($strString, 0 , 1);
44
		else
45
			return null;
46
	}    
47
48
49
	public static function pluralize($strName) {
50
			// Special Rules go Here
51
			switch (true) {	
52
				case (strtolower($strName) == 'play'):
53
					return $strName . 's';
54
			}
55
56
			$intLength = strlen($strName);
57
			if (substr($strName, $intLength - 1) == "y")
58
				return substr($strName, 0, $intLength - 1) . "ies";
59
			if (substr($strName, $intLength - 1) == "s")
60
				return $strName . "es";
61
			if (substr($strName, $intLength - 1) == "x")
62
				return $strName . "es";
63
			if (substr($strName, $intLength - 1) == "z")
64
				return $strName . "zes";
65
			if (substr($strName, $intLength - 2) == "sh")
66
				return $strName . "es";
67
			if (substr($strName, $intLength - 2) == "ch")
68
				return $strName . "es";
69
70
			return $strName . "s";
71
		}
72
	
73
}