Skip to content

Commit b0aa2cb

Browse files
committed
Initial commit
0 parents  commit b0aa2cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+8699
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
vendor
2+
.phplint-cache
3+
.phpunit.result.cache
4+
.idea

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.travis.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: php
2+
3+
php:
4+
- 7.1
5+
- 7.2
6+
7+
before_script:
8+
- composer self-update
9+
- composer install
10+
11+
script:
12+
- composer run-script check

LICENCE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Phil Wright- Christie
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Dark Sky API Client
2+
3+
This is a simple client to talk to the Dark Sky API.
4+
5+
To get started, you will need to get a secret key from Dark Sky: https://darksky.net/dev/account.
6+
7+
This package makes use of HTTP adapters to connect to the API. Two are included out of the box,
8+
a Guzzle adapter and a Simple adapter (using `file_get_contents`). If you have specialised
9+
connection needs, simply implement the `ClientAdapterInterface` and pass to the client factory.
10+
11+
This package is also able to make use of a PSR-16 caching adapter to cache calls from the API.
12+
Simply pass a relevant cache service (see https://packagist.org/providers/psr/simple-cache-implementation)
13+
to the client factory to use. No caching is provided out of the box.
14+
15+
Finally, this is able to make use of a PSR-3 logger. Set your logger using `ClientFactory::setLogger($log);`.
16+
17+
This is all shown in the Advanced Usage below.
18+
19+
## Usage
20+
21+
Install the package using composer:
22+
23+
```bash
24+
composer require philwc/dark-sky
25+
```
26+
27+
### Simple Usage
28+
```php
29+
require_once __DIR__ . '/vendor/autoload.php';
30+
31+
$client = philwc\DarkSky\ClientFactory::get(
32+
philwc\DarkSky\ClientFactory::FORECAST,
33+
getenv('SECRET_KEY')
34+
);
35+
36+
$weather = $client->simpleRetrieve(53.4808, 2.2426);
37+
38+
echo $weather->getCurrently()->getSummary() . PHP_EOL;
39+
echo $weather->getCurrently()->getIcon()->toString() . PHP_EOL;
40+
```
41+
42+
### Advanced Usage
43+
```php
44+
require_once __DIR__ . '/vendor/autoload.php';
45+
46+
$log = new Monolog\Logger('test');
47+
$log->pushHandler(new Monolog\Handler\ErrorLogHandler());
48+
49+
philwc\DarkSky\ClientFactory::setLogger($log);
50+
51+
$client = philwc\DarkSky\ClientFactory::get(
52+
philwc\DarkSky\ClientFactory::FORECAST,
53+
getenv('SECRET_KEY'),
54+
new Cache\Adapter\PHPArray\ArrayCachePool(),
55+
new philwc\DarkSky\ClientAdapter\SimpleAdapter()
56+
);
57+
58+
$weather = $client->simpleRetrieve(53.4808, 2.2426);
59+
60+
echo $weather->getCurrently()->getSummary() . PHP_EOL;
61+
echo $weather->getCurrently()->getIcon()->toString() . PHP_EOL;
62+
63+
// This second call will now be retrieved from the cache
64+
$weather = $client->simpleRetrieve(53.4808, 2.2426);
65+
66+
echo $weather->getCurrently()->getSummary() . PHP_EOL;
67+
echo $weather->getCurrently()->getIcon()->toString() . PHP_EOL;
68+
```
69+
70+
In addition to using the `simpleRetrieve` method on the client,
71+
you can use the `retrieve` method. This requires strict typing to validate
72+
your values. Internally, `simpleRetrieve` uses `retrieve`.
73+
74+
```php
75+
...
76+
$weather = $client->retrieve(new Latitude(53.4808), new Longitude(2.2426));
77+
78+
echo $weather->getCurrently()->getSummary() . PHP_EOL;
79+
echo $weather->getCurrently()->getIcon()->toString() . PHP_EOL;
80+
```
81+
82+
Finally, to use the `TimeMachineClient` set the appropriate option on the `ClientFactory`
83+
84+
```php
85+
$client = philwc\DarkSky\ClientFactory::get(
86+
philwc\DarkSky\ClientFactory::TIME_MACHINE,
87+
getenv('SECRET_KEY')
88+
);
89+
```
90+
91+
The calls to `retrieve` and `simpleRetrieve` now require a timestamp:
92+
93+
```php
94+
$weather = $client->simpleRetrieve(53.4808, 2.2426, 1514764800);
95+
$weather = $client->retrieve(new Latitude(53.4808), new Longitude(2.2426), new DateTimeImmutable('2018-01-01 00:00:00'));
96+
```

composer.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "philwc/dark-sky",
3+
"description": "A simple client to talk to the Dark Sky API.",
4+
"license": "MIT",
5+
"type": "library",
6+
"require": {
7+
"php": ">=7.1",
8+
"ext-json": "*",
9+
"php-ds/php-ds": "^1.2",
10+
"beberlei/assert": "^3.0",
11+
"guzzlehttp/psr7": "^1.4",
12+
"psr/simple-cache": "^1.0",
13+
"psr/log": "^1.0"
14+
},
15+
"suggest": {
16+
"guzzlehttp/guzzle": "Allows for better handling of http connections",
17+
"psr/simple-cache-implementation": "Allows for caching results"
18+
},
19+
"require-dev": {
20+
"phpunit/phpunit": "^7.3",
21+
"squizlabs/php_codesniffer": "^3.3",
22+
"guzzlehttp/guzzle": "^6.3",
23+
"cache/array-adapter": "^1.0",
24+
"monolog/monolog": "^1.23",
25+
"overtrue/phplint": "^1.0"
26+
},
27+
"authors": [
28+
{
29+
"name": "Phil Wright- Christie",
30+
"email": "[email protected]"
31+
}
32+
],
33+
"autoload": {
34+
"psr-4": {
35+
"philwc\\DarkSky\\": "src/"
36+
}
37+
},
38+
"scripts": {
39+
"test": "phpunit",
40+
"cs": "phpcs",
41+
"lint": "phplint ./ --exclude=vendor --extensions=php,tpl",
42+
"check": [
43+
"@lint",
44+
"@cs",
45+
"@test"
46+
]
47+
}
48+
}

0 commit comments

Comments
 (0)