1: <?php
2:
3: /**
4: * @package Codegen
5: */
6:
7: /**
8: * Class QConvertNotationBase: Helps convert notations from entities
9: */
10: abstract class QConvertNotationBase {
11: /**
12: * Returns prefix for variable according to variable type
13: *
14: * @param string $strType The type of variable for which the prefix is needed
15: *
16: * @return string The variable prefix
17: */
18: public static function PrefixFromType($strType) {
19: switch ($strType) {
20: case QType::ArrayType:
21: return "obj";
22: case QType::Boolean:
23: return "bln";
24: case QType::DateTime:
25: return "dtt";
26: case QType::Float:
27: return "flt";
28: case QType::Integer:
29: return "int";
30: case QType::Object:
31: return "obj";
32: case QType::String:
33: return "str";
34: }
35: // Suppressing the IDE warning about no value being return
36: return "";
37: }
38:
39: /**
40: * Replaces underscores with spaces and makes the first character of all the words as uppercase
41: *
42: * @param string $strName String which has to be converted into single words
43: *
44: * @return string The resulting string (as words)
45: */
46: public static function WordsFromUnderscore($strName) {
47: $strToReturn = trim(str_replace('_', ' ', $strName));
48: if (strtolower($strToReturn) == $strToReturn)
49: return ucwords($strToReturn);
50: return $strToReturn;
51: }
52:
53: /**
54: * Converts a underscored word into a CamelCased word
55: *
56: * @param string $strName String to be converted
57: *
58: * @return string The resulting camel-cased word
59: */
60: public static function CamelCaseFromUnderscore($strName) {
61: $strToReturn = '';
62:
63: // If entire underscore string is all uppercase, force to all lowercase
64: // (mixed case and all lowercase can remain as is)
65: if ($strName == strtoupper($strName))
66: $strName = strtolower($strName);
67:
68: while (($intPosition = strpos($strName, "_")) !== false) {
69: // Use 'ucfirst' to create camelcasing
70: $strName = ucfirst($strName);
71: if ($intPosition == 0) {
72: $strName = substr($strName, 1);
73: } else {
74: $strToReturn .= substr($strName, 0, $intPosition);
75: $strName = substr($strName, $intPosition + 1);
76: }
77: }
78:
79: $strToReturn .= ucfirst($strName);
80: return $strToReturn;
81: }
82:
83: /**
84: * Converts a CamelCased word into separate words
85: *
86: * @param string $strName String to be converted
87: *
88: * @return string Resulting set of words derived from camel case
89: */
90: public static function WordsFromCamelCase($strName) {
91: if (strlen($strName) == 0)
92: return '';
93:
94: $strToReturn = QString::FirstCharacter($strName);
95:
96: for ($intIndex = 1; $intIndex < strlen($strName); $intIndex++) {
97: // Get the current character we're examining
98: $strChar = substr($strName, $intIndex, 1);
99:
100: // Get the character previous to this
101: $strPrevChar = substr($strName, $intIndex - 1, 1);
102:
103: // If an upper case letter
104: if ((ord($strChar) >= ord('A')) &&
105: (ord($strChar) <= ord('Z')))
106: // Add a Space
107: $strToReturn .= ' ' . $strChar;
108:
109: // If a digit, and the previous character is NOT a digit
110: else if ((ord($strChar) >= ord('0')) &&
111: (ord($strChar) <= ord('9')) &&
112: ((ord($strPrevChar) < ord('0')) ||
113: (ord($strPrevChar) > ord('9'))))
114: // Add a space
115: $strToReturn .= ' ' . $strChar;
116:
117: // If a letter, and the previous character is a digit
118: else if ((ord(strtolower($strChar)) >= ord('a')) &&
119: (ord(strtolower($strChar)) <= ord('z')) &&
120: (ord($strPrevChar) >= ord('0')) &&
121: (ord($strPrevChar) <= ord('9')))
122: // Add a space
123: $strToReturn .= ' ' . $strChar;
124:
125: // Otherwise
126: else
127: // Don't add a space
128: $strToReturn .= $strChar;
129: }
130:
131: return $strToReturn;
132: }
133:
134: /**
135: * Given a CamelCased word, returns the underscored version
136: * example:
137: * CamelCased word: WeightInGrams
138: * underscored word: weight_in_grams
139: *
140: * @param string $strName CamelCased word
141: *
142: * @return string Underscored word
143: */
144: public static function UnderscoreFromCamelCase($strName) {
145: if (strlen($strName) == 0)
146: return '';
147:
148: $strToReturn = QString::FirstCharacter($strName);
149:
150: for ($intIndex = 1; $intIndex < strlen($strName); $intIndex++) {
151: $strChar = substr($strName, $intIndex, 1);
152: if (strtoupper($strChar) == $strChar)
153: $strToReturn .= '_' . $strChar;
154: else
155: $strToReturn .= $strChar;
156: }
157:
158: return strtolower($strToReturn);
159: }
160:
161: /**
162: * Returns a javaCase word given an underscore word
163: * example:
164: * underscore word: weight_in_grams
165: * javaCase word: weightInGrams
166: *
167: * javaCase words are like camel case words, except that the first character is lower case
168: *
169: * @param string $strName The underscored word
170: *
171: * @return string The word in javaCase
172: */
173: public static function JavaCaseFromUnderscore($strName) {
174: $strToReturn = QConvertNotation::CamelCaseFromUnderscore($strName);
175: return strtolower(substr($strToReturn, 0, 1)) . substr($strToReturn, 1);
176: }
177: }