You are here

public function NameGenerator::generateSampleNames in Name Field 8

Service to generate random names.

Parameters

int $limit: The number to generate.

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The field definition if in context.

Return value

array An array of name components.

Overrides NameGeneratorInterface::generateSampleNames

File

src/NameGenerator.php, line 78

Class

NameGenerator
Handles name generation.

Namespace

Drupal\name

Code

public function generateSampleNames($limit = 3, FieldDefinitionInterface $field_definition = NULL) {
  if (!$this->components) {
    $keys = _name_component_keys() + [
      'preferred' => 'preferred',
      'alternative' => 'alternative',
    ];
    $this->components = [
      'female' => array_fill_keys($keys, []),
      'male' => array_fill_keys($keys, []),
    ];

    // Parse genderless configuration.
    $components = $this
      ->loadConfiguration('name.generate.components', 'components', $field_definition);
    foreach ($keys as $key) {
      if (isset($components[$key])) {
        $this->components['female'][$key] = $components[$key];
        $this->components['male'][$key] = $components[$key];
      }
    }

    // Parse gender configuration.
    $components = $this
      ->loadConfiguration('name.generate.components', 'gender', $field_definition);
    foreach ([
      'female',
      'male',
    ] as $gender) {
      foreach ($keys as $key) {
        if (isset($components[$gender][$key])) {
          $this->components[$gender][$key] = array_merge($this->components[$gender][$key], $components[$gender][$key]);
        }
      }
    }
  }
  $preferred = $this
    ->loadConfiguration('name.generate.preferred', 'preferred', $field_definition);
  $names = [];
  for ($i = 0; $i < $limit; $i++) {
    $gender = rand(0, 1) ? 'male' : 'female';
    $name = [
      'title' => '',
      'given' => $this->components[$gender]['given'][array_rand($this->components[$gender]['given'])],
      'middle' => '',
      'family' => $this->components[$gender]['family'][array_rand($this->components[$gender]['family'])],
      'generational' => '',
      'credentials' => '',
    ];
    if (rand(0, 2)) {
      $name['title'] = $this->components[$gender]['title'][array_rand($this->components[$gender]['title'])];
    }
    if (rand(0, 1)) {
      $name['middle'] = $this->components[$gender]['middle'][array_rand($this->components[$gender]['middle'])];
    }
    if (rand(0, 1)) {
      $creds = [];
      $credential_count = count($this->components[$gender]['credentials']);
      $cred_limit = min([
        rand(1, 3),
        $credential_count,
      ]);
      for ($j = 0; $j <= $cred_limit; $j++) {
        $creds[] = $this->components[$gender]['credentials'][array_rand($this->components[$gender]['credentials'])];
      }
      $name['credentials'] = implode(', ', $creds);
    }
    if (!rand(0, 2)) {
      $name['generational'] = $this->components[$gender]['generational'][array_rand($this->components[$gender]['generational'])];
    }

    // All defined names have a preferred alternative, randomise it slightly.
    if (rand(0, 1) && !empty($name['given']) && !empty($preferred[$name['given']])) {
      $name['preferred'] = $preferred[$name['given']];
    }
    $names[] = $name;
  }
  if ($field_definition) {
    $names = $this
      ->filterByFieldSettings($field_definition, $names);
  }
  return $names;
}