|
19 | 19 |
|
20 | 20 | use Cmfcmf\OpenWeatherMap\AbstractCache; |
21 | 21 | use Cmfcmf\OpenWeatherMap\CurrentWeather; |
| 22 | +use Cmfcmf\OpenWeatherMap\CurrentUvi; |
22 | 23 | use Cmfcmf\OpenWeatherMap\CurrentWeatherGroup; |
23 | 24 | use Cmfcmf\OpenWeatherMap\Exception as OWMException; |
24 | 25 | use Cmfcmf\OpenWeatherMap\Fetcher\CurlFetcher; |
@@ -67,6 +68,16 @@ class OpenWeatherMap |
67 | 68 | */ |
68 | 69 | private $weatherHistoryUrl = 'http://history.openweathermap.org/data/2.5/history/city?'; |
69 | 70 |
|
| 71 | + /** |
| 72 | + * @var string The basic api url to fetch current uv data from. |
| 73 | + */ |
| 74 | + private $uviUrl = 'http://api.openweathermap.org/v3/uvi/%s,%s/current.json?'; |
| 75 | + |
| 76 | + /** |
| 77 | + * @var string The basic api url to fetch current uv data from. |
| 78 | + */ |
| 79 | + private $uviHistoryUrl = 'http://api.openweathermap.org/v3/uvi/%s,%s/%s.json?'; |
| 80 | + |
70 | 81 | /** |
71 | 82 | * @var AbstractCache|bool $cache The cache to use. |
72 | 83 | */ |
@@ -436,6 +447,109 @@ public function getRawWeatherHistory($query, \DateTime $start, $endOrCount = 1, |
436 | 447 | return $this->cacheOrFetchResult($url); |
437 | 448 | } |
438 | 449 |
|
| 450 | + /** |
| 451 | + * Directly returns the json string returned by OpenWeatherMap for the UVI data. |
| 452 | + * |
| 453 | + * @param array $query The place to get information as follows: [latitude, longitude, date time]. For possible values see ::getWeather. |
| 454 | + * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details. |
| 455 | + * |
| 456 | + * @throws \InvalidArgumentException |
| 457 | + * |
| 458 | + * @return string Returns false on failure and the fetched data in the format you specified on success. |
| 459 | + * |
| 460 | + * Warning If an error occurred, OpenWeatherMap ALWAYS returns data in json format. |
| 461 | + * |
| 462 | + * @api |
| 463 | + */ |
| 464 | + public function getRawUviData($query, $appid = '') |
| 465 | + { |
| 466 | + if (!is_array($query)) { |
| 467 | + throw new \InvalidArgumentException('$query must get information is as follows: [latitude, longitude]'); |
| 468 | + } elseif(count($query) != 2) { |
| 469 | + throw new \InvalidArgumentException('$query must get information is as follows: [latitude, longitude]'); |
| 470 | + } else { |
| 471 | + $url = $this->buildUviUrl($query, $appid); |
| 472 | + } |
| 473 | + |
| 474 | + return $this->cacheOrFetchResult($url); |
| 475 | + } |
| 476 | + |
| 477 | + /** |
| 478 | + * Directly returns the json string returned by OpenWeatherMap for the UVI history data. |
| 479 | + * |
| 480 | + * @param array|int|string $query The place to get weather information for. For possible values see ::getWeather. |
| 481 | + * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details. |
| 482 | + * |
| 483 | + * @throws \InvalidArgumentException |
| 484 | + * |
| 485 | + * @return string Returns false on failure and the fetched data in the format you specified on success. |
| 486 | + * |
| 487 | + * Warning If an error occurred, OpenWeatherMap ALWAYS returns data in json format. |
| 488 | + * |
| 489 | + * @api |
| 490 | + */ |
| 491 | + public function getRawUviHistory($query, $appid = '') |
| 492 | + { |
| 493 | + if (!is_array($query)) { |
| 494 | + throw new \InvalidArgumentException('$query must get information is as follows: [latitude, longitude, ISO 8601 date format]'); |
| 495 | + } elseif(count($query) != 3) { |
| 496 | + throw new \InvalidArgumentException('$query must get information is as follows: [latitude, longitude, ISO 8601 date format]'); |
| 497 | + } else { |
| 498 | + $url = $this->buildUviUrl($query, $appid); |
| 499 | + } |
| 500 | + |
| 501 | + return $this->cacheOrFetchResult($url); |
| 502 | + } |
| 503 | + |
| 504 | + /** |
| 505 | + * Returns the current uvi at the location you specified. |
| 506 | + * |
| 507 | + * @param array|int|string $query The place to get weather information for. For possible values see below. |
| 508 | + * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details. |
| 509 | + * |
| 510 | + * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error. |
| 511 | + * @throws \InvalidArgumentException If an argument error occurs. |
| 512 | + * |
| 513 | + * @return CurrentUvi The uvi object. |
| 514 | + * |
| 515 | + * There are three ways to specify the place to get weather information for: |
| 516 | + * - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values. |
| 517 | + * |
| 518 | + * @api |
| 519 | + */ |
| 520 | + public function getUvi($query, $appid = '') |
| 521 | + { |
| 522 | + $answer = $this->getRawUviData($query, $appid); |
| 523 | + $json = $this->parseJson($answer); |
| 524 | + |
| 525 | + return new CurrentUvi($json); |
| 526 | + } |
| 527 | + |
| 528 | + /** |
| 529 | + * Returns the history uvi at the location you specified. |
| 530 | + * |
| 531 | + * @param array|int|string $query The place to get weather information for. For possible values see below. |
| 532 | + * @param string $appid Your app id, default ''. See http://openweathermap.org/appid for more details. |
| 533 | + * @param string $dateTime Your date time, default ''. See http://openweathermap.org/api/uvi for more details about date format. |
| 534 | + * |
| 535 | + * @throws OpenWeatherMap\Exception If OpenWeatherMap returns an error. |
| 536 | + * @throws \InvalidArgumentException If an argument error occurs. |
| 537 | + * |
| 538 | + * @return CurrentUvi The uvi object. |
| 539 | + * |
| 540 | + * There are three ways to specify the place to get weather information for: |
| 541 | + * - Use the coordinates: $query must be an associative array containing the 'lat' and 'lon' values. |
| 542 | + * |
| 543 | + * @api |
| 544 | + */ |
| 545 | + public function getUviHistory($query, $appid = '') |
| 546 | + { |
| 547 | + $answer = $this->getRawUviHistory($query, $appid); |
| 548 | + $json = $this->parseJson($answer); |
| 549 | + |
| 550 | + return new CurrentUvi($json); |
| 551 | + } |
| 552 | + |
439 | 553 | /** |
440 | 554 | * Returns whether or not the last result was fetched from the cache. |
441 | 555 | * |
@@ -503,6 +617,34 @@ private function buildUrl($query, $units, $lang, $appid, $mode, $url) |
503 | 617 | return $url; |
504 | 618 | } |
505 | 619 |
|
| 620 | + /** |
| 621 | + * Build the url to fetch UVI data from. |
| 622 | + * |
| 623 | + * @param $query |
| 624 | + * @param $units |
| 625 | + * @param $lang |
| 626 | + * @param $appid |
| 627 | + * @param $mode |
| 628 | + * @param string $url The url to prepend. |
| 629 | + * |
| 630 | + * @return bool|string The fetched url, false on failure. |
| 631 | + */ |
| 632 | + private function buildUviUrl($query, $appid) |
| 633 | + { |
| 634 | + $queryLength = count($query); |
| 635 | + switch ($queryLength) { |
| 636 | + case 2: |
| 637 | + $queryUrl = sprintf($this->uviUrl, $query[0], $query[1]); |
| 638 | + break; |
| 639 | + case 3: |
| 640 | + $queryUrl = sprintf($this->uviHistoryUrl, $query[0], $query[1], $query[2]); |
| 641 | + break; |
| 642 | + } |
| 643 | + $queryUrl .= 'APPID='; |
| 644 | + |
| 645 | + return $queryUrl .= empty($appid) ? $this->apiKey : $appid; |
| 646 | + } |
| 647 | + |
506 | 648 | /** |
507 | 649 | * Builds the query string for the url. |
508 | 650 | * |
|
0 commit comments