Skip to content

Commit 8b9cd23

Browse files
committed
Api: substituteUrlParameters() replaced by expandColonParameters() [BC break]
1 parent 6f542cf commit 8b9cd23

3 files changed

Lines changed: 75 additions & 74 deletions

File tree

src/Github/Api.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,9 @@ public function request(Http\Request $request)
216216
*/
217217
public function createRequest($method, $urlPath, array $parameters = [], array $headers = [], $content = NULL)
218218
{
219-
$parameters += $this->defaultParameters;
220-
$this->substituteUrlParameters($urlPath, $parameters);
219+
$urlPath = $this->expandColonParameters($urlPath, $parameters, $this->defaultParameters);
221220

222-
$url = rtrim($this->url, '/') . '/' . trim($urlPath, '/');
223-
if (count($parameters)) {
224-
$url .= '?' . http_build_query($parameters);
225-
}
221+
$url = rtrim($this->url, '/') . '/' . ltrim($urlPath, '/');
226222

227223
if ($content !== NULL && (is_array($content) || is_object($content))) {
228224
$headers['Content-Type'] = 'application/json; charset=utf-8';
@@ -339,21 +335,30 @@ public function getUrl()
339335

340336
/**
341337
* @param string
342-
* @param array
338+
* @return string
343339
*
344340
* @throws MissingParameterException
345341
*/
346-
protected function substituteUrlParameters(& $url, array & $parameters)
342+
protected function expandColonParameters($url, array $parameters, array $defaultParameters)
347343
{
344+
$parameters += $defaultParameters;
345+
348346
$url = preg_replace_callback('#(^|/):([^/]+)#', function($m) use ($url, & $parameters) {
349347
if (!isset($parameters[$m[2]])) {
350348
throw new MissingParameterException("Missing parameter '$m[2]' for URL path '$url'.");
351349
}
352-
353350
$parameter = $parameters[$m[2]];
354351
unset($parameters[$m[2]]);
355352
return $m[1] . rawurlencode($parameter);
356353
}, $url);
354+
355+
$url = rtrim($url, '/');
356+
357+
if (count($parameters)) {
358+
$url .= '?' . http_build_query($parameters);
359+
}
360+
361+
return $url;
357362
}
358363

359364
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/**
4+
* @author Miloslav Hůla
5+
*/
6+
7+
require __DIR__ . '/../bootstrap.php';
8+
9+
10+
class TestApi extends Milo\Github\Api
11+
{
12+
public function expandColonParameters($url, array $parameters, array $defaultParameters = [])
13+
{
14+
return parent::expandColonParameters($url, $parameters, $defaultParameters);
15+
}
16+
}
17+
18+
19+
# URL parameters like :name
20+
test(function() {
21+
$api = new TestApi;
22+
23+
$urls = [
24+
'' => '?a=A&b=B',
25+
'/' => '?a=A&b=B',
26+
':a' => 'A?b=B',
27+
'/:a' => '/A?b=B',
28+
':a/' => 'A?b=B',
29+
'/:a/' => '/A?b=B',
30+
'/:a/:b/c' => '/A/B/c',
31+
];
32+
33+
foreach ($urls as $url => $expected) {
34+
Assert::same($expected, $api->expandColonParameters($url, ['a' => 'A', 'b' => 'B']));
35+
}
36+
37+
Assert::exception(function() use ($api) {
38+
$api->expandColonParameters(':a', ['A' => 'a']);
39+
}, 'Milo\Github\MissingParameterException', "Missing parameter 'a' for URL path ':a'.");
40+
41+
Assert::exception(function() use ($api) {
42+
$api->expandColonParameters(':a:b', ['a' => 'A', 'b' => 'B']);
43+
}, 'Milo\Github\MissingParameterException', "Missing parameter 'a:b' for URL path ':a:b'.");
44+
});
45+
46+
47+
# Parameters escaping
48+
test(function() {
49+
$api = new TestApi;
50+
51+
Assert::same('/with%20space', $api->expandColonParameters('/:name', ['name' => 'with space']));
52+
});
53+
54+
55+
# Default parameters expanding
56+
test(function() {
57+
$api = new TestApi;
58+
59+
Assert::same('/default', $api->expandColonParameters('/:var', [], ['var' => 'default']));
60+
Assert::same('/set', $api->expandColonParameters('/:var', ['var' => 'set'], ['var' => 'default']));
61+
});

tests/Github/Api.substituteUrlParameters.phpt

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)