Skip to content

Commit

Permalink
PDOProviderConfiguration constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Jan 2, 2023
1 parent 2639c7d commit e971c6f
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 132 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
💥 **Breaking changes**

- Minimum PHP version is now 8.0
- `PDOProviderConfiguration` now has a proper constructor, and its properties are no longer public
- `PDOProviderConfiguration` now throws exceptions in the constructor when configuration is invalid

## [0.7.0](https://github.com/brick/money/releases/tag/0.7.0) - 2022-10-06

Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,16 +299,17 @@ use Brick\Money\ExchangeRateProvider\PDOProviderConfiguration;

$pdo = new \PDO(...);

$configuration = new PDOProviderConfiguration;
$configuration->tableName = 'exchange_rates';
$configuration->sourceCurrencyColumnName = 'source_currency_code';
$configuration->targetCurrencyColumnName = 'target_currency_code';
$configuration->exchangeRateColumnName = 'exchange_rate';
$configuration = new PDOProviderConfiguration(
tableName: 'exchange_rates',
exchangeRateColumnName: 'exchange_rate',
sourceCurrencyColumnName: 'source_currency_code',
targetCurrencyColumnName: 'target_currency_code',
);

$provider = new PDOProvider($pdo, $configuration);
```

PDOProvider also supports fixed source or target currency, and dynamic WHERE conditions. Check the [PDOProviderConfiguration](https://github.com/brick/money/blob/0.4.0/src/ExchangeRateProvider/PDOProviderConfiguration.php) class for more information.
PDOProvider also supports fixed source or target currency, and dynamic `WHERE` conditions. Check the [PDOProviderConfiguration](https://github.com/brick/money/blob/0.8.0/src/ExchangeRateProvider/PDOProviderConfiguration.php) class for more information.

### BaseCurrencyProvider

Expand Down
40 changes: 12 additions & 28 deletions src/ExchangeRateProvider/PDOProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,44 +42,28 @@ public function __construct(\PDO $pdo, PDOProviderConfiguration $configuration)
{
$conditions = [];

if ($configuration->tableName === null) {
throw new \InvalidArgumentException('Invalid configuration: $tableName is not set.');
if (null !== $whereConditions = $configuration->getWhereConditions()) {
$conditions[] = sprintf('(%s)', $whereConditions);
}

if ($configuration->exchangeRateColumnName === null) {
throw new \InvalidArgumentException('Invalid configuration: $exchangeRateColumnName is not set.');
if (null !== $sourceCurrencyCode = $configuration->getSourceCurrencyCode()) {
$this->sourceCurrencyCode = $sourceCurrencyCode;
} elseif (null !== $sourceCurrencyColumnName = $configuration->getSourceCurrencyColumnName()) {
$conditions[] = sprintf('%s = ?', $sourceCurrencyColumnName);
}

if ($configuration->sourceCurrencyCode !== null && $configuration->targetCurrencyCode !== null) {
throw new \InvalidArgumentException('Invalid configuration: $sourceCurrencyCode and $targetCurrencyCode cannot be both set.');
}

if ($configuration->whereConditions !== null) {
$conditions[] = '(' . $configuration->whereConditions . ')';
}

if ($configuration->sourceCurrencyCode !== null) {
$this->sourceCurrencyCode = $configuration->sourceCurrencyCode;
} elseif ($configuration->sourceCurrencyColumnName !== null) {
$conditions[] = $configuration->sourceCurrencyColumnName . ' = ?';
} else {
throw new \InvalidArgumentException('Invalid configuration: one of $sourceCurrencyCode or $sourceCurrencyColumnName must be set.');
}

if ($configuration->targetCurrencyCode !== null) {
$this->targetCurrencyCode = $configuration->targetCurrencyCode;
} elseif ($configuration->targetCurrencyColumnName !== null) {
$conditions[] = $configuration->targetCurrencyColumnName . ' = ?';
} else {
throw new \InvalidArgumentException('Invalid configuration: one of $targetCurrencyCode or $targetCurrencyColumnName must be set.');
if (null !== $targetCurrencyCode = $configuration->getTargetCurrencyCode()) {
$this->targetCurrencyCode = $targetCurrencyCode;
} elseif (null !== $targetCurrencyColumnName = $configuration->getTargetCurrencyColumnName()) {
$conditions[] = sprintf('%s = ?', $targetCurrencyColumnName);
}

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

$query = sprintf(
'SELECT %s FROM %s WHERE %s',
$configuration->exchangeRateColumnName,
$configuration->tableName,
$configuration->getExchangeRateColumnName(),
$configuration->getTableName(),
$conditions
);

Expand Down
167 changes: 117 additions & 50 deletions src/ExchangeRateProvider/PDOProviderConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,126 @@

namespace Brick\Money\ExchangeRateProvider;

use InvalidArgumentException;

/**
* Configuration for the PDOExchangeRateProvider.
*
* @psalm-suppress MissingConstructor
*/
final class PDOProviderConfiguration
{
/**
* The name of the table that holds the exchange rates. Required.
*/
public ?string $tableName = null;

/**
* The name of the column that holds the source currency code. Optional.
*
* If not set, $sourceCurrencyCode must be set.
*/
public ?string $sourceCurrencyColumnName = null;

/**
* The source currency code, if it is fixed. Optional.
*
* If not set, $sourceCurrencyColumnName must be set.
*/
public ?string $sourceCurrencyCode = null;

/**
* The name of the column that holds the target currency code. Optional.
*
* If not set, $targetCurrencyCode must be set.
*/
public ?string $targetCurrencyColumnName = null;

/**
* The target currency code, if it is fixed. Optional.
*
* If not set, $targetCurrencyColumnName must be set.
*/
public ?string $targetCurrencyCode = null;

/**
* The name of the column that holds the exchange rate for the currency pair. Required.
*/
public ?string $exchangeRateColumnName = null;

/**
* Extra WHERE conditions that will be included in the database query. Optional.
*
* The conditions can include question mark placeholders, that will be resolved dynamically when loading
* exchange rates. The parameters need to be set using the setParameters() method. The number of parameters
* provided to setParameters() must match the number of placeholders.
*
* This can be used, for example, to query an exchange rate for a particular date.
*/
public ?string $whereConditions = null;
public function __construct(
/**
* The name of the table that holds the exchange rates. Required.
*/
private string $tableName,

/**
* The name of the column that holds the exchange rate for the currency pair. Required.
*/
private string $exchangeRateColumnName,

/**
* The source currency code, if it is fixed. Optional.
*
* If not set, $sourceCurrencyColumnName must be set.
*/
private ?string $sourceCurrencyCode = null,

/**
* The name of the column that holds the source currency code. Optional.
*
* If not set, $sourceCurrencyCode must be set.
*/
private ?string $sourceCurrencyColumnName = null,

/**
* The target currency code, if it is fixed. Optional.
*
* If not set, $targetCurrencyColumnName must be set.
*/
private ?string $targetCurrencyCode = null,

/**
* The name of the column that holds the target currency code. Optional.
*
* If not set, $targetCurrencyCode must be set.
*/
private ?string $targetCurrencyColumnName = null,

/**
* Extra WHERE conditions that will be included in the database query. Optional.
*
* The conditions can include question mark placeholders, that will be resolved dynamically when loading
* exchange rates. The parameters need to be set using the setParameters() method. The number of parameters
* provided to setParameters() must match the number of placeholders.
*
* This can be used, for example, to query an exchange rate for a particular date.
*/
private ?string $whereConditions = null,
) {
if ($sourceCurrencyCode === null && $sourceCurrencyColumnName === null) {
throw new InvalidArgumentException(
'Invalid configuration: one of $sourceCurrencyCode or $sourceCurrencyColumnName must be set.',
);
}

if ($sourceCurrencyCode !== null && $sourceCurrencyColumnName !== null) {
throw new InvalidArgumentException(
'Invalid configuration: $sourceCurrencyCode and $sourceCurrencyColumnName cannot be both set.',
);
}

if ($targetCurrencyCode === null && $targetCurrencyColumnName === null) {
throw new \InvalidArgumentException(
'Invalid configuration: one of $targetCurrencyCode or $targetCurrencyColumnName must be set.',
);
}

if ($targetCurrencyCode !== null && $targetCurrencyColumnName !== null) {
throw new InvalidArgumentException(
'Invalid configuration: $targetCurrencyCode and $targetCurrencyColumnName cannot be both set.',
);
}

if ($sourceCurrencyCode !== null && $targetCurrencyCode !== null) {
throw new InvalidArgumentException(
'Invalid configuration: $sourceCurrencyCode and $targetCurrencyCode cannot be both set.',
);
}
}

public function getTableName(): string
{
return $this->tableName;
}

public function getExchangeRateColumnName(): string
{
return $this->exchangeRateColumnName;
}

public function getSourceCurrencyColumnName(): ?string
{
return $this->sourceCurrencyColumnName;
}

public function getSourceCurrencyCode(): ?string
{
return $this->sourceCurrencyCode;
}

public function getTargetCurrencyColumnName(): ?string
{
return $this->targetCurrencyColumnName;
}

public function getTargetCurrencyCode(): ?string
{
return $this->targetCurrencyCode;
}

public function getWhereConditions(): ?string
{
return $this->whereConditions;
}
}

0 comments on commit e971c6f

Please sign in to comment.