Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
FormatOptionService
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
5 / 5
15
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCustomFormatOptions
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getCustomListFormatOptions
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 normalizeLegacyFormatMachineName
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
8
 getFormatPatternByMachineName
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\name\Service;
6
7use Drupal\Core\Entity\EntityTypeManagerInterface;
8
9/**
10 * Options and patterns from name_format and name_list_format config entities.
11 *
12 * @internal
13 */
14class FormatOptionService implements FormatOptionInterface {
15
16  public function __construct(
17    private readonly EntityTypeManagerInterface $entityTypeManager,
18  ) {}
19
20  /**
21   * Returns sort options for custom name_format entities.
22   *
23   * @return array<string, string>
24   *   Machine name keyed labels.
25   */
26  public function getCustomFormatOptions(): array {
27    $options = [];
28    foreach ($this->entityTypeManager->getStorage('name_format')->loadMultiple() as $format) {
29      $options[$format->id()] = $format->label();
30    }
31    natcasesort($options);
32    return $options;
33  }
34
35  /**
36   * Returns sort options for custom name_list_format entities.
37   *
38   * @return array<string, string>
39   *   Machine name keyed labels.
40   */
41  public function getCustomListFormatOptions(): array {
42    $options = [];
43    foreach ($this->entityTypeManager->getStorage('name_list_format')->loadMultiple() as $format) {
44      $options[$format->id()] = $format->label();
45    }
46    natcasesort($options);
47    return $options;
48  }
49
50  /**
51   * Coerces legacy / untyped format ids for config entity load().
52   *
53   * For deprecated helpers and theme preprocess only. Accepts scalars and
54   * \\Stringable that stringify to non-empty text; otherwise NULL.
55   *
56   * @param mixed $raw
57   *   A machine name from legacy callers or theme variables.
58   *
59   * @return string|null
60   *   Non-empty machine name, or NULL when load() should not run.
61   */
62  public static function normalizeLegacyFormatMachineName(mixed $raw): ?string {
63    if ($raw === NULL) {
64      return NULL;
65    }
66    $is_invalid_type = (is_array($raw) || is_resource($raw));
67    if ($is_invalid_type) {
68      return NULL;
69    }
70    if (is_object($raw)) {
71      if (!$raw instanceof \Stringable) {
72        return NULL;
73      }
74      return ((string) $raw) === '' ? NULL : (string) $raw;
75    }
76
77    $candidate = (string) $raw;
78
79    return $candidate === '' ? NULL : $candidate;
80  }
81
82  /**
83   * Loads a format pattern by machine name.
84   */
85  public function getFormatPatternByMachineName(string $machine_name): ?string {
86    $entity = $this->entityTypeManager->getStorage('name_format')->load($machine_name);
87    if ($entity) {
88      return $entity->get('pattern');
89    }
90
91    return NULL;
92  }
93
94}