Skip to content

Commit

Permalink
Exchange: evolution see changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Dec 22, 2022
1 parent 97e3141 commit cba295b
Show file tree
Hide file tree
Showing 53 changed files with 1,236 additions and 1,003 deletions.
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
changelog.md export-ignore
tests/ export-ignore
*.sh eol=lf
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['7.4', '8.0', '8.1', '8.2']
php: ['8.0', '8.1', '8.2']

fail-fast: false

Expand Down
27 changes: 0 additions & 27 deletions .travis.yml

This file was deleted.

99 changes: 24 additions & 75 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
Exchange
-------
[![Build Status](https://travis-ci.com/h4kuna/exchange.svg?branch=master)](https://travis-ci.com/h4kuna/exchange)
[![Latest stable](https://img.shields.io/packagist/v/h4kuna/exchange.svg)](https://packagist.org/packages/h4kuna/exchange)
[![Downloads this Month](https://img.shields.io/packagist/dm/h4kuna/exchange.svg)](https://packagist.org/packages/h4kuna/exchange)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/h4kuna/exchange/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/h4kuna/exchange/?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/h4kuna/exchange/badge.svg?branch=master)](https://coveralls.io/github/h4kuna/exchange?branch=master)

Exchange is PHP script works with currencies. You can convert price, format add VAT or only render exchange rates.
Exchange is PHP script works with currencies. You can convert price, format add VAT or only render exchange rates.

Here is [changelog](changelog.md).

## Extension for framework

- [Nette extension](//github.com/h4kuna/exchange-nette)

Installation via composer
-----------------------

```sh
$ composer require h4kuna/exchange
```

## How to use
Init object [Exchange](src/Exchange.php) and [Cache](src/Caching/Cache.php). Default Driver for read is [Cnb](src/Driver/Cnb/Day.php), [here are others](src/Driver).

Init object [Exchange](src/Exchange.php) by [ExchangeFactory](src/ExchangeFactory.php). Default Driver for read is [Cnb](src/Driver/Cnb/Day.php), [here are others](src/Driver).

For example define own exchange rates:

- 25 CZK = 1 EUR
- 20 CZK = 1 USD

```php
use h4kuna\Exchange;

$cache = new Caching\Cache('/temp/dir');
$exchange = new Exchange\Exchange($cache);
$exchangeFactory = new Exchange\ExchangeFactory('eur', 'usd', '/tmp/exchange', [
'czk',
'usd',
'eur',
]);

$exchange->setDefault('eur');
$exchange->setOutput('usd');
$exchange = $exchangeFactory->create();

echo $exchange->change(100); // EUR -> USD = 125.0
echo $exchange->change(100, 'czk'); // CZK -> USD = 5.0
Expand All @@ -42,61 +45,31 @@ echo $exchange->change(100, 'usd', 'czk'); // USD -> CZK = 2000.0
```

### Change driver and date

Download history exchange rates.
```php
$exchange->setDriver(new Exchange\Driver\Cnb\Day, new \Datetime('2000-12-30'));
```
If we need read data from database, it is not problem write own Driver.
```php
class MyDatabaseDriver extend \h4kuna\Exchange\Driver\ADriver
{
/**
* Load data for iterator.
* @param DateTime|NULL $date
* @return array
*/
protected function loadFromSource(DateTime $date = NULL)
{
// your implementation
$this->setDate('Y-m-d', (string) $database->selectDate());
return $database->fetchMyCurrencies();
}

/**
* Modify data before save to cache.
* @return Exchange\Currency\Property|NULL
*/
protected function createProperty($row)
{
return new \h4kuna\Exchange\Currency\Property([
'code' => $row['currency'],
'home' => $row['rate'],
'foreign' => 1
]);
}
}

$exchange->setDriver(new \MyDatabaseDriver);
```php
$exchange = $exchangeFactory->create(new \Datetime('2000-12-30'));
```

### Format output

Define output formats, for more information read this documentation [h4kuna/number-format](//github.com/h4kuna/number-format#numberformatstate).

```php
$formats = new Exchange\Currency\Formats(new \h4kuna\Number\NumberFormatFactory());

$formats->addFormat('EUR', ['decimalPoint' => '.', 'unit' => '€']);
```

Create [Filters](src/Filters.php) for format API.

```php
$filters = new Exchange\Filters($exchange, $formats);
```
You can define VAT
```php
// VAT 21%
$filters->setVat(new \h4kuna\Number\Tax(21));
$filters = new Exchange\Filters($exchange, $formats, new h4kuna\Number\Tax(21));
```

Output

```php

// format 100 EUR like default to USD is set above
Expand All @@ -113,21 +86,11 @@ $filters->changeTo(100, 'usd'); // 125.0
$filters->vat(100); // 121.0
```

### Temporary rate

```php
$exchange->addRate('usd', 23.0);
$exchange->change(1, 'usd', 'czk'); // 23.0

$exchange->removeRate('usd');
$exchange->change(1, 'usd', 'czk'); // 20.0
```

### Access and iterator

```php
/* @var $property Exchange\Currenry\Property */
$property = $exchange['eur'];
$property = $exchange->getRatingList()['eur'];
var_dump($property);


Expand All @@ -136,7 +99,9 @@ foreach ($exchange as $code => $property) {
var_dump($property);
}
```

### Limit for currencies

```php
$cache = new Caching\Cache('/temp/dir');
$cache->setAllowedCurrencies(['CZK', 'USD', 'EUR']);
Expand All @@ -148,19 +113,3 @@ foreach ($exchange as $code => $property) {
var_dump($property);
}
```

### Save state to cookie
Here is [prepared object](src/Http/CookieManager.php) whose keep selected currency.

Run on startup of you project.
```php
$cookieManager = Exchange\Http\CookieManager($exchange);
```

Set new option
```php
$cookieManager->setCurrency($_GET['currency']);
```

### Cache
Default Cache save data on filesystem you can implement [ICache](src/Caching/ICache.php) interface and change it.
18 changes: 18 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
# Changelog

## v7.0

- your temporary rate implement by own RatingList
- custom driver must be to use in Builder, see to ExchangeFactory
- cache is implemented by PSR-6
- remove dependency on Guzzle, PSR-7, PRS-17 and PSR-18 ready
- remove dependency on h4kuna/data-type, nette/safe-stream, nette/utils
- access for currency use RatingList instead of Exchange, Exchange::getRatingList()['EUR']
- support php 8.0+
- the methods that are preserved are the same prototype and behavior
- CookieManager moved [this extension](//github.com/h4kuna/exchange-nette)


## v6.0

- support php 7.1+
- use type hints
- api is same like v5.0


## v5.0

Dependency on Nette framework was removed, If you want, follow [this extension](//github.com/h4kuna/exchange-nette). Minimal php is 5.5+. Api is changed, not compatible with older version.


## v4.0

Here is [older version](//github.com/h4kuna/exchange/tree/v4.2.2).
30 changes: 15 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
}
],
"require": {
"php": ">=7.1",
"guzzlehttp/guzzle": ">=6.1",
"h4kuna/data-type": "^2.1",
"php": ">=8.0",
"arvenil/ninja-mutex": "^0.6",
"h4kuna/number-format": "^3.0",
"h4kuna/serialize-polyfill": "^0.0",
"nette/safe-stream": "^2.0",
"nette/utils": "^3.0"
"h4kuna/serialize-polyfill": "^0.2",
"psr/http-factory": "^1.0",
"psr/simple-cache": "^3.0"
},
"require-dev": {
"nette/http": "^3.0",
"guzzlehttp/guzzle": "^7.0",
"nette/tester": "^2.0",
"phpstan/phpstan": "^0.12",
"phpstan/phpstan": "^1.0",
"phpstan/phpstan-strict-rules": "^1.4",
"tracy/tracy": "^2.0"
},
"autoload": {
Expand All @@ -32,21 +32,21 @@
}
},
"suggest": {
"nette/http": "If you want to use h4kuna\\Exchange\\Http\\CookieManager.",
"guzzlehttp/guzzle": "As default implementation for PSR standards.",
"ext-simplexml": "If you want to use h4kuna\\Exchange\\Driver\\Ecb."
},
"autoload-dev": {
"psr-4": {
"h4kuna\\Exchange\\": "tests/src"
},
"files": [
"tests/lib/Driver.php"
]
"h4kuna\\Exchange\\Tests\\": "tests/src",
"h4kuna\\Exchange\\Fixtures\\": "tests/Fixtures"
}
},
"config": {
"sort-packages": true
},
"scripts": {
"phpstan": "vendor/bin/phpstan analyse --level max -c tests/config/phpstan.neon src tests"
"phpstan": "vendor/bin/phpstan analyse",
"tests": "vendor/bin/tester -s -j 4 -C --colors 1 tests/src",
"coverage": "vendor/bin/tester -s -C -p xphp --coverage tests/temp/coverage.html --coverage-src=src --colors 1 tests/src"
}
}
84 changes: 84 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
parameters:
level: max
paths:
- src
- tests/src
bootstrapFiles:
- tests/bootstrap.php
ignoreErrors:
-
message: "#^Variable property access on \\$this\\(h4kuna\\\\Exchange\\\\Currency\\\\Property\\)\\.$#"
count: 1
path: src/Currency/Property.php
-
message: "#^Method h4kuna\\\\Exchange\\\\ExchangeFactory\\:\\:getDriver\\(\\) return type with generic class h4kuna\\\\Exchange\\\\Driver\\\\Driver does not specify its types\\: T$#"
count: 1
path: src/ExchangeFactory.php

-
message: "#^Method h4kuna\\\\Exchange\\\\ExchangeFactory\\:\\:setDriver\\(\\) has parameter \\$driver with generic class h4kuna\\\\Exchange\\\\Driver\\\\Driver but does not specify its types\\: T$#"
count: 1
path: src/ExchangeFactory.php

-
message: "#^Property h4kuna\\\\Exchange\\\\ExchangeFactory\\:\\:\\$driver with generic class h4kuna\\\\Exchange\\\\Driver\\\\Driver does not specify its types\\: T$#"
count: 1
path: src/ExchangeFactory.php

-
message: "#^Method h4kuna\\\\Exchange\\\\RatingList\\\\Builder\\:\\:create\\(\\) has parameter \\$driver with generic class h4kuna\\\\Exchange\\\\Driver\\\\Driver but does not specify its types\\: T$#"
count: 1
path: src/RatingList/Builder.php

-
message: "#^Method h4kuna\\\\Exchange\\\\RatingList\\\\RatingListBuilder\\:\\:countTTL\\(\\) has parameter \\$driver with generic class h4kuna\\\\Exchange\\\\Driver\\\\Driver but does not specify its types\\: T$#"
count: 1
path: src/RatingList/RatingListBuilder.php

-
message: "#^Method h4kuna\\\\Exchange\\\\RatingList\\\\RatingListBuilder\\:\\:create\\(\\) has parameter \\$driver with generic class h4kuna\\\\Exchange\\\\Driver\\\\Driver but does not specify its types\\: T$#"
count: 1
path: src/RatingList/RatingListBuilder.php

-
message: "#^Method h4kuna\\\\Exchange\\\\RatingList\\\\RatingListCache\\:\\:create\\(\\) has parameter \\$driver with generic class h4kuna\\\\Exchange\\\\Driver\\\\Driver but does not specify its types\\: T$#"
count: 1
path: src/RatingList/RatingListCache.php

-
message: "#^Method h4kuna\\\\Exchange\\\\RatingList\\\\RatingListCache\\:\\:createKey\\(\\) has parameter \\$driver with generic class h4kuna\\\\Exchange\\\\Driver\\\\Driver but does not specify its types\\: T$#"
count: 1
path: src/RatingList/RatingListCache.php

-
message: "#^Method h4kuna\\\\Exchange\\\\RatingList\\\\RatingListCache\\:\\:criticalSection\\(\\) has parameter \\$driver with generic class h4kuna\\\\Exchange\\\\Driver\\\\Driver but does not specify its types\\: T$#"
count: 1
path: src/RatingList/RatingListCache.php

-
message: "#^Method h4kuna\\\\Exchange\\\\RatingList\\\\RatingListCache\\:\\:driverName\\(\\) has parameter \\$driver with generic class h4kuna\\\\Exchange\\\\Driver\\\\Driver but does not specify its types\\: T$#"
count: 1
path: src/RatingList/RatingListCache.php

-
message: "#^Method h4kuna\\\\Exchange\\\\RatingList\\\\RatingListCache\\:\\:flush\\(\\) has parameter \\$driver with generic class h4kuna\\\\Exchange\\\\Driver\\\\Driver but does not specify its types\\: T$#"
count: 1
path: src/RatingList/RatingListCache.php

-
message: "#^Method h4kuna\\\\Exchange\\\\RatingList\\\\RatingListCache\\:\\:tryCreateCriticalSection\\(\\) has parameter \\$driver with generic class h4kuna\\\\Exchange\\\\Driver\\\\Driver but does not specify its types\\: T$#"
count: 1
path: src/RatingList/RatingListCache.php

-
message: "#^Method h4kuna\\\\Exchange\\\\RatingList\\\\RatingListLongProcess\\:\\:__construct\\(\\) has parameter \\$driver with generic class h4kuna\\\\Exchange\\\\Driver\\\\Driver but does not specify its types\\: T$#"
count: 1
path: src/RatingList/RatingListLongProcess.php

-
message: "#^Expression \"\\$ratingList\\['qwe'\\]\" on a separate line does not do anything\\.$#"
count: 1
path: tests/src/RatingList/RatingListTest.php

includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
Loading

0 comments on commit cba295b

Please sign in to comment.