Skip to content

Commit

Permalink
Exchange: keep internal object instead of string like currency code
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Apr 5, 2023
1 parent 946114c commit 6728a60
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.gitignore export-ignore
changelog.md export-ignore
tests/ export-ignore
phpstan.neon export-ignore
*.sh eol=lf
50 changes: 38 additions & 12 deletions src/Exchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,61 @@
*/
class Exchange implements \IteratorAggregate
{
private Currency\Property $from;

private Currency\Property $to;


public function __construct(
private string $from,
private string $to,
string $from,
string $to,
private RatingList\Accessor $ratingList,
)
{
$this->setFrom($from);
$this->setTo($to);
}


public function getFrom(): string
public function getFrom(): Currency\Property
{
return $this->from;
}


public function default(string $to, string $from = null): static
public function default(string $to, string $from = ''): static
{
$exchange = clone $this;
$exchange->from = strtoupper($from ?? $this->from);
$exchange->to = strtoupper($to);
if ($from !== '') {
$exchange->setFrom($from);
}
$exchange->setTo($to);

return $exchange;
}


public function getTo(): string
public function getTo(): Currency\Property
{
return $this->to;
}


/**
* @deprecated use getFrom()
*/
public function getDefault(): Currency\Property
{
return $this->ratingList->get()->offsetGet($this->from);
return $this->getFrom();
}


/**
* @deprecated use getTo()
*/
public function getOutput(): Currency\Property
{
return $this->ratingList->get()->offsetGet($this->to);
return $this->getTo();
}


Expand All @@ -69,12 +83,12 @@ public function change(float $price, ?string $from = null, ?string $to = null):
*/
public function transfer(float $price, ?string $from = null, ?string $to = null): array
{
$to = $this->ratingList->get()->offsetGet($to ?? $this->to);
$to = $to === null ? $this->to : $this->ratingList->get()->offsetGet($to);
if ($price === 0.0) {
return [0, $to];
return [0.0, $to];
}

$from = $this->ratingList->get()->offsetGet($from ?? $this->from);
$from = $from === null ? $this->from : $this->ratingList->get()->offsetGet($from);
if ($to !== $from) {
$price *= $from->rate / $to->rate;
}
Expand All @@ -97,4 +111,16 @@ public function getRatingList(): RatingList\RatingList
return $this->ratingList->get();
}


protected function setFrom(string $from): void
{
$this->from = $this->ratingList->get()->offsetGet(strtoupper($from));
}


protected function setTo(string $to): void
{
$this->to = $this->ratingList->get()->offsetGet(strtoupper($to));
}

}
1 change: 1 addition & 0 deletions src/RatingList/RatingListCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function create(string $driver, \DateTimeInterface $date = null): RatingL
{
$key = self::createKey($driver, $date);
$ratingList = $this->cache->get($key);
assert($ratingList === null || $ratingList instanceof RatingList);

if ($ratingList === null || $ratingList->isValid() === false) {
$ratingList = $this->cache->load($key, fn (
Expand Down
7 changes: 5 additions & 2 deletions tests/src/ExchangeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
$exchange = $exchangeFactory->create();

// change driver
Assert::same('EUR', $exchange->getDefault()->code);
Assert::same($exchange->getOutput(), $exchange->getDefault());
Assert::same('EUR', $exchange->getFrom()->code);
Assert::same($exchange->getTo(), $exchange->getFrom());

Assert::same(100.0, $exchange->change(100));
Assert::same(25.0, $exchange->change(1, 'EUR', 'CZK'));
Expand All @@ -29,3 +29,6 @@
foreach ($exchange as $code => $property) {
Assert::type(Exchange\Currency\Property::class, $property);
}

$exchange2 = $exchange->default('CZK', 'EUR');
Assert::same(2500.0, $exchange2->change(100));

0 comments on commit 6728a60

Please sign in to comment.