Skip to content

Commit

Permalink
Expose default strategy for URL normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Jan 8, 2023
1 parent 7cdf390 commit 8fe91d8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
5 changes: 3 additions & 2 deletions camel/Extraction/ExtractedEndpointData.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,11 @@ public function __construct(array $parameters = [])

parent::__construct($parameters);

$defaultNormalizer = fn() => UrlParamsNormalizer::normalizeParameterNamesInRouteUri($this->route, $this->method);
$this->uri = match (is_callable(Globals::$__normalizeEndpointUrlUsing)) {
true => call_user_func_array(Globals::$__normalizeEndpointUrlUsing,
[$this->route->uri, $this->route, $this->method, $this->controller]),
default => UrlParamsNormalizer::normalizeParameterNamesInRouteUri($this->route, $this->method),
[$this->route->uri, $this->route, $this->method, $this->controller, $defaultNormalizer]),
default => $defaultNormalizer(),
};
}

Expand Down
25 changes: 24 additions & 1 deletion tests/Unit/ExtractedEndpointDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function normalizes_resource_url_params()
/** @test */
public function allows_user_specified_normalization()
{
Scribe::normalizeEndpointUrlUsing(function (string $url, LaravelRoute $route, \ReflectionFunctionAbstract $method, ?\ReflectionClass $controller) {
Scribe::normalizeEndpointUrlUsing(function (string $url, LaravelRoute $route) {
if ($url == 'things/{thing}') return 'things/{the_id_of_the_thing}';

if ($route->named('things.otherthings.destroy')) return 'things/{thing-id}/otherthings/{other_thing-id}';
Expand All @@ -51,6 +51,29 @@ public function allows_user_specified_normalization()
Scribe::normalizeEndpointUrlUsing(null);
}

/** @test */
public function allows_user_specified_normalization_fallback_to_default()
{
Scribe::normalizeEndpointUrlUsing(function (string $url, LaravelRoute $route,
\ReflectionFunctionAbstract $method, ?\ReflectionClass $controller, callable $default) {
if ($route->named('things.otherthings.destroy')) return 'things/{thing-id}/otherthings/{other_thing-id}';

return $default();
});

Route::apiResource('things', TestController::class)->only('show');
$route = $this->getRoute(['prefixes' => '*']);
$this->assertEquals('things/{thing}', $this->originalUri($route));
$this->assertEquals('things/{id}', $this->expectedUri($route));

Route::apiResource('things.otherthings', TestController::class)->only('destroy');
$route = $this->getRoute(['prefixes' => '*/otherthings/*']);
$this->assertEquals('things/{thing}/otherthings/{otherthing}', $this->originalUri($route));
$this->assertEquals('things/{thing-id}/otherthings/{other_thing-id}', $this->expectedUri($route));

Scribe::normalizeEndpointUrlUsing(null);
}

/** @test */
public function normalizes_resource_url_params_from_underscores_to_hyphens()
{
Expand Down

0 comments on commit 8fe91d8

Please sign in to comment.