Temperature.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace App\Yr\Forecast\Tabular\Time;
  3. /**
  4. * Temperature
  5. *
  6. * @author Joachim M. Giæver (joachim[]giaever.org)
  7. */
  8. class Temperature extends AbstractUnit implements ConvertableInterface {
  9. const UNIT_CELCIUS = 'celcius';
  10. const UNIT_FAHRENHEIT = 'fahrenheit';
  11. const UNIT_KELVIN = 'kelvin';
  12. /**
  13. * @param \SimpleXMLElement $xml XML containing the temperature
  14. */
  15. public function __construct(?\SimpleXMLElement $xml) {
  16. parent::__construct(
  17. (float)$xml['value'],
  18. (string)$xml['unit']
  19. );
  20. }
  21. /**
  22. * {@inheritDoc}
  23. * @todo support conversion from other types, not just celcius
  24. */
  25. public function convertTo(string $unit): ConvertableInterface {
  26. switch ($unit) {
  27. case self::UNIT_FAHRENHEIT:
  28. return $this->mul(new CustomUnit(
  29. 9/5, $unit
  30. ))->add(new CustomUnit(
  31. 32, $unit
  32. ));
  33. case self::UNIT_KELVIN:
  34. return $this->add(new CustomUnit(
  35. 273.15, $unit
  36. ));
  37. }
  38. }
  39. /**
  40. * Return the ⁰X symbol
  41. */
  42. public function getDegree(): string {
  43. return sprintf("⁰%s", strtoupper(substr($this->getUnit(), 0, 1)));
  44. }
  45. }