WindSpeed.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App\Yr\Forecast\Tabular\Time;
  3. /**
  4. * Wind speed
  5. *
  6. * @author Joachim M. Giæver (joachim[]giaever.org)
  7. */
  8. class WindSpeed extends AbstractUnit {
  9. const DEFAULT_VARIANCE = (4.9 / 3.5);
  10. const UNIT_MPS = 'mp/s';
  11. const UNIT_FTS = 'ft/s';
  12. const UNIT_KMH = 'km/h';
  13. const UNIT_KNOTS = 'knots';
  14. private $name;
  15. /**
  16. * @param \SimpleXMLElement $xml XML containing the wind spedd
  17. */
  18. public function __construct(\SimpleXMLElement $xml){
  19. parent::__construct(
  20. (float)$xml['mps'], self::UNIT_MPS
  21. );
  22. $this->name = (string)$xml['name'];
  23. }
  24. /**
  25. * Convert the wind speed to a different unit
  26. *
  27. * @param string $unit The unit to convert to, eg UNIT_FTS
  28. */
  29. public function convertTo(string $unit): int {
  30. switch ($unit) {
  31. case self::UNIT_KNOTS:
  32. return $this->getValue() * 1.9438445;
  33. case self::UNIT_FTS:
  34. return $this->getValue() * 3.28084;
  35. case self::UNIT_KMH:
  36. return $this->getValue() * 3.6;
  37. }
  38. }
  39. /**
  40. * Returns the wind name, e.g «light breeze»
  41. */
  42. public function getName(): string {
  43. return $this->name;
  44. }
  45. /**
  46. * {@inheritDoc}
  47. */
  48. public function diff(DiffInterface $e): int {
  49. if (parent::diff($e))
  50. return ($this->getName() != $e->getName()) ? 1 : 0;
  51. return 0;
  52. }
  53. public function __toString(): string {
  54. return sprintf(
  55. '%s (%s)', parent::__toString(), $this->name
  56. );
  57. }
  58. }