|
15 | 15 | */
|
16 | 16 | abstract class Normalise
|
17 | 17 | {
|
18 |
| - /** |
19 |
| - * Method to convert a string from camel case. |
20 |
| - * |
21 |
| - * This method offers two modes. Grouped allows for splitting on groups of uppercase characters as follows: |
22 |
| - * |
23 |
| - * "FooBarABCDef" becomes array("Foo", "Bar", "ABC", "Def") |
24 |
| - * "JFooBar" becomes array("J", "Foo", "Bar") |
25 |
| - * "J001FooBar002" becomes array("J001", "Foo", "Bar002") |
26 |
| - * "abcDef" becomes array("abc", "Def") |
27 |
| - * "abc_defGhi_Jkl" becomes array("abc_def", "Ghi_Jkl") |
28 |
| - * "ThisIsA_NASAAstronaut" becomes array("This", "Is", "A_NASA", "Astronaut")) |
29 |
| - * "JohnFitzgerald_Kennedy" becomes array("John", "Fitzgerald_Kennedy")) |
30 |
| - * |
31 |
| - * Non-grouped will split strings at each uppercase character. |
32 |
| - * |
33 |
| - * @param string $input The string input (ASCII only). |
34 |
| - * @param boolean $grouped Optionally allows splitting on groups of uppercase characters. |
35 |
| - * |
36 |
| - * @return array|string The space separated string, as an array if grouped. |
37 |
| - * |
38 |
| - * @since 1.0 |
39 |
| - */ |
40 |
| - public static function fromCamelCase($input, $grouped = false) |
41 |
| - { |
42 |
| - return $grouped |
43 |
| - ? preg_split('/(?<=[^A-Z_])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][^A-Z_])/x', $input) |
44 |
| - : trim(preg_replace('#([A-Z])#', ' $1', $input)); |
45 |
| - } |
| 18 | + /** |
| 19 | + * Method to convert a string from camel case. |
| 20 | + * |
| 21 | + * This method offers two modes. Grouped allows for splitting on groups of uppercase characters as follows: |
| 22 | + * |
| 23 | + * "FooBarABCDef" becomes array("Foo", "Bar", "ABC", "Def") |
| 24 | + * "JFooBar" becomes array("J", "Foo", "Bar") |
| 25 | + * "J001FooBar002" becomes array("J001", "Foo", "Bar002") |
| 26 | + * "abcDef" becomes array("abc", "Def") |
| 27 | + * "abc_defGhi_Jkl" becomes array("abc_def", "Ghi_Jkl") |
| 28 | + * "ThisIsA_NASAAstronaut" becomes array("This", "Is", "A_NASA", "Astronaut")) |
| 29 | + * "JohnFitzgerald_Kennedy" becomes array("John", "Fitzgerald_Kennedy")) |
| 30 | + * |
| 31 | + * Non-grouped will split strings at each uppercase character. |
| 32 | + * |
| 33 | + * @param string $input The string input (ASCII only). |
| 34 | + * @param boolean $grouped Optionally allows splitting on groups of uppercase characters. |
| 35 | + * |
| 36 | + * @return array|string The space separated string, as an array if grouped. |
| 37 | + * |
| 38 | + * @since 1.0 |
| 39 | + */ |
| 40 | + public static function fromCamelCase($input, $grouped = false) |
| 41 | + { |
| 42 | + return $grouped |
| 43 | + ? preg_split('/(?<=[^A-Z_])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][^A-Z_])/x', $input) |
| 44 | + : trim(preg_replace('#([A-Z])#', ' $1', $input)); |
| 45 | + } |
46 | 46 |
|
47 |
| - /** |
48 |
| - * Method to convert a string into camel case. |
49 |
| - * |
50 |
| - * @param string $input The string input (ASCII only). |
51 |
| - * |
52 |
| - * @return string The camel case string. |
53 |
| - * |
54 |
| - * @since 1.0 |
55 |
| - */ |
56 |
| - public static function toCamelCase($input) |
57 |
| - { |
58 |
| - // Convert words to uppercase and then remove spaces. |
59 |
| - $input = static::toSpaceSeparated($input); |
60 |
| - $input = ucwords($input); |
61 |
| - $input = str_ireplace(' ', '', $input); |
| 47 | + /** |
| 48 | + * Method to convert a string into camel case. |
| 49 | + * |
| 50 | + * @param string $input The string input (ASCII only). |
| 51 | + * |
| 52 | + * @return string The camel case string. |
| 53 | + * |
| 54 | + * @since 1.0 |
| 55 | + */ |
| 56 | + public static function toCamelCase($input) |
| 57 | + { |
| 58 | + // Convert words to uppercase and then remove spaces. |
| 59 | + $input = static::toSpaceSeparated($input); |
| 60 | + $input = ucwords($input); |
| 61 | + $input = str_ireplace(' ', '', $input); |
62 | 62 |
|
63 |
| - return $input; |
64 |
| - } |
| 63 | + return $input; |
| 64 | + } |
65 | 65 |
|
66 |
| - /** |
67 |
| - * Method to convert a string into dash separated form. |
68 |
| - * |
69 |
| - * @param string $input The string input (ASCII only). |
70 |
| - * |
71 |
| - * @return string The dash separated string. |
72 |
| - * |
73 |
| - * @since 1.0 |
74 |
| - */ |
75 |
| - public static function toDashSeparated($input) |
76 |
| - { |
77 |
| - // Convert spaces and underscores to dashes. |
78 |
| - return preg_replace('#[ \-_]+#', '-', $input); |
79 |
| - } |
| 66 | + /** |
| 67 | + * Method to convert a string into dash separated form. |
| 68 | + * |
| 69 | + * @param string $input The string input (ASCII only). |
| 70 | + * |
| 71 | + * @return string The dash separated string. |
| 72 | + * |
| 73 | + * @since 1.0 |
| 74 | + */ |
| 75 | + public static function toDashSeparated($input) |
| 76 | + { |
| 77 | + // Convert spaces and underscores to dashes. |
| 78 | + return preg_replace('#[ \-_]+#', '-', $input); |
| 79 | + } |
80 | 80 |
|
81 |
| - /** |
82 |
| - * Method to convert a string into space separated form. |
83 |
| - * |
84 |
| - * @param string $input The string input (ASCII only). |
85 |
| - * |
86 |
| - * @return string The space separated string. |
87 |
| - * |
88 |
| - * @since 1.0 |
89 |
| - */ |
90 |
| - public static function toSpaceSeparated($input) |
91 |
| - { |
92 |
| - // Convert underscores and dashes to spaces. |
93 |
| - return preg_replace('#[ \-_]+#', ' ', $input); |
94 |
| - } |
| 81 | + /** |
| 82 | + * Method to convert a string into space separated form. |
| 83 | + * |
| 84 | + * @param string $input The string input (ASCII only). |
| 85 | + * |
| 86 | + * @return string The space separated string. |
| 87 | + * |
| 88 | + * @since 1.0 |
| 89 | + */ |
| 90 | + public static function toSpaceSeparated($input) |
| 91 | + { |
| 92 | + // Convert underscores and dashes to spaces. |
| 93 | + return preg_replace('#[ \-_]+#', ' ', $input); |
| 94 | + } |
95 | 95 |
|
96 |
| - /** |
97 |
| - * Method to convert a string into underscore separated form. |
98 |
| - * |
99 |
| - * @param string $input The string input (ASCII only). |
100 |
| - * |
101 |
| - * @return string The underscore separated string. |
102 |
| - * |
103 |
| - * @since 1.0 |
104 |
| - */ |
105 |
| - public static function toUnderscoreSeparated($input) |
106 |
| - { |
107 |
| - // Convert spaces and dashes to underscores. |
108 |
| - return preg_replace('#[ \-_]+#', '_', $input); |
109 |
| - } |
| 96 | + /** |
| 97 | + * Method to convert a string into underscore separated form. |
| 98 | + * |
| 99 | + * @param string $input The string input (ASCII only). |
| 100 | + * |
| 101 | + * @return string The underscore separated string. |
| 102 | + * |
| 103 | + * @since 1.0 |
| 104 | + */ |
| 105 | + public static function toUnderscoreSeparated($input) |
| 106 | + { |
| 107 | + // Convert spaces and dashes to underscores. |
| 108 | + return preg_replace('#[ \-_]+#', '_', $input); |
| 109 | + } |
110 | 110 |
|
111 |
| - /** |
112 |
| - * Method to convert a string into variable form. |
113 |
| - * |
114 |
| - * @param string $input The string input (ASCII only). |
115 |
| - * |
116 |
| - * @return string The variable string. |
117 |
| - * |
118 |
| - * @since 1.0 |
119 |
| - */ |
120 |
| - public static function toVariable($input) |
121 |
| - { |
122 |
| - // Remove dashes and underscores, then convert to camel case. |
123 |
| - $input = static::toCamelCase($input); |
| 111 | + /** |
| 112 | + * Method to convert a string into variable form. |
| 113 | + * |
| 114 | + * @param string $input The string input (ASCII only). |
| 115 | + * |
| 116 | + * @return string The variable string. |
| 117 | + * |
| 118 | + * @since 1.0 |
| 119 | + */ |
| 120 | + public static function toVariable($input) |
| 121 | + { |
| 122 | + // Remove dashes and underscores, then convert to camel case. |
| 123 | + $input = static::toCamelCase($input); |
124 | 124 |
|
125 |
| - // Remove leading digits. |
126 |
| - $input = preg_replace('#^[0-9]+#', '', $input); |
| 125 | + // Remove leading digits. |
| 126 | + $input = preg_replace('#^[0-9]+#', '', $input); |
127 | 127 |
|
128 |
| - // Lowercase the first character. |
129 |
| - $input = lcfirst($input); |
| 128 | + // Lowercase the first character. |
| 129 | + $input = lcfirst($input); |
130 | 130 |
|
131 |
| - return $input; |
132 |
| - } |
| 131 | + return $input; |
| 132 | + } |
133 | 133 |
|
134 |
| - /** |
135 |
| - * Method to convert a string into key form. |
136 |
| - * |
137 |
| - * @param string $input The string input (ASCII only). |
138 |
| - * |
139 |
| - * @return string The key string. |
140 |
| - * |
141 |
| - * @since 1.0 |
142 |
| - */ |
143 |
| - public static function toKey($input) |
144 |
| - { |
145 |
| - // Remove spaces and dashes, then convert to lower case. |
146 |
| - return strtolower(static::toUnderscoreSeparated($input)); |
147 |
| - } |
| 134 | + /** |
| 135 | + * Method to convert a string into key form. |
| 136 | + * |
| 137 | + * @param string $input The string input (ASCII only). |
| 138 | + * |
| 139 | + * @return string The key string. |
| 140 | + * |
| 141 | + * @since 1.0 |
| 142 | + */ |
| 143 | + public static function toKey($input) |
| 144 | + { |
| 145 | + // Remove spaces and dashes, then convert to lower case. |
| 146 | + return strtolower(static::toUnderscoreSeparated($input)); |
| 147 | + } |
148 | 148 | }
|
0 commit comments