WindSpeed.php 1.8 KB

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