protected function AccountRegistration::compileFormRegex in SMS Framework 8
Same name and namespace in other branches
- 2.x modules/sms_user/src/AccountRegistration.php \Drupal\sms_user\AccountRegistration::compileFormRegex()
- 2.1.x modules/sms_user/src/AccountRegistration.php \Drupal\sms_user\AccountRegistration::compileFormRegex()
Compile incoming form configuration to a regular expression.
Parameters
string $form_string: A incoming form configuration message.
string $delimiter: The delimiter to escape, as used by preg_match*().
Return value
string A regular expression.
1 call to AccountRegistration::compileFormRegex()
- AccountRegistration::incomingPatternMessage in modules/
sms_user/ src/ AccountRegistration.php - Creates a user if an incoming message contents matches a pattern.
File
- modules/
sms_user/ src/ AccountRegistration.php, line 272
Class
- AccountRegistration
- Defines the account registration service.
Namespace
Drupal\sms_userCode
protected function compileFormRegex($form_string, $delimiter) {
$placeholders = [
'username' => '.+',
'email' => '\\S+',
'password' => '.+',
];
// Placeholders enclosed in square brackets and escaped for use in regular
// expressions. \[user\] , \[email\] etc.
$regex_placeholders = [];
foreach (array_keys($placeholders) as $d) {
$regex_placeholders[] = preg_quote('[' . $d . ']');
}
// Split message so placeholders are separated from other text.
// e.g. for 'U [username] P [password], splits to:
// 'U ', '[username]', ' P ', '[password]'.
$regex = '/(' . implode('|', $regex_placeholders) . '+)/';
$words = preg_split($regex, $form_string, NULL, PREG_SPLIT_DELIM_CAPTURE);
// Track if a placeholder was used, so subsequent usages create a named back
// reference. This allows you to use placeholders more than once as a form
// of confirmation. e.g: 'U [username] P [password] [password]'.
$placeholder_usage = [];
$compiled = '';
foreach ($words as $word) {
// Remove square brackets from word to determine if it is a placeholder.
$placeholder = mb_substr($word, 1, -1);
// Determine if word is a placeholder.
if (isset($placeholders[$placeholder])) {
$placeholder_regex = $placeholders[$placeholder];
if (!in_array($placeholder, $placeholder_usage)) {
// Convert placeholder to a capture group.
$compiled .= '(?<' . $placeholder . '>' . $placeholder_regex . ')';
$placeholder_usage[] = $placeholder;
}
else {
// Create a back reference to the previous named capture group.
$compiled .= '\\k{' . $placeholder . '}';
}
}
else {
// Text is not a placeholder, do not convert to a capture group.
$compiled .= preg_quote($word, $delimiter);
}
}
return $compiled;
}