WindDirection.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 strtolower($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. public function thresholdDiff(DiffInterface $e): bool {
  57. return $this->diff($e) != 0;
  58. }
  59. public function __toString(): string {
  60. return sprintf("%s: %s (%01.1f⁰)",
  61. basename(str_replace("\\", DIRECTORY_SEPARATOR, get_class($this))),
  62. $this->getName(), $this->getValue()
  63. );
  64. }
  65. }