Skip to content

Commit f434e63

Browse files
authored
Merge pull request #1 from zhooravell/develop
Develop
2 parents a561541 + 83e189e commit f434e63

24 files changed

+1345
-4
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/vendor
22
composer.lock
33
/coverage
4-
/phpmetrics
4+
/phpmetrics
5+
/example

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,46 @@
11
# Monobank open API client
22
PHP client for Monobank open API
33

4+
[![License][license-image]][license-link] [![Build Status][travis-image]][travis-link] [![codecov][codecov-image]][codecov-link] [![scrutinizer][scrutinizer-image]][scrutinizer-link] [![intelligence][intelligence-image]][intelligence-link]
5+
46
![](https://api.monobank.ua/docs/images/logo.png)
57

8+
## Examples
9+
10+
```php
11+
<?php
12+
13+
include 'vendor/autoload.php';
14+
15+
use GuzzleHttp\Client;
16+
use Monobank\MonobankClient;
17+
use Monobank\ValueObject\Token;
18+
19+
$httpClient = new Client();
20+
$token = new Token('...');
21+
$client = new MonobankClient($httpClient, $token);
22+
23+
var_dump($client->getExchangeRates());
24+
25+
var_dump($client->getClientInfo());
26+
27+
var_dump($client->getPersonalStatement(new PersonalStatementRequest(new DateTime('2019-06-20'), new DateTime())));
28+
```
629
## Source(s)
730

831
* [Monobank API docs](https://api.monobank.ua/docs/)
32+
33+
[license-link]: https://github.com/zhooravell/php-monobank/blob/master/LICENSE
34+
[license-image]: https://img.shields.io/dub/l/vibe-d.svg
35+
36+
[travis-link]: https://travis-ci.com/zhooravell/php-monobank
37+
[travis-image]: https://travis-ci.com/zhooravell/php-monobank.svg?branch=master
38+
39+
[codecov-link]: https://codecov.io/gh/zhooravell/php-monobank
40+
[codecov-image]: https://codecov.io/gh/zhooravell/php-monobank/branch/master/graph/badge.svg
41+
42+
[scrutinizer-link]: https://scrutinizer-ci.com/g/zhooravell/php-monobank/?branch=master
43+
[scrutinizer-image]: https://scrutinizer-ci.com/g/zhooravell/php-monobank/badges/quality-score.png?b=master
44+
45+
[intelligence-link]: https://scrutinizer-ci.com/code-intelligence
46+
[intelligence-image]: https://scrutinizer-ci.com/g/zhooravell/php-monobank/badges/code-intelligence.svg?b=master

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
],
1212
"require": {
1313
"php": ">=7.1",
14-
"ext-mbstring": "*",
15-
"ext-json": "*",
1614
"ext-filter": "*",
15+
"ext-json": "*",
16+
"ext-mbstring": "*",
1717
"guzzlehttp/guzzle": "^6.3"
1818
},
1919
"require-dev": {
@@ -22,7 +22,10 @@
2222
"autoload": {
2323
"psr-4": {
2424
"Monobank\\": "src/"
25-
}
25+
},
26+
"files": [
27+
"src/constants.php"
28+
]
2629
},
2730
"autoload-dev": {
2831
"psr-4": {

src/.gitkeep

Whitespace-only changes.

src/Exception/AccountException.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Monobank\Exception;
6+
7+
use Exception;
8+
9+
/**
10+
* Class AccountException.
11+
*/
12+
class AccountException extends Exception implements MonobankException
13+
{
14+
/**
15+
* @return AccountException
16+
*/
17+
public static function emptyID(): self
18+
{
19+
return new self('Account ID should not be blank.');
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Monobank\Exception;
6+
7+
use Exception;
8+
9+
/**
10+
* Class InvalidResponseException.
11+
*/
12+
class InvalidResponseException extends Exception implements MonobankException
13+
{
14+
/**
15+
* @return InvalidResponseException
16+
*/
17+
public static function wrongFormat(): self
18+
{
19+
return new self('Wrong response body format.');
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Monobank\Exception;
6+
7+
use Exception;
8+
9+
/**
10+
* Class InvalidTokenException.
11+
*/
12+
class InvalidTokenException extends Exception implements MonobankException
13+
{
14+
/**
15+
* @return InvalidTokenException
16+
*/
17+
public static function emptyToken(): self
18+
{
19+
return new self('Monobank API token should not be blank.');
20+
}
21+
}

src/Exception/MonobankException.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Monobank\Exception;
6+
7+
/**
8+
* Interface MonobankException.
9+
*/
10+
interface MonobankException
11+
{
12+
}

src/Exception/RequestException.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Monobank\Exception;
6+
7+
use Exception;
8+
9+
/**
10+
* Class RequestException.
11+
*/
12+
class RequestException extends Exception implements MonobankException
13+
{
14+
/**
15+
* @return RequestException
16+
*/
17+
public static function maxPersonalStatementPeriod(): self
18+
{
19+
return new self('The maximum period is 31 days (2678400 seconds).');
20+
}
21+
22+
/**
23+
* @return RequestException
24+
*/
25+
public static function invalidPersonalStatementPeriod(): self
26+
{
27+
return new self('"Date to" should be greater than "date from".');
28+
}
29+
}

src/MonobankClient.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Monobank;
6+
7+
use GuzzleHttp\RequestOptions;
8+
use Monobank\ValueObject\Token;
9+
use GuzzleHttp\ClientInterface;
10+
use Psr\Http\Message\ResponseInterface;
11+
use GuzzleHttp\Exception\GuzzleException;
12+
use Monobank\Request\PersonalStatementRequest;
13+
use Monobank\Exception\InvalidResponseException;
14+
15+
/**
16+
* Monobank HTTP client.
17+
*/
18+
class MonobankClient implements MonobankPublicDataClientInterface, MonobankPrivateDataClientInterface
19+
{
20+
private const HOST = 'https://api.monobank.ua';
21+
22+
/**
23+
* @var ClientInterface
24+
*/
25+
private $client;
26+
27+
/**
28+
* @var Token
29+
*/
30+
private $token;
31+
32+
/**
33+
* @var string
34+
*/
35+
private $host;
36+
37+
/**
38+
* @param ClientInterface $client
39+
* @param Token $token
40+
* @param string $host
41+
*/
42+
public function __construct(ClientInterface $client, Token $token, string $host = self::HOST)
43+
{
44+
$this->client = $client;
45+
$this->token = $token;
46+
$this->host = rtrim($host, '/');
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function getExchangeRates(): array
53+
{
54+
return $this->decode($this->request('GET', '/bank/currency'));
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
public function getClientInfo(): array
61+
{
62+
return $this->decode($this->request('GET', '/personal/client-info', $this->token));
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public function getPersonalStatement(PersonalStatementRequest $request): array
69+
{
70+
$endpoint = sprintf(
71+
'/personal/statement/%s/%d/%d',
72+
null === $request->getAccountID() ? '0' : strval($request->getAccountID()), // 0 - default account
73+
$request->getFrom()->getTimestamp(),
74+
$request->getTo()->getTimestamp()
75+
);
76+
77+
return $this->decode($this->request('GET', $endpoint, $this->token));
78+
}
79+
80+
/**
81+
* @param string $method
82+
* @param string $endpoint
83+
* @param Token|null $token
84+
*
85+
* @return ResponseInterface
86+
*
87+
* @throws GuzzleException
88+
*/
89+
private function request(string $method, string $endpoint, Token $token = null): ResponseInterface
90+
{
91+
$headers = [
92+
'Accept' => 'application/json',
93+
'User-Agent' => sprintf('%s MonobankPHPClient/%s', $this->client->getConfig('headers')['User-Agent'], MONOBANK_CLIENT_VERSION),
94+
];
95+
96+
if (null !== $token) {
97+
$headers['X-Token'] = strval($token);
98+
}
99+
100+
return $this->client->request($method, $this->host.$endpoint, [
101+
RequestOptions::HEADERS => $headers,
102+
]);
103+
}
104+
105+
/**
106+
* @param ResponseInterface $response
107+
*
108+
* @return array
109+
*
110+
* @throws InvalidResponseException
111+
*/
112+
private function decode(ResponseInterface $response): array
113+
{
114+
$decodedContents = json_decode($response->getBody()->getContents(), true);
115+
116+
if (!is_array($decodedContents)) {
117+
throw InvalidResponseException::wrongFormat();
118+
}
119+
120+
return $decodedContents;
121+
}
122+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Monobank;
6+
7+
use GuzzleHttp\Exception\GuzzleException;
8+
use Monobank\Request\PersonalStatementRequest;
9+
use Monobank\Exception\InvalidResponseException;
10+
11+
/**
12+
* Private data.
13+
*
14+
* @see https://api.monobank.ua/docs/
15+
* @see https://api.monobank.ua/docs/corporate.html
16+
*/
17+
interface MonobankPrivateDataClientInterface
18+
{
19+
/**
20+
* Obtaining information about the client and the list of his accounts.
21+
* Limit on the use of the function no more than 1 time in 60 seconds.
22+
*
23+
* @return array
24+
*
25+
* @throws GuzzleException
26+
* @throws InvalidResponseException
27+
*/
28+
public function getClientInfo(): array;
29+
30+
/**
31+
* Receive an extract for the time {from} to {to} time in seconds Unix time format.
32+
* The maximum time for which it is possible to extract is 31 days (2678400 seconds).
33+
* Limit on the use of the function no more than 1 time in 60 seconds.
34+
*
35+
* @param PersonalStatementRequest $request
36+
*
37+
* @return array
38+
*
39+
* @throws GuzzleException
40+
* @throws InvalidResponseException
41+
*/
42+
public function getPersonalStatement(PersonalStatementRequest $request): array;
43+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Monobank;
6+
7+
use GuzzleHttp\Exception\GuzzleException;
8+
use Monobank\Exception\InvalidResponseException;
9+
10+
/**
11+
* Public data.
12+
*
13+
* @see https://api.monobank.ua/docs/
14+
*/
15+
interface MonobankPublicDataClientInterface
16+
{
17+
/**
18+
* Get a basic list of monobank currency rates.
19+
* Information is cached and updated no more than once every 5 minutes.
20+
*
21+
* @return array
22+
*
23+
* @throws GuzzleException
24+
* @throws InvalidResponseException
25+
*/
26+
public function getExchangeRates(): array;
27+
}

0 commit comments

Comments
 (0)