public function TokenProcessor::tokenReplace in Lightweight Directory Access Protocol (LDAP) 8.3
Replace a token.
Parameters
array|UserInterface $resource: The resource to act upon.
string $text: The text such as "[dn]", "[cn]@my.org", "[displayName] [sn]", "Drupal Provisioned".
string $resource_type: What kind of type to replace.
Return value
string The text with tokens replaced or NULL if replacement not available.
File
- ldap_servers/
src/ Processor/ TokenProcessor.php, line 48
Class
- TokenProcessor
- Helper to manage LDAP tokens and process their content.
Namespace
Drupal\ldap_servers\ProcessorCode
public function tokenReplace($resource, $text, $resource_type = 'ldap_entry') {
// Desired tokens are of form "cn","mail", etc.
$desired_tokens = ConversionHelper::findTokensNeededForTemplate($text);
if (empty($desired_tokens)) {
// If no tokens exist in text, return text itself. It is literal value.
return $text;
}
$tokens = [];
switch ($resource_type) {
case 'ldap_entry':
$tokens = $this
->tokenizeLdapEntry($resource, $desired_tokens, self::PREFIX, self::SUFFIX);
break;
case 'user_account':
$tokens = $this
->tokenizeUserAccount($resource, $desired_tokens, self::PREFIX, self::SUFFIX);
break;
}
// Add lowercase tokens to avoid case sensitivity.
foreach ($tokens as $attribute => $value) {
$tokens[mb_strtolower($attribute)] = $value;
}
// Array of attributes (sn, givenname, etc)
$attributes = array_keys($tokens);
// Array of attribute values (Lincoln, Abe, etc)
$values = array_values($tokens);
// TODO: This comparison is likely not ideal:
// The sub-functions redundantly lowercase replacements in addition to the
// source formatting. Otherwise comparison would fail here in
// case-insensitive requests. Ideally, a reimplementation would resolve this
// redundant and inconsistent approach with a clearer API.
$result = str_replace($attributes, $values, $text);
// Strip out any unreplace tokens.
$result = preg_replace('/^\\[.*\\]$/', '', $result);
// Return NULL if $result is empty, else $result.
if ($result == '') {
return NULL;
}
else {
return $result;
}
}