Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ In the example above, we are retrieving exchange rates for GBP and EUR based on
which includes the base currency, retrieved rates and the time of retrieval. Retrieved rates are an `array` with currency codes as keys and exchange rates as values.

```php
$rates = $exchangeRates->getRates(); // ['GBP' => 1.0, 'EUR' => 1.0]
/** @var \Worksome\Exchange\Support\Rates $exchangeRates */
$rates = $exchangeRates->rates; // ['GBP' => 1.0, 'EUR' => 1.0]
```

### Fixer
Expand Down
2 changes: 1 addition & 1 deletion src/Actions/ValidateCurrencyCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Worksome\Exchange\Contracts\CurrencyCodeProvider;
use Worksome\Exchange\Exceptions\InvalidCurrencyCodeException;

final class ValidateCurrencyCodes implements ValidatesCurrencyCodes
final readonly class ValidateCurrencyCodes implements ValidatesCurrencyCodes
{
public function __construct(private CurrencyCodeProvider $currencyCodeProvider)
{
Expand Down
4 changes: 1 addition & 3 deletions src/Commands/Concerns/HasUsefulConsoleMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@

use function Termwind\render;

/**
* @mixin Command
*/
/** @mixin Command */
trait HasUsefulConsoleMethods
{
private function success(string $message): self
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class InstallCommand extends Command

protected $signature = 'exchange:install';

protected $description = 'Publish Exchange\'s config file to your project.';
protected $description = "Publish Exchange's config file to your project.";

public function handle(): int
{
Expand Down
32 changes: 18 additions & 14 deletions src/Commands/ViewLatestRatesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public function handle(Exchange $exchange, CurrencyCodeProvider $currencyCodePro
$data = $this->data($currencyCodeProvider);

try {
// @phpstan-ignore argument.type, argument.type
$this->renderRates($exchange->rates($data['base_currency'], $data['currencies']));
} catch (InvalidCurrencyCodeException $exception) {
$this->newLine();
Expand All @@ -44,23 +43,28 @@ public function handle(Exchange $exchange, CurrencyCodeProvider $currencyCodePro
return self::SUCCESS;
}

/**
* @return array<string, mixed>
*/
/** @return array{base_currency: string, currencies: non-empty-list<string>} */
private function data(CurrencyCodeProvider $currencyCodeProvider): array
{
/** @var array<int, string> $givenCurrencies */
/** @var list<string> $givenCurrencies */
$givenCurrencies = $this->argument('currencies');

$baseCurrency = $this->argument('base-currency') ?? $this->ask('Which base currency do you want to use?');

assert(is_string($baseCurrency));
Comment thread
owenvoke marked this conversation as resolved.

/** @var list<string> $currencies */
$currencies = count($givenCurrencies) > 0 ? $givenCurrencies : $this->choice(
'Which currencies do you want to fetch exchange rates for?',
$currencyCodeProvider->all(),
multiple: true,
);

assert($currencies !== []);
Comment thread
owenvoke marked this conversation as resolved.

return [
'base_currency' => $this->argument('base-currency') ?? $this->ask(
'Which base currency do you want to use?'
),
'currencies' => count($givenCurrencies) > 0 ? $givenCurrencies : $this->choice(
'Which currencies do you want to fetch exchange rates for?',
$currencyCodeProvider->all(),
multiple: true,
),
'base_currency' => $baseCurrency,
'currencies' => $currencies,
];
}

Expand All @@ -85,6 +89,6 @@ private function renderRates(Rates $rates): void
@endforeach
</div>
</div>
HTML, ['baseCurrency' => $rates->getBaseCurrency(), 'rates' => $rates->getRates()]));
HTML, ['baseCurrency' => $rates->baseCurrency, 'rates' => $rates->rates]));
}
}
4 changes: 2 additions & 2 deletions src/Contracts/Actions/ValidatesCurrencyCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
interface ValidatesCurrencyCodes
{
/**
* @param non-empty-array<int, string> $currencyCodes
* @param non-empty-list<string> $currencyCodes
*
* @return non-empty-array<int, string>
* @return non-empty-list<string>
*
* @throws InvalidCurrencyCodeException
*/
Expand Down
4 changes: 1 addition & 3 deletions src/Contracts/CurrencyCodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

interface CurrencyCodeProvider
{
/**
* @return non-empty-array<int, string>
*/
/** @return non-empty-list<string> */
public function all(): array;
}
4 changes: 1 addition & 3 deletions src/Contracts/ExchangeRateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

interface ExchangeRateProvider
{
/**
* @param non-empty-array<int, string> $currencies
*/
/** @param non-empty-list<string> $currencies */
public function getRates(string $baseCurrency, array $currencies): Rates;
}
6 changes: 3 additions & 3 deletions src/Exchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
final class Exchange
{
public function __construct(
private ValidatesCurrencyCodes $validateCurrencyCodes,
private readonly ValidatesCurrencyCodes $validateCurrencyCodes,
private ExchangeRateProvider $exchangeRateProvider,
) {
}
Expand All @@ -23,11 +23,11 @@ public function __construct(
*/
public function fake(array $rates = []): void
{
$this->exchangeRateProvider = (new FakeExchangeRateProvider())->defineRates($rates);
$this->exchangeRateProvider = new FakeExchangeRateProvider()->defineRates($rates);
}

/**
* @param non-empty-array<int, string> $currencies
* @param non-empty-list<string> $currencies
*
* @throws InvalidCurrencyCodeException
*/
Expand Down
2 changes: 1 addition & 1 deletion src/ExchangeRateProviders/CachedProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Worksome\Exchange\Contracts\ExchangeRateProvider;
use Worksome\Exchange\Support\Rates;

final class CachedProvider implements ExchangeRateProvider
final readonly class CachedProvider implements ExchangeRateProvider
{
public function __construct(
private Repository $cache,
Expand Down
2 changes: 1 addition & 1 deletion src/ExchangeRateProviders/CurrencyGEOProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Worksome\Exchange\Contracts\ExchangeRateProvider;
use Worksome\Exchange\Support\Rates;

final class CurrencyGEOProvider implements ExchangeRateProvider
final readonly class CurrencyGEOProvider implements ExchangeRateProvider
{
public function __construct(
private Factory $client,
Expand Down
2 changes: 1 addition & 1 deletion src/ExchangeRateProviders/ExchangeRateHostProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Worksome\Exchange\Contracts\ExchangeRateProvider;
use Worksome\Exchange\Support\Rates;

final class ExchangeRateHostProvider implements ExchangeRateProvider
final readonly class ExchangeRateHostProvider implements ExchangeRateProvider
{
private FixerProvider $fixerProvider;

Expand Down
2 changes: 1 addition & 1 deletion src/ExchangeRateProviders/FixerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Worksome\Exchange\Contracts\ExchangeRateProvider;
use Worksome\Exchange\Support\Rates;

final class FixerProvider implements ExchangeRateProvider
final readonly class FixerProvider implements ExchangeRateProvider
{
public function __construct(
private Factory $client,
Expand Down
2 changes: 1 addition & 1 deletion src/ExchangeRateProviders/FrankfurterProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Worksome\Exchange\Contracts\ExchangeRateProvider;
use Worksome\Exchange\Support\Rates;

final class FrankfurterProvider implements ExchangeRateProvider
final readonly class FrankfurterProvider implements ExchangeRateProvider
{
public function __construct(
private Factory $client,
Expand Down
2 changes: 1 addition & 1 deletion src/ExchangeRateProviders/NullProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Worksome\Exchange\Contracts\ExchangeRateProvider;
use Worksome\Exchange\Support\Rates;

final class NullProvider implements ExchangeRateProvider
final readonly class NullProvider implements ExchangeRateProvider
{
public function getRates(string $baseCurrency, array $currencies): Rates
{
Expand Down
2 changes: 1 addition & 1 deletion src/ExchangeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function registeringPackage(): void

$this->app->bind(
ExchangeRateProvider::class,
fn (Application $app) => (new ExchangeRateManager($app))->driver()
fn (Application $app) => new ExchangeRateManager($app)->driver()
);

$this->app->bind(CurrencyCodeProvider::class, FlatCurrencyCodeProvider::class);
Expand Down
1 change: 1 addition & 0 deletions src/Support/FlatCurrencyCodeProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

final class FlatCurrencyCodeProvider implements CurrencyCodeProvider
{
/** {@inheritdoc} */
public function all(): array
{
return [
Expand Down
20 changes: 10 additions & 10 deletions src/Support/Rates.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@
namespace Worksome\Exchange\Support;

use Carbon\CarbonInterface;
use Deprecated;

final class Rates
final readonly class Rates
{
/**
* @param non-empty-array<string, float> $rates
*/
/** @param non-empty-array<string, float> $rates */
public function __construct(
private string $baseCurrency,
private array $rates,
private CarbonInterface $retrievedAt,
public string $baseCurrency,
public array $rates,
public CarbonInterface $retrievedAt,
) {
}

#[Deprecated('Use `baseCurrency` property instead. This will be removed in 3.x', since: '2.5.0')]
public function getBaseCurrency(): string
{
return $this->baseCurrency;
}

/**
* @return non-empty-array<string, float>
*/
/** @return non-empty-array<string, float> */
#[Deprecated('Use `rates` property instead. This will be removed in 3.x', since: '2.5.0')]
public function getRates(): array
{
return $this->rates;
}

#[Deprecated('Use `retrievedAt` property instead. This will be removed in 3.x', since: '2.5.0')]
public function getRetrievedAt(): CarbonInterface
{
return $this->retrievedAt;
Expand Down