ForeEx::getQueryString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace cHeeSaW\AlphaVantageBundle\Endpoints;
6
7
use InvalidArgumentException;
8
9
/**
10
 * @link https://www.alphavantage.co/documentation/#fx
11
 */
12
class ForeEx implements Endpoint
13
{
14
    public const FOREX_CURRENCY_EXCHANGE_RATE = 'CURRENCY_EXCHANGE_RATE';
15
    public const FOREX_FX_INTRADAY = 'FX_INTRADAY';
16
    public const FOREX_FX_DAILY = 'FX_DAILY';
17
    public const FOREX_FX_WEEKLY = 'FX_WEEKLY';
18
    public const FOREX_FX_MONTHLY = 'FX_MONTHLY';
19
20
    private static array $validFunctions = [
21
        self::FOREX_CURRENCY_EXCHANGE_RATE,
22
        self::FOREX_FX_INTRADAY,
23
        self::FOREX_FX_DAILY,
24
        self::FOREX_FX_WEEKLY,
25
        self::FOREX_FX_MONTHLY
26
    ];
27
28
    private static array $validIntervals = [
29
        self::INTERVAL_1_MIN,
30
        self::INTERVAL_5_MIN,
31
        self::INTERVAL_15_MIN,
32
        self::INTERVAL_30_MIN,
33
        self::INTERVAL_60_MIN
34
    ];
35
36
    private string $function;
37
38
    /**
39
     * @see https://www.alphavantage.co/physical_currency_list/
40
     * @see https://www.alphavantage.co/digital_currency_list/
41
     */
42
    private ?string $from_currency;
43
44
    /**
45
     * @see https://www.alphavantage.co/physical_currency_list/
46
     * @see https://www.alphavantage.co/digital_currency_list/
47
     */
48
    private ?string $to_currenccy;
49
50
    /**
51
     * A three-letter symbol from the forex currency list.
52
     * @see https://www.alphavantage.co/physical_currency_list/
53
     */
54
    private ?string $from_symbol;
55
56
    /**
57
     * A three-letter symbol from the forex currency list.
58
     * @see https://www.alphavantage.co/physical_currency_list/
59
     */
60
    private ?string $to_symbol;
61
62
    /**
63
     * Time interval between two consecutive data points in the time series.
64
     * The following values are supported: 1min, 5min, 15min, 30min, 60min
65
     */
66
    private string $interval;
67
68
    /**
69
     * By default, datatype=json. Strings json and csv are accepted with the following specifications:
70
     * json returns the intraday time series in JSON format; csv returns the time series as a
71
     * CSV (comma separated value) file.
72
     */
73
    private string $dataType;
74
75
    /**
76
     * By default, outputsize=compact. Strings compact and full are accepted with the following specifications:
77
     * compact returns only the latest 100 data points in the intraday time series; full returns the full-length
78
     * intraday time series. The "compact" option is recommended if you would like to reduce the data size of
79
     * each API call.
80
     */
81
    private string $outputsize;
82
83
    public function __construct(
84
        string $function,
85
        string $from_currency = null,
86
        string $to_currenccy = null,
87
        string $from_symbol = null,
88
        string $to_symbol = null,
89
        string $dataType = self::DATATYPE_JSON,
90
        string $outputsize = self::OUTPUTSIZE_COMPACT,
91
        string $interval = self::INTERVAL_5_MIN
92
    ) {
93
        if ($dataType !== self::DATATYPE_JSON && $dataType !== self::DATATYPE_CSV) {
94
            throw new InvalidArgumentException($dataType . ' is not valid, try: csv or json');
95
        }
96
97
        if (!in_array($function, self::$validFunctions, true)) {
98
            throw new InvalidArgumentException(
99
                $function . ' is not valid, check https://www.alphavantage.co/documentation/ for valid Forex functions'
100
            );
101
        }
102
103
        if (!in_array($interval, self::$validIntervals, true)) {
104
            throw new InvalidArgumentException(
105
                $interval . ' is not valid, valid ones are: 1min, 5min, 15min, 30min, 60min'
106
            );
107
        }
108
109
        if (!in_array($outputsize, [self::OUTPUTSIZE_COMPACT, self::OUTPUTSIZE_FULL], true)) {
110
            throw new InvalidArgumentException('Invalid outputsize given, valid values are: full, compact');
111
        }
112
113
        $this->outputsize = $outputsize;
114
        $this->dataType = $dataType;
115
        $this->function = $function;
116
        $this->interval = $interval;
117
        $this->from_currency = $from_currency;
118
        $this->to_currenccy = $to_currenccy;
119
        $this->from_symbol = $from_symbol;
120
        $this->to_symbol = $to_symbol;
121
    }
122
123
    public static function getValidFunctions(): array
124
    {
125
        return self::$validFunctions;
126
    }
127
128
    public static function getValidIntervals(): array
129
    {
130
        return self::$validIntervals;
131
    }
132
133
    public function getFunction(): string
134
    {
135
        return $this->function;
136
    }
137
138
    public function getFromCurrency(): ?string
139
    {
140
        return $this->from_currency;
141
    }
142
143
    public function getToCurrenccy(): ?string
144
    {
145
        return $this->to_currenccy;
146
    }
147
148
    public function getFromSymbol(): ?string
149
    {
150
        return $this->from_symbol;
151
    }
152
153
    public function getToSymbol(): ?string
154
    {
155
        return $this->to_symbol;
156
    }
157
158
    public function getInterval(): string
159
    {
160
        return $this->interval;
161
    }
162
163
    public function getDataType(): string
164
    {
165
        return $this->dataType;
166
    }
167
168
    public function getOutputsize(): string
169
    {
170
        return $this->outputsize;
171
    }
172
173
    public function getQueryString(): string
174
    {
175
        return http_build_query($this);
176
    }
177
}
178