You are here

class EventTemplate in MongoDB 8.2

Class EventTemplate models an event template.

Templates are the invariant part of events.

Since this is essentially a value object, naming is constrained by the property names in MongoDB, so ignore variable naming rules for fields.

Plugin annotation

@SuppressWarnings(PHPMD . CamelCasePropertyName);

Hierarchy

Expanded class hierarchy of EventTemplate

2 files declare their use of EventTemplate
DetailController.php in modules/mongodb_watchdog/src/Controller/DetailController.php
OverviewController.php in modules/mongodb_watchdog/src/Controller/OverviewController.php

File

modules/mongodb_watchdog/src/EventTemplate.php, line 22

Namespace

Drupal\mongodb_watchdog
View source
class EventTemplate implements Unserializable {
  use StringTranslationTrait;

  // @codingStandardsIgnoreStart

  /**
   * The event identifier.
   *
   * Coding standards are suspended to avoid a warning on _id, which is not
   * standards-compliant, but required by MongoDB.
   *
   * @var string
   */
  public $_id;

  // @codingStandardsIgnoreEnd

  /**
   * Latest event insertion for the template.
   *
   * @var int
   */
  public $changed;

  /**
   * Event count for the template.
   *
   * @var int
   */
  public $count;

  /**
   * The message "type": a Drupal logger "channel".
   *
   * @var string
   */
  public $type;

  /**
   * The event template message with placeholders, not substituted.
   *
   * @var string
   */
  public $message;

  /**
   * The RFC 5424 severity level.
   *
   * @var int
   */
  public $severity;

  /**
   * EventTemplate constructor.
   *
   * @param array $data
   *   The raw properties.
   */
  public function __construct(array $data) {
    $this
      ->bsonUnserialize($data);
  }

  /**
   * List the template keys and their behaviours.
   *
   * @return array[string]
   *   A properties by key array.
   */
  public static function keys() : array {
    $ret = [
      '_id' => [
        'label' => t('ID'),
      ],
      'changed' => [
        'label' => t('Changed'),
        'creation_callback' => 'intval',
      ],
      'count' => [
        'label' => t('Count'),
        'creation_callback' => 'intval',
      ],
      'type' => [
        'label' => t('Type'),
      ],
      'message' => [
        'label' => t('Message'),
      ],
      'severity' => [
        'label' => t('Severity'),
        'creation_callback' => 'intval',
        'display_callback' => function ($level) {
          return RfcLogLevel::getLevels()[$level];
        },
      ],
    ];
    return $ret;
  }

  /**
   * {@inheritdoc}
   */
  public function bsonUnserialize(array $data) : void {
    foreach (static::keys() as $key => $info) {
      $datum = $data[$key] ?? NULL;
      $this->{$key} = isset($info['creation_callback']) ? $info['creation_callback']($datum) : $datum;
    }
    if (!is_string($this->message)) {
      $this->message = print_r($this->message, TRUE);
    }
  }

  /**
   * Returns the message with its variables substituted into it.
   *
   * This code passes a variable to $this->t() because the "variable" ultimately
   * comes from a module code, not from user input. This assumes modules
   * correctly pass only template messages to PSR-3 methods, instead of already
   * assembled messages.
   *
   * XXX babysit broken modules using messages containing user input.
   *
   * This code disables coding standards because the module relies on translated
   * event templates, which are known to be variable but - assuming no coding
   * errors - will always match a constant event template string found in code.
   *
   * @param array $variables
   *   The event variables.
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
   *   The template message with its variables substituted.
   */
  public function asString(array $variables) : TranslatableMarkup {

    // @codingStandardsIgnoreStart
    return $this
      ->t($this->message, $variables);

    // @codingStandardsIgnoreEnd
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EventTemplate::$changed public property Latest event insertion for the template.
EventTemplate::$count public property Event count for the template.
EventTemplate::$message public property The event template message with placeholders, not substituted.
EventTemplate::$severity public property The RFC 5424 severity level.
EventTemplate::$type public property The message "type": a Drupal logger "channel".
EventTemplate::$_id public property The event identifier.
EventTemplate::asString public function Returns the message with its variables substituted into it.
EventTemplate::bsonUnserialize public function
EventTemplate::keys public static function List the template keys and their behaviours.
EventTemplate::__construct public function EventTemplate constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.