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

h4kuna / exchange / 10609481475

29 Aug 2024 05:50AM UTC coverage: 97.872% (-0.7%) from 98.587%
10609481475

push

github

h4kuna
feat(typo): remove old doc

276 of 282 relevant lines covered (97.87%)

0.98 hits per line

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

93.33
/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
 * @since 2009-06-22 - version 0.5
12
 * @implements IteratorAggregate<string, CurrencyInterface>
13
 * @implements ArrayAccess<string, CurrencyInterface>
14
 * properties become readonly
15
 */
16
class Exchange implements IteratorAggregate, ArrayAccess
17
{
18
        private CurrencyInterface $from;
19

20
        private CurrencyInterface $to;
21

22

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

38

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

47

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

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

63
                return (float) $price;
1✔
64
        }
65

66

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

72

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

78

79
        public function isValid(): bool
80
        {
81
                return $this->ratingList->isValid();
×
82
        }
83

84

85
        /**
86
         * @return RatingListInterface<CurrencyInterface>
87
         */
88
        public function getIterator(): RatingListInterface
89
        {
90
                return $this->ratingList;
1✔
91
        }
92

93

94
        public function offsetExists(mixed $offset): bool
1✔
95
        {
96
                return $this->ratingList->offsetExists($offset);
1✔
97
        }
98

99

100
        public function offsetGet(mixed $offset): CurrencyInterface
1✔
101
        {
102
                return $this->get($offset);
1✔
103
        }
104

105

106
        public function offsetSet(mixed $offset, mixed $value): void
1✔
107
        {
108
                $this->ratingList->offsetSet($offset, $value);
1✔
109
        }
110

111

112
        public function offsetUnset(mixed $offset): void
1✔
113
        {
114
                $this->ratingList->offsetUnset($offset);
1✔
115
        }
116

117
}
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