1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digitonic\IexCloudSdk\ReferenceData; |
4
|
|
|
|
5
|
|
|
use Digitonic\IexCloudSdk\Contracts\IEXCloud; |
6
|
|
|
use Digitonic\IexCloudSdk\Exceptions\WrongData; |
7
|
|
|
use Digitonic\IexCloudSdk\Requests\BaseRequest; |
8
|
|
|
|
9
|
|
|
class USHolTradeDates extends BaseRequest |
10
|
|
|
{ |
11
|
|
|
const ENDPOINT = 'ref-data/us/dates/{type}/{direction}/{last}/{startDate}'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $direction = 'next'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $type = 'trade'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
protected $last = 1; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $startDate = ''; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Create constructor. |
35
|
|
|
* |
36
|
|
|
* @param IEXCloud $api |
37
|
|
|
*/ |
38
|
8 |
|
public function __construct(IEXCloud $api) |
39
|
|
|
{ |
40
|
8 |
|
parent::__construct($api); |
41
|
8 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $type |
45
|
|
|
* |
46
|
|
|
* @return USHolTradeDates |
47
|
|
|
*/ |
48
|
4 |
|
public function setType(string $type): self |
49
|
|
|
{ |
50
|
4 |
|
$this->validateInput($type, '/(trade|holiday)/mi', 'Type can be trade or holiday'); |
51
|
|
|
|
52
|
3 |
|
$this->type = $type; |
53
|
|
|
|
54
|
3 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $direction |
59
|
|
|
* |
60
|
|
|
* @return USHolTradeDates |
61
|
|
|
*/ |
62
|
2 |
|
public function setDirection(string $direction): self |
63
|
|
|
{ |
64
|
2 |
|
$this->validateInput($direction, '/(next|last)/mi', 'Direction can be next or last'); |
65
|
|
|
|
66
|
1 |
|
$this->direction = $direction; |
67
|
|
|
|
68
|
1 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $last |
73
|
|
|
* |
74
|
|
|
* @return USHolTradeDates |
75
|
|
|
*/ |
76
|
1 |
|
public function setLast(string $last): self |
77
|
|
|
{ |
78
|
1 |
|
$this->last = (int) $last; |
79
|
|
|
|
80
|
1 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $startDate |
85
|
|
|
* |
86
|
|
|
* @return USHolTradeDates |
87
|
|
|
*/ |
88
|
2 |
|
public function setStartDate(string $startDate): self |
89
|
|
|
{ |
90
|
2 |
|
$this->validateInput( |
91
|
2 |
|
$startDate, |
92
|
2 |
|
'/(20\d{2})(\d{2})(\d{2})/mi', |
93
|
2 |
|
'Format date to use YYYYMMDD to fetch sentiment data.' |
94
|
|
|
); |
95
|
|
|
|
96
|
1 |
|
$this->startDate = $startDate; |
97
|
|
|
|
98
|
1 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
4 |
|
protected function getFullEndpoint(): string |
105
|
|
|
{ |
106
|
4 |
|
return self::ENDPOINT; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $input |
111
|
|
|
* @param string $pattern |
112
|
|
|
* @param string $errorMessage |
113
|
|
|
*/ |
114
|
7 |
View Code Duplication |
private function validateInput(string $input, string $pattern, string $errorMessage): void |
|
|
|
|
115
|
|
|
{ |
116
|
7 |
|
preg_match_all($pattern, $input, $matches, PREG_SET_ORDER, 0); |
117
|
|
|
|
118
|
7 |
|
if (empty($matches)) { |
119
|
3 |
|
throw WrongData::invalidValuesProvided($errorMessage); |
120
|
|
|
} |
121
|
4 |
|
} |
122
|
|
|
} |
123
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.