TestFetcher.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * OpenWeatherMap-PHP-API — A php api to parse weather data from http://www.OpenWeatherMap.org .
  4. *
  5. * @license MIT
  6. *
  7. * Please see the LICENSE file distributed with this source code for further
  8. * information regarding copyright and licensing.
  9. *
  10. * Please visit the following links to read about the usage policies and the license of
  11. * OpenWeatherMap before using this class:
  12. *
  13. * @see http://www.OpenWeatherMap.org
  14. * @see http://www.OpenWeatherMap.org/terms
  15. * @see http://openweathermap.org/appid
  16. */
  17. namespace Cmfcmf\OpenWeatherMap\Tests;
  18. use Cmfcmf\OpenWeatherMap\Fetcher\FetcherInterface;
  19. class TestFetcher implements FetcherInterface
  20. {
  21. /**
  22. * Fetch contents from the specified url.
  23. *
  24. * @param string $url The url to be fetched.
  25. *
  26. * @return string The fetched content.
  27. *
  28. * @api
  29. */
  30. public function fetch($url)
  31. {
  32. $format = strpos($url, 'json') !== false ? 'json' : 'xml';
  33. if (strpos($url, 'forecast') !== false) {
  34. return $this->forecast($format);
  35. } elseif (strpos($url, 'group') !== false) {
  36. return $this->group($format);
  37. } else {
  38. return $this->currentWeather($format);
  39. }
  40. }
  41. private function currentWeather($format)
  42. {
  43. if ($format == 'xml') {
  44. return FakeData::CURRENT_WEATHER_XML;
  45. }
  46. }
  47. private function forecast($format)
  48. {
  49. if ($format == 'xml') {
  50. return FakeData::forecastXML();
  51. }
  52. }
  53. private function group($format)
  54. {
  55. if ($format == 'json') {
  56. return FakeData::WEATHER_GROUP_JSON;
  57. }
  58. }
  59. }