You are here

public function NameFormatParser::parse in Name Field 8

Parses a name component array into the given format.

Parameters

array $name_components: Keyed array of name components.

string $format: The name format pattern to generate the name.

array $settings: Additional settings to control the parser.

  • sep1 (string): first separator.
  • sep2 (string): second separator.
  • sep3 (string): third separator.
  • markup (string): key of the markup type.
  • boundary (string): regexp for word boundary.

Return value

\Drupal\Component\Render\MarkupInterface A renderable object representing the name.

File

src/NameFormatParser.php, line 71

Class

NameFormatParser
Converts a name from an array of components into a defined format.

Namespace

Drupal\name

Code

public function parse(array $name_components, $format = '', array $settings = []) {
  foreach ([
    'sep1',
    'sep2',
    'sep3',
  ] as $sep_key) {
    if (isset($settings[$sep_key])) {
      $this->{$sep_key} = (string) $settings[$sep_key];
    }
  }
  $this->markup = !empty($settings['markup']) ? $settings['markup'] : 'none';
  if (isset($settings['boundary']) && strlen($settings['boundary'])) {
    $this->boundary = $settings['boundary'];
  }
  $name_string = $this
    ->format($name_components, $format);
  switch ($this->markup) {

    // Component values are already escaped.
    case 'simple':
    case 'rdfa':
    case 'microdata':
      return new FormattableMarkup($name_string, []);

    // Unescaped text.
    case 'raw':
      return $name_string;

    // Raw component values.
    case 'none':
    default:
      return new HtmlEscapedText($name_string);
  }
}