protected function NameFormatParser::format in Name Field 8
Formats an array of name components into the supplied format.
Parameters
array $name_components: A keyed array of the components.
string $format: The name format string or segment to parse.
array $tokens: The generated tokens.
Return value
string The formatted string.
1 call to NameFormatParser::format()
- NameFormatParser::parse in src/
NameFormatParser.php - Parses a name component array into the given format.
File
- src/
NameFormatParser.php, line 115
Class
- NameFormatParser
- Converts a name from an array of components into a defined format.
Namespace
Drupal\nameCode
protected function format(array $name_components, $format = '', array $tokens = NULL) {
if (empty($format)) {
return '';
}
if (!isset($tokens)) {
$tokens = $this
->generateTokens($name_components);
}
// Neutralise any escaped backslashes.
$format = str_replace('\\\\', "\t", $format);
$pieces = [];
$modifiers = '';
$conditions = '';
for ($i = 0; $i < strlen($format); $i++) {
$char = $format[$i];
$last_char = $i > 0 ? $format[$i - 1] : FALSE;
// Handle escaped letters.
if ($char == '\\') {
continue;
}
if ($last_char == '\\') {
$pieces[] = $this
->addComponent($char, $modifiers, $conditions);
continue;
}
switch ($char) {
case 'L':
case 'U':
case 'F':
case 'T':
case 'S':
case 'G':
case 'B':
case 'b':
$modifiers .= $char;
break;
case '=':
case '^':
case '|':
case '+':
case '-':
case '~':
$conditions .= $char;
break;
case '(':
case ')':
$remaining_string = substr($format, $i);
if ($char == '(' && ($closing_bracket = $this
->closingBracketPosition($remaining_string))) {
$sub_string = $this
->format($tokens, substr($format, $i + 1, $closing_bracket - 1), $tokens);
// Increment the counter past the closing bracket.
$i += $closing_bracket;
$pieces[] = $this
->addComponent($sub_string, $modifiers, $conditions);
}
else {
// Unmatched, add it.
$pieces[] = $this
->addComponent($char, $modifiers, $conditions);
}
break;
default:
if (array_key_exists($char, $tokens)) {
$char = $tokens[$char];
}
$pieces[] = $this
->addComponent($char, $modifiers, $conditions);
break;
}
}
$parsed_pieces = [];
for ($i = 0; $i < count($pieces); $i++) {
$component = $pieces[$i]['value'];
$conditions = $pieces[$i]['conditions'];
$last_component = $i > 0 ? $pieces[$i - 1]['value'] : FALSE;
$next_component = $i < count($pieces) - 1 ? $pieces[$i + 1]['value'] : FALSE;
if (empty($conditions)) {
$parsed_pieces[$i] = $component;
}
else {
// Modifier: Conditional insertion. Insert if both the surrounding
// tokens are not empty.
if (strpos($conditions, '+') !== FALSE && !empty($last_component) && !empty($next_component)) {
$parsed_pieces[$i] = $component;
}
// Modifier: Conditional insertion. Insert if the previous token is
// not empty.
if (strpos($conditions, '-') !== FALSE && !empty($last_component)) {
$parsed_pieces[$i] = $component;
}
// Modifier: Conditional insertion. Insert if the previous token is
// empty.
if (strpos($conditions, '~') !== FALSE && empty($last_component)) {
$parsed_pieces[$i] = $component;
}
// Modifier: Insert the token if the next token is empty.
if (strpos($conditions, '^') !== FALSE && empty($next_component)) {
$parsed_pieces[$i] = $component;
}
// Modifier: Insert the token if the next token is not empty.
// This overrides the above two settings.
if (strpos($conditions, '=') !== FALSE && !empty($next_component)) {
$parsed_pieces[$i] = $component;
}
// Modifier: Conditional insertion. Uses the previous token unless
// empty, otherwise insert this token.
if (strpos($conditions, '|') !== FALSE) {
if (empty($last_component)) {
$parsed_pieces[$i] = $component;
}
else {
unset($parsed_pieces[$i]);
}
}
}
}
return str_replace('\\\\', "\t", implode('', $parsed_pieces));
}