1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digitonic\IexCloudSdk\Stocks; |
4
|
|
|
|
5
|
|
|
use Digitonic\IexCloudSdk\Contracts\IEXCloud; |
6
|
|
|
use Digitonic\IexCloudSdk\Exceptions\WrongData; |
7
|
|
|
use Digitonic\IexCloudSdk\Requests\BaseRequest; |
8
|
|
|
|
9
|
|
|
class Batch extends BaseRequest |
10
|
|
|
{ |
11
|
|
|
const ENDPOINT = 'stock/{symbol}/batch?'; |
12
|
|
|
|
13
|
|
|
protected $symbol = 'market'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $symbols; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $types; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $range; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create constructor. |
32
|
|
|
* |
33
|
|
|
* @param IEXCloud $api |
34
|
|
|
*/ |
35
|
7 |
|
public function __construct(IEXCloud $api) |
36
|
|
|
{ |
37
|
7 |
|
parent::__construct($api); |
38
|
7 |
|
} |
39
|
|
|
|
40
|
5 |
|
public function setSymbols(...$symbols): self |
41
|
|
|
{ |
42
|
5 |
|
if (count($symbols) === 1) { |
43
|
3 |
|
$this->symbol = $symbols[0]; |
44
|
|
|
} |
45
|
|
|
|
46
|
5 |
|
$this->symbols = implode(',', $symbols); |
47
|
|
|
|
48
|
5 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
4 |
|
public function setTypes(...$types): self |
52
|
|
|
{ |
53
|
4 |
|
$this->types = implode(',', $types); |
54
|
|
|
|
55
|
4 |
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
2 |
|
public function setRange(string $range): self |
59
|
|
|
{ |
60
|
2 |
|
$this->range = $range; |
61
|
|
|
|
62
|
2 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
4 |
|
public function getFullEndpoint(): string |
69
|
|
|
{ |
70
|
|
|
$params = [ |
71
|
4 |
|
'types' => $this->types, |
72
|
|
|
]; |
73
|
|
|
|
74
|
4 |
|
if (count(explode(',', $this->symbols)) > 1) { |
75
|
1 |
|
$params['symbols'] = $this->symbols; |
76
|
|
|
} |
77
|
|
|
|
78
|
4 |
|
if ($this->range) { |
79
|
2 |
|
if (in_array('chart', explode(',', $this->types))) { |
80
|
1 |
|
$params['range'] = $this->range; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
$query = http_build_query($params); |
85
|
|
|
|
86
|
4 |
|
$endpoint = str_replace('{symbol}', $this->symbol, self::ENDPOINT); |
87
|
4 |
|
$endpoint = $endpoint . $query; |
88
|
|
|
|
89
|
4 |
|
return $endpoint; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return bool|void |
94
|
|
|
*/ |
95
|
6 |
|
protected function validateParams(): void |
96
|
|
|
{ |
97
|
6 |
|
if (empty($this->symbols)) { |
98
|
1 |
|
throw WrongData::invalidValuesProvided('Please provide a symbol to query!'); |
99
|
|
|
} |
100
|
|
|
|
101
|
5 |
|
if (empty($this->types)) { |
102
|
1 |
|
throw WrongData::invalidValuesProvided( |
103
|
|
|
'Types Required: comma delimited list of endpoints to call. ' . |
104
|
1 |
|
'The names should match the individual endpoint names. Limited to 10 endpoints.' |
105
|
|
|
); |
106
|
|
|
} |
107
|
4 |
|
} |
108
|
|
|
} |
109
|
|
|
|