You are here

public function EventTemplateConverter::convert in MongoDB 8.2

Converts path variables to their corresponding objects.

Parameters

mixed $value: The raw value.

mixed $definition: The parameter definition provided in the route options.

string $name: The name of the parameter.

array $defaults: The route defaults array.

Return value

mixed|null The converted parameter value.

Overrides ParamConverterInterface::convert

File

modules/mongodb_watchdog/src/EventTemplateConverter.php, line 47

Class

EventTemplateConverter
Class EventTemplateConverter load MongoDB watchdog event templates by id.

Namespace

Drupal\mongodb_watchdog

Code

public function convert($value, $definition, $name, array $defaults) : ?EventTemplate {
  if (!is_string($value)) {
    $this->logger
      ->notice('Non-string event template id: %id', [
      '%id' => var_export($value, TRUE),
    ]);
    return NULL;
  }
  $selector = [
    '_id' => $value,
  ];
  $options = [
    'typeMap' => [
      'array' => 'array',
      'document' => 'array',
      'root' => 'Drupal\\mongodb_watchdog\\EventTemplate',
    ],
  ];

  // Returns null if there is no match, as expected by ParamConverter.
  // Never returns an array as findOne() could, because of $options.
  $template = $this->watchdog
    ->templateCollection()
    ->findOne($selector, $options);
  if (empty($template)) {
    $this->logger
      ->notice('Invalid event template id: %id', [
      '%id' => $value,
    ]);
  }
  assert(is_null($template) || $template instanceof EventTemplate);
  return $template;
}