WindDirection.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Yr\Forecast\Tabular\Time;
  3. /**
  4. * Wind direction
  5. *
  6. * @author Joachim M. Giæver (joachim[]giaever.org)
  7. */
  8. class WindDirection extends AbstractUnit{
  9. const DEFAULT_VARIANCE = 22.5;
  10. private $name;
  11. /**
  12. * @param \SimpleXMLElement $xml XML element containing the wind direction
  13. */
  14. public function __construct(\SimpleXMLElement $xml) {
  15. parent::__construct(
  16. (float)$xml['deg'],
  17. (string)$xml['code']
  18. );
  19. $this->name = (string)$xml['name'];
  20. }
  21. /**
  22. * Returns the wind direction in full,
  23. * e.g «Northeast».
  24. *
  25. * @return string
  26. */
  27. public function getName(): string {
  28. return $this->name;
  29. }
  30. /**
  31. * Returns a direction on the compass, not
  32. * in degree, but in interger between 1 - 16,
  33. * each explining which spectre.
  34. *
  35. * E.g
  36. * 1: North,
  37. * 2: North-northeast,
  38. * 3: North-east,
  39. * [...]
  40. * 9: South
  41. * etc..
  42. *
  43. * @return int compass spectre
  44. */
  45. public function getDirection(): int {
  46. return (($this->getValue()-11.5) / 22.5);
  47. }
  48. /**
  49. * {@inheritDoc}
  50. */
  51. public function diff(DiffInterface $d): int {
  52. if (parent::diff($d))
  53. return $this->getDirection() - $d->getDirection();
  54. return 0;
  55. }
  56. }