You are here

public function SensorResult::getFormattedValue in Monitoring 8

Same name and namespace in other branches
  1. 7 lib/Drupal/monitoring/Result/SensorResult.php \Drupal\monitoring\Result\SensorResult::getFormattedValue()

Formats the value to be human readable.

Parameters

mixed $value: Sensor result value.

Return value

string Formatted value.

Throws

\Drupal\monitoring\Sensor\SensorCompilationException

Overrides SensorResultInterface::getFormattedValue

1 call to SensorResult::getFormattedValue()
SensorResult::compile in src/Result/SensorResult.php
Compiles added status messages sets the status.

File

src/Result/SensorResult.php, line 319

Class

SensorResult
Generic container for the sensor result.

Namespace

Drupal\monitoring\Result

Code

public function getFormattedValue($value) {
  $value_type = $this
    ->getSensorConfig()
    ->getValueType();

  // If the value type is defined we have the formatter that will format the
  // value to be ready for display.
  if (!empty($value_type)) {
    $value_types = monitoring_value_types();
    if (!isset($value_types[$value_type])) {
      throw new SensorCompilationException(new FormattableMarkup('Invalid value type @type', array(
        '@type' => $value_type,
      )));
    }
    elseif (empty($value_types[$value_type]['formatter_callback']) && ($label = $this
      ->getSensorConfig()
      ->getValueLabel())) {
      $label = mb_strtolower($label);
      return new FormattableMarkup('@value @label', array(
        '@value' => $value,
        '@label' => $label,
      ));
    }
    elseif (isset($value_types[$value_type]['formatter_callback']) && !function_exists($value_types[$value_type]['formatter_callback'])) {
      throw new SensorCompilationException(new FormattableMarkup('Formatter callback @callback for @type does not exist', array(
        '@callback' => $value_types[$value_type]['formatter_callback'],
        '@type' => $value_type,
      )));
    }
    elseif (isset($value_types[$value_type]['formatter_callback'])) {
      $callback = $value_types[$value_type]['formatter_callback'];
      return $callback($this);
    }
  }

  // If there is no value formatter we try to provide something human readable
  // by concatenating the value and label.
  if ($label = $this
    ->getSensorConfig()
    ->getValueLabel()) {

    // @todo This assumption will no longer work when non-english messages
    // supported.
    $label = mb_strtolower($label);
    return new FormattableMarkup('@value @label', array(
      '@value' => $value,
      '@label' => $label,
    ));
  }
  return new FormattableMarkup('Value @value', array(
    '@value' => $value,
  ));
}