You are here

protected function ThemeService::formatPressure in Weather 8

Same name and namespace in other branches
  1. 2.0.x src/Service/ThemeService.php \Drupal\weather\Service\ThemeService::formatPressure()

Convert pressure.

Parameters

int $pressure: Pressure in hPa.

string $unit: Unit to be returned (for example, inHg, mmHg, hPa, kPa).

Return value

array Formatted representation.

1 call to ThemeService::formatPressure()
ThemeService::setForecastsVariables in src/Service/ThemeService.php
Adds variables related to weather forecast.

File

src/Service/ThemeService.php, line 561

Class

ThemeService
ThemeService service.

Namespace

Drupal\weather\Service

Code

protected function formatPressure($pressure, $unit) {
  if ($unit == 'inhg') {
    $result = $this
      ->t('@pressure inHg', [
      '@pressure' => round($pressure * 0.02953, 2),
    ]);
  }
  elseif ($unit == 'inhg_value') {
    $result = round($pressure * 0.02953, 2);
  }
  elseif ($unit == 'mmhg') {
    $result = $this
      ->t('@pressure mmHg', [
      '@pressure' => round($pressure * 0.7500599999999999, 0),
    ]);
  }
  elseif ($unit == 'mmhg_value') {
    $result = round($pressure * 0.7500599999999999, 0);
  }
  elseif ($unit == 'kpa') {
    $result = $this
      ->t('@pressure kPa', [
      '@pressure' => round($pressure / 10, 1),
    ]);
  }
  elseif ($unit == 'kpa_value') {
    $result = round($pressure / 10, 1);
  }
  elseif ($unit == 'hpa_value') {
    $result = $pressure;
  }
  else {

    // Default to metric units.
    $result = $this
      ->t('@pressure hPa', [
      '@pressure' => $pressure,
    ]);
  }
  return [
    '#markup' => preg_replace("/([^ ]*)&thinsp;([^ ]*)/", '<span style="white-space:nowrap;">\\1&thinsp;\\2</span>', $result),
  ];
}