Temperature.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. public function thresholdDiff(DiffInterface $e): bool {
  40. return abs($this->diff($e)/100) > 1;
  41. }
  42. /**
  43. * Return the ⁰X symbol
  44. */
  45. public function getDegree(): string {
  46. return sprintf("⁰%s", strtoupper(substr($this->getUnit(), 0, 1)));
  47. }
  48. public function __toString(): string {
  49. return sprintf(
  50. "%s: %d %s",
  51. basename(str_replace("\\", DIRECTORY_SEPARATOR, get_class($this))),
  52. $this->getValue(),
  53. $this->getDegree()
  54. );
  55. }
  56. }