-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.power
More file actions
163 lines (138 loc) · 4.66 KB
/
code.power
File metadata and controls
163 lines (138 loc) · 4.66 KB
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
160
161
162
163
/**
* The field builder switch
*
* @since 3.0.9
*/
protected static int $builder = 0;
/**
* Cache for processed field type names to avoid redundant computations.
*
* @since 5.1.1
*/
protected static array $cache = [];
/**
* Making field type name safe
*
* @param String $string The you would like to make safe
* @param String $option The option for the component.
*
* @returns string on success
*
* @since 3.0.9
*/
public static function safe($string, $option = null): string
{
// Check if result is already cached
if (isset(self::$cache[$string]))
{
return self::$cache[$string];
}
// get global value
if (self::$builder == 0)
{
self::$builder = (int) Helper::getParams($option)->get('type_name_builder', 2);
}
// Apply new naming convention
if (2 == self::$builder)
{
// Ensure input is a valid string
if (StringHelper::check($string))
{
// Ensure the first character is not a number
if (is_numeric(substr($string, 0, 1)))
{
$string = StringHelper::numbers($string);
}
// Detect if camel case is used
$has_camel_case = preg_match('/[a-z][A-Z]/', $string);
// Detect non-ASCII characters (extended Unicode)
$has_non_ascii = preg_match('/[^\x20-\x7E]/', $string); // Anything outside printable ASCII
// Detect non-standard symbols (anything that is not a letter, number, underscore, or dot)
$has_non_standard_symbols = preg_match('/[^a-zA-Z0-9_.]/', $string);
// Only transliterate if necessary:
// - If it contains non-ASCII characters, or
// - If it has non-standard symbols that may affect safe usage
if ($has_non_ascii || $has_non_standard_symbols)
{
// Store original casing before transliteration
$original_casing = $string;
// Run transliteration
$string = StringHelper::transliterate($string);
// If original had camel case and transliterate altered casing, restore it
if ($has_camel_case)
{
// Use original casing where possible, as Joomla's transliteration might enforce lowercase
$string = self::restoreCamelCase($original_casing, $string);
}
}
// Clean the string while preserving camelCase
$cleaned = self::clean($string);
// Cache the result
self::$cache[$string] = $cleaned;
return $cleaned;
}
return '';
}
// Default behaviour for legacy convention
$result = StringHelper::safe($string);
// Cache the result
self::$cache[$string] = $result;
return $result;
}
/**
* Restores the camel case pattern from the original string after transliteration.
*
* @param string $original The original string before transliteration.
* @param string $modified The string after transliteration.
*
* @return string The string with restored camel case.
* @since 5.1.1
*/
protected static function restoreCamelCase(string $original, string $modified): string
{
// Split original and modified strings into character arrays
$origChars = preg_split('//u', $original, -1, PREG_SPLIT_NO_EMPTY);
$modChars = preg_split('//u', $modified, -1, PREG_SPLIT_NO_EMPTY);
// Ensure lengths match before processing
if (count($origChars) !== count($modChars))
{
return $modified; // Return as-is if lengths differ (indicating complex transformation)
}
// Restore camel case where applicable
foreach ($origChars as $index => $char)
{
if (ctype_upper($char))
{
$modChars[$index] = strtoupper($modChars[$index]);
}
}
return implode('', $modChars);
}
/**
* Cleans type name string by:
* - Removing all characters except letters, numbers, underscores, and periods.
* - Allowing only one period (the first period in the string).
*
* @param string $string Type name string to clean.
*
* @return string Cleaned string with only one period (first occurrence).
* @since 5.1.1
*/
protected static function clean(string $string): string
{
// Step 1: Remove unwanted characters (allow letters, numbers, underscores, and periods)
$string = preg_replace('/[^A-Za-z0-9_.]/', '', $string);
// Step 2: Keep only the first period, remove all subsequent periods
$parts = explode('.', $string, 2); // Split on first period only
if (count($parts) === 2)
{
// Reconstruct string: first part + '.' + remove any further periods from second part
$cleaned_string = $parts[0] . '.' . str_replace('.', '', $parts[1]);
}
else
{
// No period or just one part, no further action needed
$cleaned_string = $string;
}
return trim($cleaned_string);
}