Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
NameFormatOutput
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
7
100.00% covered (success)
100.00%
1 / 1
 wrap
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\name\Utility;
6
7use Drupal\Component\Render\FormattableMarkup;
8use Drupal\Component\Render\HtmlEscapedText;
9use Drupal\Component\Utility\Xss;
10use Drupal\Core\Render\Markup;
11
12/**
13 * Wraps a formatted name string in the appropriate Drupal render type.
14 *
15 * @internal
16 */
17final class NameFormatOutput {
18
19  /**
20   * Wraps a formatted name string according to the active markup mode.
21   *
22   * - simple / rdfa / microdata: component values are already HTML-escaped by
23   *   NameFormatTokens, so the string is wrapped in FormattableMarkup with no
24   *   additional substitutions.
25   * - raw: the string passes through Xss::filterAdmin() and is marked safe.
26   * - none (default): the string is HTML-escaped via HtmlEscapedText.
27   *
28   * @param string $name_string
29   *   The assembled name string from NameFormatParser::format().
30   * @param string $markup
31   *   The markup mode key.
32   *
33   * @return mixed
34   *   A renderable value (string or MarkupInterface object).
35   */
36  public static function wrap(string $name_string, string $markup): mixed {
37    switch ($markup) {
38      case 'simple':
39      case 'rdfa':
40      case 'microdata':
41        return (new FormattableMarkup($name_string, []))->jsonSerialize();
42
43      case 'raw':
44        return Markup::create(Xss::filterAdmin($name_string));
45
46      case 'none':
47      default:
48        return (new HtmlEscapedText($name_string))->jsonSerialize();
49
50    }
51  }
52
53}