Skip to content

Commit e13da9b

Browse files
committed
Replace Stringy->slugify with a less destructive method
1 parent b17dcb9 commit e13da9b

6 files changed

+19
-16
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ Please see the following table for some examples.
4545
| | | /path/to/fixtures/example.com/api/articles/1.mock |
4646
| http://example.com/api/articles | POST | /path/to/fixtures/example.com/api/articles.post.mock |
4747
| | | /path/to/fixtures/example.com/api/articles.mock |
48-
| http://example.com/api/comments?query=json | GET | /path/to/fixtures/example.com/api/comments.query-json.get.mock |
49-
| | | /path/to/fixtures/example.com/api/comments.query-json.mock |
48+
| http://example.com/api/comments?query=json | GET | /path/to/fixtures/example.com/api/comments.query=json.get.mock |
49+
| | | /path/to/fixtures/example.com/api/comments.query=json.mock |
5050
| | | /path/to/fixtures/example.com/api/comments.get.mock |
5151
| | | /path/to/fixtures/example.com/api/comments.mock |
52-
| http://example.com/api/comments?query=json&order=id | GET | /path/to/fixtures/example.com/api/comments.order-id-query-json.get.mock |
53-
| | | /path/to/fixtures/example.com/api/comments.order-id-query-json.mock |
52+
| http://example.com/api/comments?query=json&order=id | GET | /path/to/fixtures/example.com/api/comments.order=id&query=json.get.mock |
53+
| | | /path/to/fixtures/example.com/api/comments.order=id&query=json.mock |
5454
| | | /path/to/fixtures/example.com/api/comments.get.mock |
5555
| | | /path/to/fixtures/example.com/api/comments.mock |
5656

src/ResponseBuilder.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -225,22 +225,22 @@ protected function getMethodFromRequest(RequestInterface $request): string
225225

226226
/**
227227
* @param \Psr\Http\Message\RequestInterface $request
228-
* @param string $separator
228+
* @param string $replacement
229229
*
230230
* @return string
231231
*/
232-
protected function getQueryFromRequest(RequestInterface $request, $separator = '-'): string
232+
protected function getQueryFromRequest(RequestInterface $request, $replacement = '-'): string
233233
{
234234
$query = urldecode($request->getUri()->getQuery());
235-
$parts = array_map(
236-
function (string $part) use ($separator) {
237-
return str_replace('=', $separator, $part);
238-
},
239-
explode('&', $query)
240-
);
235+
$parts = explode('&', $query);
241236
sort($parts);
237+
$query = implode('&', $parts);
242238

243-
return Stringy::create(implode($separator, $parts))->slugify($separator);
239+
return (string)Stringy::create(str_replace(['\\', '/', '?', ':', '*', '"', '>', '<', '|'], $replacement, $query))
240+
->toAscii()
241+
->delimit($replacement)
242+
->removeLeft($replacement)
243+
->removeRight($replacement);
244244
}
245245

246246
/**

tests/ResponseBuilderTest.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,18 @@ public function getResponses(): array
4545
['http://example.com/api/articles', 'GET', 'example.com/api/articles.mock'],
4646
// Nested
4747
['http://example.com/api/articles/1', 'GET', 'example.com/api/articles/1.mock'],
48-
// With query
49-
['http://example.com/api/comments?query=json', 'GET', 'example.com/api/comments.query-json.mock'],
48+
// With simple query
49+
['http://example.com/api/comments?query=json', 'GET', 'example.com/api/comments.query=json.mock'],
50+
// With complex query
51+
['http://example.com/api/comments?query=json&foo[]=bar&foo[]=baz', 'GET', 'example.com/api/comments.foo[]=bar&foo[]=baz&query=json.mock'],
5052
// With query fallback
5153
['http://example.com/api/comments?foo=bar', 'GET', 'example.com/api/comments.mock'],
5254
// With method
5355
['http://example.com/api/people', 'GET', 'example.com/api/people.get.mock'],
5456
// With method fallback
5557
['http://example.com/api/people', 'POST', 'example.com/api/people.mock'],
5658
// With query and method
57-
['http://example.com/api/tags?query=json', 'POST', 'example.com/api/tags.query-json.post.mock'],
59+
['http://example.com/api/tags?query=json', 'POST', 'example.com/api/tags.query=json.post.mock'],
5860
// With query and method fallback
5961
['http://example.com/api/tags?foo=bar', 'GET', 'example.com/api/tags.mock'],
6062
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
comments-foo-bar-foo-baz-query-json

0 commit comments

Comments
 (0)