WindSpeed.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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): ConvertableInterface {
  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 strtolower($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. public function thresholdDiff(DiffInterface $e): bool {
  52. return $this->getName() != $e->getName();
  53. }
  54. public function __toString(): string {
  55. return sprintf(
  56. '%s: %s (%01.1f %s)',
  57. basename(str_replace('\\', DIRECTORY_SEPARATOR, get_class($this))),
  58. $this->getName(),
  59. $this->getValue(),
  60. $this->getUnit()
  61. );
  62. }
  63. }