You are here

public function TokenProcessor::ldapEntryReplacementsForDrupalAccount in Lightweight Directory Access Protocol (LDAP) 8.4

Replace a single token.

Parameters

\Symfony\Component\Ldap\Entry $resource: The resource to act upon.

string $text: The text such as "[dn]", "[cn]@my.org", "[displayName] [sn]", "Drupal Provisioned".

Return value

string|null Replaced string.

See also

\Drupal\ldap_user\EventSubscriber\LdapEntryProvisionSubscriber::fetchDrupalAttributeValue()

File

ldap_servers/src/Processor/TokenProcessor.php, line 86

Class

TokenProcessor
Helper to manage LDAP tokens and process their content.

Namespace

Drupal\ldap_servers\Processor

Code

public function ldapEntryReplacementsForDrupalAccount(Entry $resource, string $text) : string {

  // Reset since service can be reused in multi-user processors.
  $this->tokens = [];

  // Greedy matching of groups of [], ignore spaces and trailing data with /x.
  preg_match_all('/\\[([^\\[\\]]*)\\]/x', $text, $matches);
  if (!isset($matches[1]) || empty($matches[1])) {

    // If no tokens exist in text, return text itself.
    return $text;
  }
  $this
    ->tokenizeLdapEntry($resource, $matches[1]);
  foreach ($matches[0] as $target) {
    if (isset($this->tokens[$target])) {
      $text = str_replace($target, $this->tokens[$target], $text);
    }
  }

  // Strip out any un-replaced tokens.
  return preg_replace('/\\[.*\\]/', '', $text);
}