Skip to content

Commit 1491b94

Browse files
authored
Merge pull request #36 from Tailchakra/main
Fixing history according to issue #35
2 parents 524d467 + b095ae3 commit 1491b94

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/StationClient.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function backend(string $action): void
114114
));
115115
}
116116

117-
/**
117+
/**
118118
* @param DateTime|null $start
119119
* @param DateTime|null $end
120120
*
@@ -123,17 +123,36 @@ public function backend(string $action): void
123123
* @throws Exception\AccessDeniedException
124124
* @throws Exception\ClientRequestException
125125
*/
126+
126127
public function history(?DateTime $start = null, ?DateTime $end = null): array
127128
{
128-
$songHistoryDataArray = $this->request('GET', sprintf(
129-
'station/%s/history',
130-
$this->stationId
131-
));
129+
$queryParams = [];
130+
131+
// Only add "start" if we actually have a DateTime object
132+
if ($start instanceof DateTime) {
133+
// Adjust the format string to what your API expects
134+
$queryParams['start'] = $start->format('Y-m-d H:i:s');
135+
}
136+
137+
// Only add "end" if we actually have a DateTime object
138+
if ($end instanceof DateTime) {
139+
$queryParams['end'] = $end->format('Y-m-d H:i:s');
140+
}
141+
142+
$queryString = http_build_query($queryParams);
143+
144+
$endpoint = sprintf('station/%s/history', $this->stationId);
145+
if (!empty($queryString)) {
146+
$endpoint .= '?' . $queryString;
147+
}
148+
149+
$songHistoryDataArray = $this->request('GET', $endpoint);
132150

133151
$songHistory = [];
134152
foreach ($songHistoryDataArray as $songHistoryData) {
135153
$songHistory[] = Dto\SongHistoryDto::fromArray($songHistoryData);
136154
}
155+
137156
return $songHistory;
138157
}
139158

0 commit comments

Comments
 (0)