WindSpeed.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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): self {
  30. switch ($unit) {
  31. case self::UNIT_KNOTS:
  32. return $this->mul(new CustomUnit(1.9438445, $unit))->setName($this->getName());
  33. case self::UNIT_FTS:
  34. return $this->mul(new CustomUnit(3.28084, $unit))->setName($this->getName());
  35. case self::UNIT_KMH:
  36. return $this->mul(new CustomUnit(3.6, $unit))->setName($this->getName());
  37. }
  38. }
  39. /**
  40. * Returns the wind name, e.g «light breeze»
  41. */
  42. public function getName(): string {
  43. return $this->name;
  44. }
  45. protected function setName(string $name): self {
  46. $this->name = $name;
  47. return $this;
  48. }
  49. /**
  50. * {@inheritDoc}
  51. */
  52. public function diff(DiffInterface $e): int {
  53. if (parent::diff($e))
  54. return ($this->getName() != $e->getName()) ? 1 : 0;
  55. return 0;
  56. }
  57. public function __toString(): string {
  58. return sprintf(
  59. '%s (%s)', parent::__toString(), $this->name
  60. );
  61. }
  62. }