From e13da9b0ed3f53a8748dbe626aa028d1b98e1b0a Mon Sep 17 00:00:00 2001 From: Jasper Zonneveld Date: Wed, 5 Sep 2018 14:31:49 +0200 Subject: [PATCH] Replace Stringy->slugify with a less destructive method --- README.md | 8 ++++---- src/ResponseBuilder.php | 18 +++++++++--------- tests/ResponseBuilderTest.php | 8 +++++--- ...omments.foo[]=bar&foo[]=baz&query=json.mock | 1 + ...uery-json.mock => comments.query=json.mock} | 0 ...son.post.mock => tags.query=json.post.mock} | 0 6 files changed, 19 insertions(+), 16 deletions(-) create mode 100644 tests/_fixtures/example.com/api/comments.foo[]=bar&foo[]=baz&query=json.mock rename tests/_fixtures/example.com/api/{comments.query-json.mock => comments.query=json.mock} (100%) rename tests/_fixtures/example.com/api/{tags.query-json.post.mock => tags.query=json.post.mock} (100%) diff --git a/README.md b/README.md index 63d567c..9fe0703 100644 --- a/README.md +++ b/README.md @@ -45,12 +45,12 @@ Please see the following table for some examples. | | | /path/to/fixtures/example.com/api/articles/1.mock | | http://example.com/api/articles | POST | /path/to/fixtures/example.com/api/articles.post.mock | | | | /path/to/fixtures/example.com/api/articles.mock | -| http://example.com/api/comments?query=json | GET | /path/to/fixtures/example.com/api/comments.query-json.get.mock | -| | | /path/to/fixtures/example.com/api/comments.query-json.mock | +| http://example.com/api/comments?query=json | GET | /path/to/fixtures/example.com/api/comments.query=json.get.mock | +| | | /path/to/fixtures/example.com/api/comments.query=json.mock | | | | /path/to/fixtures/example.com/api/comments.get.mock | | | | /path/to/fixtures/example.com/api/comments.mock | -| http://example.com/api/comments?query=json&order=id | GET | /path/to/fixtures/example.com/api/comments.order-id-query-json.get.mock | -| | | /path/to/fixtures/example.com/api/comments.order-id-query-json.mock | +| http://example.com/api/comments?query=json&order=id | GET | /path/to/fixtures/example.com/api/comments.order=id&query=json.get.mock | +| | | /path/to/fixtures/example.com/api/comments.order=id&query=json.mock | | | | /path/to/fixtures/example.com/api/comments.get.mock | | | | /path/to/fixtures/example.com/api/comments.mock | diff --git a/src/ResponseBuilder.php b/src/ResponseBuilder.php index 6c7fcf1..3c08609 100644 --- a/src/ResponseBuilder.php +++ b/src/ResponseBuilder.php @@ -225,22 +225,22 @@ protected function getMethodFromRequest(RequestInterface $request): string /** * @param \Psr\Http\Message\RequestInterface $request - * @param string $separator + * @param string $replacement * * @return string */ - protected function getQueryFromRequest(RequestInterface $request, $separator = '-'): string + protected function getQueryFromRequest(RequestInterface $request, $replacement = '-'): string { $query = urldecode($request->getUri()->getQuery()); - $parts = array_map( - function (string $part) use ($separator) { - return str_replace('=', $separator, $part); - }, - explode('&', $query) - ); + $parts = explode('&', $query); sort($parts); + $query = implode('&', $parts); - return Stringy::create(implode($separator, $parts))->slugify($separator); + return (string)Stringy::create(str_replace(['\\', '/', '?', ':', '*', '"', '>', '<', '|'], $replacement, $query)) + ->toAscii() + ->delimit($replacement) + ->removeLeft($replacement) + ->removeRight($replacement); } /** diff --git a/tests/ResponseBuilderTest.php b/tests/ResponseBuilderTest.php index 3735cce..c8f4518 100644 --- a/tests/ResponseBuilderTest.php +++ b/tests/ResponseBuilderTest.php @@ -45,8 +45,10 @@ public function getResponses(): array ['http://example.com/api/articles', 'GET', 'example.com/api/articles.mock'], // Nested ['http://example.com/api/articles/1', 'GET', 'example.com/api/articles/1.mock'], - // With query - ['http://example.com/api/comments?query=json', 'GET', 'example.com/api/comments.query-json.mock'], + // With simple query + ['http://example.com/api/comments?query=json', 'GET', 'example.com/api/comments.query=json.mock'], + // With complex query + ['http://example.com/api/comments?query=json&foo[]=bar&foo[]=baz', 'GET', 'example.com/api/comments.foo[]=bar&foo[]=baz&query=json.mock'], // With query fallback ['http://example.com/api/comments?foo=bar', 'GET', 'example.com/api/comments.mock'], // With method @@ -54,7 +56,7 @@ public function getResponses(): array // With method fallback ['http://example.com/api/people', 'POST', 'example.com/api/people.mock'], // With query and method - ['http://example.com/api/tags?query=json', 'POST', 'example.com/api/tags.query-json.post.mock'], + ['http://example.com/api/tags?query=json', 'POST', 'example.com/api/tags.query=json.post.mock'], // With query and method fallback ['http://example.com/api/tags?foo=bar', 'GET', 'example.com/api/tags.mock'], ]; diff --git a/tests/_fixtures/example.com/api/comments.foo[]=bar&foo[]=baz&query=json.mock b/tests/_fixtures/example.com/api/comments.foo[]=bar&foo[]=baz&query=json.mock new file mode 100644 index 0000000..de9ef75 --- /dev/null +++ b/tests/_fixtures/example.com/api/comments.foo[]=bar&foo[]=baz&query=json.mock @@ -0,0 +1 @@ +comments-foo-bar-foo-baz-query-json diff --git a/tests/_fixtures/example.com/api/comments.query-json.mock b/tests/_fixtures/example.com/api/comments.query=json.mock similarity index 100% rename from tests/_fixtures/example.com/api/comments.query-json.mock rename to tests/_fixtures/example.com/api/comments.query=json.mock diff --git a/tests/_fixtures/example.com/api/tags.query-json.post.mock b/tests/_fixtures/example.com/api/tags.query=json.post.mock similarity index 100% rename from tests/_fixtures/example.com/api/tags.query-json.post.mock rename to tests/_fixtures/example.com/api/tags.query=json.post.mock