You are here

protected function SensorResult::getFormattedValue in Monitoring 7

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

Formats the value to be human readable.

Return value

string Formatted value.

Throws

\Drupal\monitoring\Sensor\SensorCompilationException

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

File

lib/Drupal/monitoring/Result/SensorResult.php, line 293
Contains \Drupal\monitoring\Result\SensorResult.

Class

SensorResult
Generic container for the sensor result.

Namespace

Drupal\monitoring\Result

Code

protected function getFormattedValue() {

  // If the value type is defined we have the formatter that will format the
  // value to be ready for display.
  if ($value_type = $this
    ->getSensorInfo()
    ->getValueType()) {
    $value_types = monitoring_value_types();
    if (!isset($value_types[$value_type])) {
      throw new SensorCompilationException(format_string('Invalid value type @type', array(
        '@type' => $value_type,
      )));
    }
    elseif (isset($value_types[$value_type]['formatter_callback']) && !function_exists($value_types[$value_type]['formatter_callback'])) {
      throw new SensorCompilationException(format_string('Formatter callback @callback for @type does not exist', array(
        '@callback' => $value_types[$value_type]['formatter_callback'],
        '@type' => $value_type,
      )));
    }
    else {
      $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
    ->getSensorInfo()
    ->getValueLabel()) {

    // @todo This assumption will no longer work when non-english messages
    // supported.
    $label = drupal_strtolower($label);
    return format_string('!value !label', array(
      '!value' => $this
        ->getValue(),
      '!label' => $label,
    ));
  }
  return format_string('Value !value', array(
    '!value' => $this
      ->getValue(),
  ));
}