RVRNEXT-5 add model and repository for currency exchange rate
This commit is contained in:
parent
7437e19de5
commit
1d96fd0cb9
71
src/PersistentData/Model/CurrencyExchangeRate.php
Normal file
71
src/PersistentData/Model/CurrencyExchangeRate.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php namespace RVR\PersistentData\Model;
|
||||
|
||||
use DateTime;
|
||||
use SokoWeb\PersistentData\Model\Model;
|
||||
|
||||
class CurrencyExchangeRate extends Model
|
||||
{
|
||||
protected static string $table = 'currency_exchange_rates';
|
||||
|
||||
protected static array $fields = ['currency_id', 'exchange_rate', 'valid_from'];
|
||||
|
||||
protected static array $relations = ['currency' => Currency::class];
|
||||
|
||||
private ?Currency $currency = null;
|
||||
|
||||
private ?int $currencyId = null;
|
||||
|
||||
private float $exchangeRate = 0.0;
|
||||
|
||||
private DateTime $validFrom;
|
||||
|
||||
public function setCurrency(Currency $currency): void
|
||||
{
|
||||
$this->currency = $currency;
|
||||
}
|
||||
|
||||
public function setCurrencyId(int $currencyId): void
|
||||
{
|
||||
$this->currencyId = $currencyId;
|
||||
}
|
||||
|
||||
public function setExchangeRate(float $exchangeRate): void
|
||||
{
|
||||
$this->exchangeRate = $exchangeRate;
|
||||
}
|
||||
|
||||
public function setValidFromDate(DateTime $validFrom): void
|
||||
{
|
||||
$this->validFrom = $validFrom;
|
||||
}
|
||||
|
||||
public function setValidFrom(string $validFrom): void
|
||||
{
|
||||
$this->validFrom = new DateTime($validFrom);
|
||||
}
|
||||
|
||||
public function getCurrency(): ?Currency
|
||||
{
|
||||
return $this->currency;
|
||||
}
|
||||
|
||||
public function getCurrencyId(): ?int
|
||||
{
|
||||
return $this->currencyId;
|
||||
}
|
||||
|
||||
public function getExchangeRate(): float
|
||||
{
|
||||
return $this->exchangeRate;
|
||||
}
|
||||
|
||||
public function getValidFromDate(): DateTime
|
||||
{
|
||||
return $this->validFrom;
|
||||
}
|
||||
|
||||
public function getValidFrom(): string
|
||||
{
|
||||
return $this->validFrom->format('Y-m-d H:i:s');
|
||||
}
|
||||
}
|
24
src/Repository/CurrencyExchangeRateRepository.php
Normal file
24
src/Repository/CurrencyExchangeRateRepository.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php namespace RVR\Repository;
|
||||
|
||||
use Generator;
|
||||
use Container;
|
||||
use RVR\PersistentData\Model\Currency;
|
||||
use RVR\PersistentData\Model\CurrencyExchangeRate;
|
||||
use SokoWeb\Database\Query\Select;
|
||||
|
||||
class CurrencyExchangeRateRepository
|
||||
{
|
||||
public function getById(int $id): ?CurrencyExchangeRate
|
||||
{
|
||||
return \Container::$persistentDataManager->selectFromDbById($id, CurrencyExchangeRate::class);
|
||||
}
|
||||
|
||||
public function getAllByCurrency(Currency $currency): Generator
|
||||
{
|
||||
$select = new Select(Container::$dbConnection);
|
||||
$select->where('currency_id', '=', $currency->getId());
|
||||
$select->orderBy('valid_from');
|
||||
|
||||
yield from Container::$persistentDataManager->selectMultipleFromDb($select, CurrencyExchangeRate::class);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user