Skip to content

Commit

Permalink
Readonly properties
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Nov 26, 2023
1 parent 7947862 commit edca194
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Context/CashContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class CashContext implements Context
*
* For example, step 5 on CHF would allow CHF 0.00, CHF 0.05, CHF 0.10, etc.
*/
private int $step;
private readonly int $step;

/**
* @param int $step The cash rounding step, in minor units. Must be a multiple of 2 and/or 5.
Expand Down
4 changes: 2 additions & 2 deletions src/Context/CustomContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ final class CustomContext implements Context
/**
* The scale of the monies using this context.
*/
private int $scale;
private readonly int $scale;

/**
* An optional cash rounding step. Must be a multiple of 2 and/or 5.
*
* For example, scale=4 and step=5 would allow amounts of 0.0000, 0.0005, 0.0010, etc.
*/
private int $step;
private readonly int $step;

/**
* @param int $scale The scale of the monies using this context.
Expand Down
8 changes: 4 additions & 4 deletions src/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class Currency implements Stringable, JsonSerializable
* For non ISO currencies no constraints are defined, but the code must be unique across an application, and must
* not conflict with ISO currency codes.
*/
private string $currencyCode;
private readonly string $currencyCode;

/**
* The numeric currency code.
Expand All @@ -33,23 +33,23 @@ final class Currency implements Stringable, JsonSerializable
*
* The numeric code can be useful when storing monies in a database.
*/
private int $numericCode;
private readonly int $numericCode;

/**
* The name of the currency.
*
* For ISO currencies this will be the official English name of the currency.
* For non ISO currencies no constraints are defined.
*/
private string $name;
private readonly string $name;

/**
* The default number of fraction digits (typical scale) used with this currency.
*
* For example, the default number of fraction digits for the Euro is 2, while for the Japanese Yen it is 0.
* This cannot be a negative number.
*/
private int $defaultFractionDigits;
private readonly int $defaultFractionDigits;

/**
* Class constructor.
Expand Down
2 changes: 1 addition & 1 deletion src/CurrencyConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class CurrencyConverter
/**
* The exchange rate provider.
*/
private ExchangeRateProvider $exchangeRateProvider;
private readonly ExchangeRateProvider $exchangeRateProvider;

/**
* @param ExchangeRateProvider $exchangeRateProvider The exchange rate provider.
Expand Down
4 changes: 2 additions & 2 deletions src/Exception/CurrencyConversionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
*/
class CurrencyConversionException extends MoneyException
{
private string $sourceCurrencyCode;
private readonly string $sourceCurrencyCode;

private string $targetCurrencyCode;
private readonly string $targetCurrencyCode;

/**
* CurrencyConversionException constructor.
Expand Down
4 changes: 2 additions & 2 deletions src/ExchangeRateProvider/BaseCurrencyProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ final class BaseCurrencyProvider implements ExchangeRateProvider
/**
* The provider for rates relative to the base currency.
*/
private ExchangeRateProvider $provider;
private readonly ExchangeRateProvider $provider;

/**
* The code of the currency all the exchanges rates are based on.
*/
private string $baseCurrencyCode;
private readonly string $baseCurrencyCode;

/**
* @param ExchangeRateProvider $provider The provider for rates relative to the base currency.
Expand Down
2 changes: 1 addition & 1 deletion src/ExchangeRateProvider/CachedProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class CachedProvider implements ExchangeRateProvider
/**
* The underlying exchange rate provider.
*/
private ExchangeRateProvider $provider;
private readonly ExchangeRateProvider $provider;

/**
* The cached exchange rates.
Expand Down
16 changes: 11 additions & 5 deletions src/ExchangeRateProvider/PDOProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ final class PDOProvider implements ExchangeRateProvider
/**
* The SELECT statement.
*/
private \PDOStatement $statement;
private readonly \PDOStatement $statement;

/**
* The source currency code if fixed, or null if dynamic.
*/
private ?string $sourceCurrencyCode = null;
private readonly ?string $sourceCurrencyCode;

/**
* The target currency code if fixed, or null if dynamic.
*/
private ?string $targetCurrencyCode = null;
private readonly ?string $targetCurrencyCode;

/**
* Extra parameters set dynamically to resolve the query placeholders.
Expand All @@ -47,18 +47,24 @@ public function __construct(\PDO $pdo, PDOProviderConfiguration $configuration)
$conditions[] = sprintf('(%s)', $configuration->whereConditions);
}

$sourceCurrencyCode = null;
$targetCurrencyCode = null;

if ($configuration->sourceCurrencyCode !== null) {
$this->sourceCurrencyCode = $configuration->sourceCurrencyCode;
$sourceCurrencyCode = $configuration->sourceCurrencyCode;
} elseif ($configuration->sourceCurrencyColumnName !== null) {
$conditions[] = sprintf('%s = ?', $configuration->sourceCurrencyColumnName);
}

if ($configuration->targetCurrencyCode !== null) {
$this->targetCurrencyCode = $configuration->targetCurrencyCode;
$targetCurrencyCode = $configuration->targetCurrencyCode;
} elseif ($configuration->targetCurrencyColumnName !== null) {
$conditions[] = sprintf('%s = ?', $configuration->targetCurrencyColumnName);
}

$this->sourceCurrencyCode = $sourceCurrencyCode;
$this->targetCurrencyCode = $targetCurrencyCode;

$conditions = implode(' AND ' , $conditions);

$query = sprintf(
Expand Down
2 changes: 1 addition & 1 deletion src/ISOCurrencyProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class ISOCurrencyProvider
*
* @psalm-var array<string, array{string, int, string, int}>
*/
private array $currencyData;
private readonly array $currencyData;

/**
* An associative array of currency numeric code to currency code.
Expand Down
6 changes: 3 additions & 3 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ final class Money extends AbstractMoney
/**
* The amount.
*/
private BigDecimal $amount;
private readonly BigDecimal $amount;

/**
* The currency.
*/
private Currency $currency;
private readonly Currency $currency;

/**
* The context that defines the capability of this Money.
*/
private Context $context;
private readonly Context $context;

/**
* @param BigDecimal $amount
Expand Down
2 changes: 1 addition & 1 deletion src/MoneyComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class MoneyComparator
/**
* The exchange rate provider.
*/
private ExchangeRateProvider $exchangeRateProvider;
private readonly ExchangeRateProvider $exchangeRateProvider;

/**
* Class constructor.
Expand Down
4 changes: 2 additions & 2 deletions src/RationalMoney.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
*/
final class RationalMoney extends AbstractMoney
{
private BigRational $amount;
private readonly BigRational $amount;

private Currency $currency;
private readonly Currency $currency;

/**
* Class constructor.
Expand Down

0 comments on commit edca194

Please sign in to comment.