forked from joomla/joomla-platform
/
normalise.php
159 lines (139 loc) · 3.48 KB
/
normalise.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
* @package Joomla.Platform
* @subpackage String
*
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
defined('JPATH_PLATFORM') or die;
/**
* Joomla Platform String Normalise Class
*
* @package Joomla.Platform
* @subpackage String
* @since 11.3
*/
abstract class JStringNormalise
{
/**
* Method to convert a string from camel case.
*
* @param string $input The string input.
*
* @return string The space separated string.
*
* @since 12.1
*/
public static function fromCamelCase($input)
{
return trim(preg_replace('#([A-Z])#', ' $1', $input));
}
/**
* Method to convert a string into camel case.
*
* @param string $input The string input.
*
* @return string The camel case string.
*
* @since 11.3
*/
public static function toCamelCase($input)
{
// Convert words to uppercase and then remove spaces.
$input = self::toSpaceSeparated($input);
$input = ucwords($input);
$input = str_ireplace(' ', '', $input);
return $input;
}
/**
* Method to convert a string into dash separated form.
*
* @param string $input The string input.
*
* @return string The dash separated string.
*
* @since 11.3
*/
public static function toDashSeparated($input)
{
// Convert spaces and underscores to dashes.
$input = str_ireplace(array(' ', '_'), '-', $input);
// Remove duplicate dashes.
$input = preg_replace('#-+#', '-', $input);
return $input;
}
/**
* Method to convert a string into space separated form.
*
* @param string $input The string input.
*
* @return string The space separated string.
*
* @since 11.3
*/
public static function toSpaceSeparated($input)
{
// Convert underscores and dashes to spaces.
$input = str_ireplace(array('_', '-'), ' ', $input);
// Remove duplicate spaces.
$input = preg_replace('#\s+#', ' ', $input);
return $input;
}
/**
* Method to convert a string into underscore separated form.
*
* @param string $input The string input.
*
* @return string The underscore separated string.
*
* @since 11.3
*/
public static function toUnderscoreSeparated($input)
{
// Convert spaces and dashes to underscores.
$input = str_ireplace(array(' ', '-'), '_', $input);
// Remove duplicate underscores.
$input = preg_replace('#_+#', '_', $input);
return $input;
}
/**
* Method to convert a string into variable form.
*
* @param string $input The string input.
*
* @return string The variable string.
*
* @since 11.3
*/
public static function toVariable($input)
{
// Remove dashes and underscores, then convert to camel case.
$input = self::toSpaceSeparated($input);
$input = self::toCamelCase($input);
// Remove leading digits.
$input = preg_replace('#^[0-9]+.*$#', '', $input);
// Lowercase the first character.
$first = substr($input, 0, 1);
$first = strtolower($first);
// Replace the first character with the lowercase character.
$input = substr_replace($input, $first, 0, 1);
return $input;
}
/**
* Method to convert a string into key form.
*
* @param string $input The string input.
*
* @return string The key string.
*
* @since 11.3
*/
public static function toKey($input)
{
// Remove spaces and dashes, then convert to lower case.
$input = self::toUnderscoreSeparated($input);
$input = strtolower($input);
return $input;
}
}