Skip to content

Commit 45789fe

Browse files
committed
feat: added buildPath helper method
1 parent cd5ddb0 commit 45789fe

2 files changed

Lines changed: 40 additions & 14 deletions

File tree

src/Api.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,10 @@ protected function request(
6767
throw new ConfigException('A base URL must be set.');
6868
}
6969

70-
// merge and overwrite query defaults
7170
if (!empty($this->queryDefaults)) {
7271
$query = array_merge($this->queryDefaults, $query);
7372
}
7473

75-
// merge and overwrite header defaults
7674
if (!empty($this->headerDefaults)) {
7775
$headers = array_merge($this->headerDefaults, $headers);
7876
}
@@ -123,7 +121,7 @@ protected function request(
123121
);
124122
}
125123

126-
$uri = $this->createUri($path, $query);
124+
$uri = $this->buildUri($path, $query);
127125
$request = $this->createRequest($method, $uri, $headers, $body);
128126

129127
$response = $this->clientBuilder->getClient()->sendRequest($request);
@@ -247,6 +245,30 @@ protected function addResponseHandler(callable $handler, int $priority = 0): sel
247245
return $this;
248246
}
249247

248+
protected function buildPath(string $path, array $parameters): string
249+
{
250+
foreach ($parameters as $parameter => $value) {
251+
$path = \str_replace(
252+
\sprintf('{%s}', $parameter),
253+
$value,
254+
$path
255+
);
256+
}
257+
258+
return $path;
259+
}
260+
261+
private function buildUri(string $path, array $query = []): string
262+
{
263+
$uri = $this->reduceDuplicateSlashes($this->baseUrl . $path);
264+
265+
if (!empty($query)) {
266+
$uri = \sprintf('%s?%s', $uri, \http_build_query($query));
267+
}
268+
269+
return $uri;
270+
}
271+
250272
private function createRequest(
251273
string $method,
252274
string $uri,
@@ -268,15 +290,4 @@ private function createRequest(
268290

269291
return $request;
270292
}
271-
272-
private function createUri(string $path, array $query = []): string
273-
{
274-
$uri = $this->reduceDuplicateSlashes($this->baseUrl . $path);
275-
276-
if (!empty($query)) {
277-
$uri = \sprintf('%s?%s', $uri, \http_build_query($query));
278-
}
279-
280-
return $uri;
281-
}
282293
}

tests/Integration/ApiTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ public function addResponseHandler(callable $handler, int $priority = 0): Api
102102
{
103103
return parent::addResponseHandler($handler, $priority);
104104
}
105+
106+
public function buildPath(string $path, array $parameters): string
107+
{
108+
return parent::buildPath($path, $parameters);
109+
}
105110
};
106111

107112
// set mock client
@@ -273,4 +278,14 @@ public function testResponseHandler()
273278

274279
$this->assertIsArray($response);
275280
}
281+
282+
public function testBuildPath()
283+
{
284+
$path = $this->class->buildPath('/path/{parameter1}/multiple/{parameter2}', [
285+
'parameter1' => 'with',
286+
'parameter2' => 'parameters'
287+
]);
288+
289+
$this->assertSame('/path/with/multiple/parameters', $path);
290+
}
276291
}

0 commit comments

Comments
 (0)