• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

h4kuna / exchange / 10120265708

27 Jul 2024 02:42AM UTC coverage: 98.571%. First build
10120265708

push

github

h4kuna
feat(CurrencyInterface): for more customize

18 of 19 new or added lines in 5 files covered. (94.74%)

276 of 280 relevant lines covered (98.57%)

0.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.55
/src/Exchange.php
1
<?php declare(strict_types=1);
2

3
namespace h4kuna\Exchange;
4

5
use ArrayAccess;
6
use h4kuna\Exchange\Exceptions\UnknownCurrencyException;
7
use h4kuna\Exchange\RatingList\RatingListInterface;
8
use IteratorAggregate;
9

10
/**
11
 * @template T of CurrencyInterface
12
 * @since 2009-06-22 - version 0.5
13
 * @implements IteratorAggregate<string, T>
14
 * @implements ArrayAccess<string, T>
15
 * properties become readonly
16
 */
17
class Exchange implements IteratorAggregate, ArrayAccess
18
{
19
        private CurrencyInterface $from;
20

21
        private CurrencyInterface $to;
22

23

24
        public function __construct(
1✔
25
                string|CurrencyInterface $from,
26
                public RatingListInterface $ratingList,
27
                string|CurrencyInterface|null $to = null,
28
        )
29
        {
30
                $this->from = $from instanceof CurrencyInterface ? $from : $this->get($from);
1✔
31
                $this->to = $to === null
1✔
32
                        ? $this->from
1✔
NEW
33
                        : ($to instanceof CurrencyInterface ? $to : $this->get($to));
×
34
        }
1✔
35

36

37
        /**
38
         * @throws UnknownCurrencyException
39
         */
40
        public function get(string $code): CurrencyInterface
1✔
41
        {
42
                return $this->ratingList->getSafe($code);
1✔
43
        }
44

45

46
        /**
47
         * Transfer number by exchange rate.
48
         */
49
        public function change(float|int|null $price, ?string $from = null, ?string $to = null): float
1✔
50
        {
51
                if ($price == 0) { // intentionally 0, 0.0, null
1✔
52
                        return .0;
1✔
53
                }
54

55
                $from = $this->getFrom($from);
1✔
56
                $to = $this->getTo($to);
1✔
57
                if ($to !== $from) {
1✔
58
                        $price *= $from->getRate() / $to->getRate();
1✔
59
                }
60

61
                return (float) $price;
1✔
62
        }
63

64

65
        public function getFrom(?string $from = null): CurrencyInterface
1✔
66
        {
67
                return $from === null ? $this->from : $this->ratingList->get($from);
1✔
68
        }
69

70

71
        public function getTo(?string $to = null): CurrencyInterface
1✔
72
        {
73
                return $to === null ? $this->to : $this->ratingList->get($to);
1✔
74
        }
75

76

77
        public function getIterator(): RatingListInterface
78
        {
79
                return $this->ratingList;
1✔
80
        }
81

82

83
        public function offsetExists(mixed $offset): bool
1✔
84
        {
85
                return $this->ratingList->offsetExists($offset);
1✔
86
        }
87

88

89
        public function offsetGet(mixed $offset): CurrencyInterface
1✔
90
        {
91
                return $this->get($offset);
1✔
92
        }
93

94

95
        public function offsetSet(mixed $offset, mixed $value): void
1✔
96
        {
97
                $this->ratingList->offsetSet($offset, $value);
1✔
98
        }
99

100

101
        public function offsetUnset(mixed $offset): void
1✔
102
        {
103
                $this->ratingList->offsetUnset($offset);
1✔
104
        }
105

106
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc