-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathRegex.php
More file actions
170 lines (156 loc) · 4.37 KB
/
Copy pathRegex.php
File metadata and controls
170 lines (156 loc) · 4.37 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
164
165
166
167
168
169
170
<?php
namespace Envms\FluentPDO;
/**
* Regex class
*/
class Regex
{
/** @var string - All UTF-8 letter characters */
public const ALPHA = '\p{L}';
/** @var string - All UTF-8 letter and number characters */
public const ALNUM = '\p{L}\p{N}';
/** @var string - All valid SQL characters except the UTF-8 groupings with quotes and wildcards */
public const SQLCHARS = '\p{L}\p{N}\p{Pc}\p{Pd}\p{Pf}\p{Pi}';
/**
* Replace "camelCaseMethod" with "camel Case Method"
*
* @param string $subject
*
* @return null|string|string[]
*/
public function camelCaseSpaced(string $subject)
{
return preg_replace('/(.)([A-Z]+)/', '$1 $2', $subject);
}
/**
* Replace "SELECT * FROM table WHERE column = ?" with
* "SELECT *
* FROM table
* WHERE column = ?"
*
* @param string $subject
*
* @return null|string|string[]
*/
public function splitClauses(string $subject)
{
return preg_replace(
'/\b(WHERE|FROM|GROUP BY|HAVING|ORDER BY|LIMIT|OFFSET|UNION|ON DUPLICATE KEY UPDATE|VALUES|SET)\b/',
"\n$0",
$subject
);
}
/**
* Replace SELECT t2.id FROM t1 LEFT JOIN t2 ON t2.id = t1.t2_id" with
* "SELECT t2.id FROM t1
* LEFT JOIN t2 ON t2.id = t1.t2_id"
*
* @param string $subject
*
* @return null|string|string[]
*/
public function splitSubClauses(string $subject)
{
return preg_replace(
'/\b(INNER|OUTER|LEFT|RIGHT|FULL|CASE|WHEN|END|ELSE|AND|OR)\b/',
"\n $0",
$subject
);
}
/**
* Replace "WHERE column = ? " with "WHERE column = ?"
*
* @param string $subject
*
* @return null|string|string[]
*/
public function removeLineEndWhitespace(string $subject)
{
return preg_replace("/\s+\n/", "\n", $subject);
}
/**
* Replace the string "table1.table2:column" with "table2.column"
*
* @param string $subject
*
* @return null|string|string[]
*/
public function removeAdditionalJoins(string $subject)
{
return preg_replace('/(?:[^\s]*[.:])?([^\s]+)[.:]([^\s]*)/u', '$1.$2', $subject);
}
/**
* Match the first file outside of the Fluent source
*
* @param string $subject
* @param ?array $matches
* @param ?string $directory
*
* @return false|int
*/
public function compareLocation(string $subject, &$matches = null, $directory = null)
{
$directory = ($directory === null) ? preg_quote(__DIR__, '/') : preg_quote($directory, '/');
return preg_match('/(^' . $directory . '(\\.php$|[\/\\\\]))/', $subject, $matches);
}
/**
* Match the string "?" or ":param"
*
* @param string $subject
* @param array|null $matches
*
* @return false|int
*/
public function sqlParameter(string $subject, &$matches = null)
{
return preg_match('/(\?|:\w+)/', $subject, $matches);
}
/**
* Match the UTF-8 string "table AS alias"
*
* @param string $subject
* @param array|null $matches
*
* @return false|int
*/
public function tableAlias(string $subject, &$matches = null)
{
if (
preg_match(
'/^\s*(\(.*\))\s+(?:AS\s+)?([' . self::SQLCHARS . ']+)\s+ON\s/uis',
$subject,
$matches
)
)
return 1;
return preg_match(
'/`?([' . self::SQLCHARS . ']+[.:]?[' . self::SQLCHARS . '*]*)`?(\s+AS)?(\s+`?([' . self::SQLCHARS . ']*)`?)?/ui',
$subject,
$matches
);
}
/**
* Match the UTF-8 string "table" or "table."
*
* @param string $subject
* @param array|null $matches
*
* @return false|int
*/
public function tableJoin(string $subject, &$matches = null)
{
return preg_match_all('/([' . self::SQLCHARS . ']+[.:]?)/u', $subject, $matches);
}
/**
* Match the UTF-8 string "table." or "table.column"
*
* @param string $subject
* @param array|null $matches
*
* @return false|int
*/
public function tableJoinFull(string $subject, &$matches = null)
{
return preg_match_all('/([^[:space:]()]+[.:])[' . self::SQLCHARS . ']*/u', $subject, $matches);
}
}