1 <?php
2 /**
3 * Part of the Joomla Framework String Package
4 *
5 * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
6 * @license GNU General Public License version 2 or later; see LICENSE
7 */
8
9 namespace Joomla\String;
10
11 /**
12 * Joomla Framework String Normalise Class
13 *
14 * @since 1.0
15 */
16 abstract class Normalise
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 string The space separated string.
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
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 = self::toSpaceSeparated($input);
60 $input = ucwords($input);
61 $input = str_ireplace(' ', '', $input);
62
63 return $input;
64 }
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 $input = preg_replace('#[ \-_]+#', '-', $input);
79
80 return $input;
81 }
82
83 /**
84 * Method to convert a string into space separated form.
85 *
86 * @param string $input The string input (ASCII only).
87 *
88 * @return string The space separated string.
89 *
90 * @since 1.0
91 */
92 public static function toSpaceSeparated($input)
93 {
94 // Convert underscores and dashes to spaces.
95 $input = preg_replace('#[ \-_]+#', ' ', $input);
96
97 return $input;
98 }
99
100 /**
101 * Method to convert a string into underscore separated form.
102 *
103 * @param string $input The string input (ASCII only).
104 *
105 * @return string The underscore separated string.
106 *
107 * @since 1.0
108 */
109 public static function toUnderscoreSeparated($input)
110 {
111 // Convert spaces and dashes to underscores.
112 $input = preg_replace('#[ \-_]+#', '_', $input);
113
114 return $input;
115 }
116
117 /**
118 * Method to convert a string into variable form.
119 *
120 * @param string $input The string input (ASCII only).
121 *
122 * @return string The variable string.
123 *
124 * @since 1.0
125 */
126 public static function toVariable($input)
127 {
128 // Remove dashes and underscores, then convert to camel case.
129 $input = self::toSpaceSeparated($input);
130 $input = self::toCamelCase($input);
131
132 // Remove leading digits.
133 $input = preg_replace('#^[0-9]+#', '', $input);
134
135 // Lowercase the first character.
136 $first = substr($input, 0, 1);
137 $first = strtolower($first);
138
139 // Replace the first character with the lowercase character.
140 $input = substr_replace($input, $first, 0, 1);
141
142 return $input;
143 }
144
145 /**
146 * Method to convert a string into key form.
147 *
148 * @param string $input The string input (ASCII only).
149 *
150 * @return string The key string.
151 *
152 * @since 1.0
153 */
154 public static function toKey($input)
155 {
156 // Remove spaces and dashes, then convert to lower case.
157 $input = self::toUnderscoreSeparated($input);
158 $input = strtolower($input);
159
160 return $input;
161 }
162 }
163