Skip to content

Commit

Permalink
learn
Browse files Browse the repository at this point in the history
  • Loading branch information
madou1217 committed Oct 24, 2022
0 parents commit 9f49e9f
Show file tree
Hide file tree
Showing 12 changed files with 220 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
* text=auto

/tests export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.scrutinizer.yml export-ignore
.travis.yml export-ignore
phpunit.php export-ignore
phpunit.xml.dist export-ignore
phpunit.xml export-ignore
.php_cs export-ignore
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.idea
*.DS_Store
/vendor
/coverage
sftp-config.json
composer.lock
.subsplit
.php_cs.cache
.editorconfig
27 changes: 27 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
$header = <<<EOF
This file is part of the madou1217/weather.
(c) Model <madou1217@126.com>
This source file is subject to the MIT license that is bundled.
EOF;

return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules(array(
'@Symfony' => true,
'header_comment' => array('header' => $header),
'array_syntax' => array('syntax' => 'short'),
'ordered_imports' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'php_unit_construct' => true,
'php_unit_strict' => true,
))
->setFinder(
PhpCsFixer\Finder::create()
->exclude('vendor')
->in(__DIR__)
)
;
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<h1 align="center"> weather </h1>

<p align="center"> 高德天气.</p>


## Installing

```shell
$ composer require madou1217/weather -vvv
```

## Usage

TODO

## Contributing

You can contribute in one of three ways:

1. File bug reports using the [issue tracker](https://github.com/madou1217/weather/issues).
2. Answer questions or fix bugs on the [issue tracker](https://github.com/madou1217/weather/issues).
3. Contribute new features or update the wiki.

_The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable._

## License

MIT
30 changes: 30 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "madou1217\/weather",
"description": "高德天气",
"license": "MIT",
"authors": [
{
"name": "Model",
"email": "madou1217@126.com"
}
],
"require": {
"guzzlehttp/guzzle": "^7.5"
},
"autoload": {
"psr-4": {
"Madou1217\\Weather\\": "src"
}
},
"extra": {
"laravel": {
"providers": [
"Madou1217\\Weather\\ServiceProvider"
]
}
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"mockery/mockery": "^1.5"
}
}
21 changes: 21 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>
Empty file added src/.gitkeep
Empty file.
8 changes: 8 additions & 0 deletions src/Exceptions/HttpException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Madou1217\Weather\Exceptions;

class HttpException extends \Exception
{

}
8 changes: 8 additions & 0 deletions src/Exceptions/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Madou1217\Weather\Exceptions;

class InvalidArgumentException extends \Exception
{

}
22 changes: 22 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Madou1217\Weather;

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{
protected $defer = true;

public function register()
{
$this->app->singleton(Weather::class, function () {
return new Weather(config('weathers.amap.key'));
});

$this->app->alias(Weather::class, 'weather');
}

public function provides()
{
return [Weather::class, 'weather'];
}
}
56 changes: 56 additions & 0 deletions src/Weather.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Madou1217\Weather;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use InvalidArgumentException;
use Madou1217\Weather\Exceptions\HttpException;

class Weather
{
private string $base_url;
private Client $client;
/**
* 高德开放平台 应用 Key
* @var string
*/
private string $key;

public function __construct(string $key)
{
$this->base_url = "https://restapi.amap.com";
$this->client = new Client();
$this->key = $key;
}


/**
* @param string $city city_name | city_code
* @param int $type
* @return array
* @throws GuzzleException
* @throws HttpException
*/
public function weatherInfo(string $city, int $type = 1)
{
$url = sprintf("%s/v3/weather/weatherInfo", $this->base_url);

if (!\in_array(\strtolower($type), [1, 2])) {
throw new InvalidArgumentException('Invalid type value(1:base/ 2:all),you are : '.$type);
}
try {
$response = $this->client->get($url, [
'key' => $this->key,
'city' => $city,
'output' => 'json',
'extensions' => $type == 1 ? 'base' : 'all',
]);
return json_decode($response->getBody()->getContents(), true) ?: [];
} catch (\Exception $e) {
throw new HttpException($e->getMessage(), $e->getCode(), $e);
}
}


}
Empty file added tests/.gitkeep
Empty file.

0 comments on commit 9f49e9f

Please sign in to comment.