Time.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace App\Yr\Forecast\Tabular;
  3. use App\Yr\Forecast\Tabular\Time\Pressure;
  4. use App\Yr\Forecast\Tabular\Time\Symbol;
  5. use App\Yr\Forecast\Tabular\Time\Temperature;
  6. use App\Yr\Forecast\Tabular\Time\WindDirection;
  7. use App\Yr\Forecast\Tabular\Time\WindSpeed;
  8. /**
  9. * Forecast data witin a time period
  10. *
  11. * @author Joachim M. Giæver (joachim[]giaever.org)
  12. */
  13. final class Time implements \IteratorAggregate {
  14. private $from;
  15. private $until;
  16. private $period;
  17. private $symbol;
  18. private $windDirection;
  19. /**
  20. * @param \SimpleXMLElement $xml The xml holding the time-object
  21. */
  22. public function __construct(\SimpleXMLElement $xml) {
  23. $this->from = new \DateTimeImmutable($xml['from']);
  24. $this->until = new \DateTimeImmutable($xml['to']);
  25. $this->period = (int)$xml['period'];
  26. $this->symbol = new Symbol($xml->symbol);
  27. $this->windDirection = new WindDirection($xml->windDirection);
  28. $this->windSpeed= new WindSpeed($xml->windSpeed);
  29. $this->temperature = new Temperature($xml->temperature);
  30. $this->pressure = new Pressure($xml->pressure);
  31. }
  32. /**
  33. * Returns the which index in the forecast
  34. * its for.
  35. */
  36. public function getPeriod(): int {
  37. return $this->period;
  38. }
  39. /**
  40. * Returns the time this forecast is from
  41. *
  42. * @return \DateTimeImmutable
  43. */
  44. public function getFrom(): \DateTimeImmutable {
  45. return $this->from;
  46. }
  47. /**
  48. * Returns the time this forecast is to/until.
  49. *
  50. * @return \DateTimeImmutable
  51. */
  52. public function getUntil(): \DateTimeImmutable {
  53. return $this->until;
  54. }
  55. /**
  56. * Returns the «symbol» (e.g Clody etc)
  57. *
  58. * @return Symbol
  59. */
  60. public function getSymbol(): Symbol {
  61. return $this->symbol;
  62. }
  63. /**
  64. * Returns the wind direction
  65. *
  66. * @return WindDirection
  67. */
  68. public function getWindDirection(): WindDirection {
  69. return $this->windDirection;
  70. }
  71. /**
  72. * Returns the wind speed
  73. *
  74. * @return WindSpeed
  75. */
  76. public function getWindSpeed(): WindSpeed {
  77. return $this->windSpeed;
  78. }
  79. /**
  80. * Returns the temperature
  81. *
  82. * @return Temperature
  83. */
  84. public function getTemperature(): Temperature {
  85. return $this->temperature;
  86. }
  87. /**
  88. * Returns the pressure
  89. *
  90. * @return Pressure
  91. */
  92. public function getPressure(): Pressure {
  93. return $this->pressure;
  94. }
  95. /**
  96. * {@inheritDoc}
  97. */
  98. public function getIterator(): \Generator {
  99. foreach ([
  100. $this->getSymbol(),
  101. $this->getWindDirection(),
  102. $this->getWindSpeed(),
  103. $this->getTemperature(),
  104. $this->getPressure(),
  105. ] as $entity)
  106. yield $entity;
  107. }
  108. public function __toString(): string {
  109. return sprintf(
  110. '%s - %s: %s, %s, %s, %s, %s',
  111. $this->from->format('H:i'), $this->until->format('H:i'),
  112. $this->symbol, $this->windDirection, $this->windSpeed,
  113. $this->temperature, $this->pressure
  114. );
  115. }
  116. }