diff --git a/src/DefaultRestClient.php b/src/DefaultRestClient.php index 20ca75b..717954e 100644 --- a/src/DefaultRestClient.php +++ b/src/DefaultRestClient.php @@ -33,41 +33,49 @@ public function __construct( ) { } + /** @throws ExpectedBaseUri */ public function get(string|null $uri = null, array|null $uriVariables = null): RequestHeadersSpec { return $this->createHeadersSpec('GET', $this->createUriFactory($uri, $uriVariables)); } + /** @throws ExpectedBaseUri */ public function head(string|null $uri = null, array|null $uriVariables = null): RequestHeadersSpec { return $this->createHeadersSpec('HEAD', $this->createUriFactory($uri, $uriVariables)); } + /** @throws ExpectedBaseUri */ public function post(string|null $uri = null, array|null $uriVariables = null): RequestBodyHeadersSpec { return $this->createBodyHeadersSpec('POST', $this->createUriFactory($uri, $uriVariables)); } + /** @throws ExpectedBaseUri */ public function put(string|null $uri = null, array|null $uriVariables = null): RequestBodyHeadersSpec { return $this->createBodyHeadersSpec('PUT', $this->createUriFactory($uri, $uriVariables)); } + /** @throws ExpectedBaseUri */ public function patch(string|null $uri = null, array|null $uriVariables = null): RequestBodyHeadersSpec { return $this->createBodyHeadersSpec('PATCH', $this->createUriFactory($uri, $uriVariables)); } + /** @throws ExpectedBaseUri */ public function delete(string|null $uri = null, array|null $uriVariables = null): RequestHeadersSpec { return $this->createHeadersSpec('DELETE', $this->createUriFactory($uri, $uriVariables)); } + /** @throws ExpectedBaseUri */ public function options(string|null $uri = null, array|null $uriVariables = null): RequestHeadersSpec { return $this->createHeadersSpec('OPTIONS', $this->createUriFactory($uri, $uriVariables)); } + /** @param non-empty-string $method */ protected function createHeadersSpec(string $method, UriFactory $uriFactory): RequestHeadersSpec { return new RequestHeadersSpec( @@ -80,6 +88,7 @@ protected function createHeadersSpec(string $method, UriFactory $uriFactory): Re ); } + /** @param non-empty-string $method */ protected function createBodyHeadersSpec(string $method, UriFactory $uriFactory): RequestBodyHeadersSpec { return new RequestBodyHeadersSpec( @@ -92,10 +101,15 @@ protected function createBodyHeadersSpec(string $method, UriFactory $uriFactory) ); } + /** @throws ExpectedBaseUri */ protected function createUriFactory(string|null $uri, array|null $uriVariables): UriFactory { - if ($this->baseUrl) { - if (str_ends_with($this->baseUrl, '/') && str_starts_with($uri, '/')) { + if ($this->baseUrl === null && $uri === null) { + throw new ExpectedBaseUri('Not passing Uri if no baseUri defined on client is not allowed'); + } + + if (! empty($this->baseUrl)) { + if (str_ends_with($this->baseUrl, '/') && $uri && str_starts_with($uri, '/')) { $uri = ltrim($uri, '/'); } diff --git a/src/DefaultRestClientBuilder.php b/src/DefaultRestClientBuilder.php index 3853ed1..5f14783 100644 --- a/src/DefaultRestClientBuilder.php +++ b/src/DefaultRestClientBuilder.php @@ -37,7 +37,7 @@ public function __construct( ) { } - public function baseUrl(string $url): static + public function baseUrl(string|null $url): static { $this->baseUrl = $url; diff --git a/src/ExpectedBaseUri.php b/src/ExpectedBaseUri.php new file mode 100644 index 0000000..1e0493a --- /dev/null +++ b/src/ExpectedBaseUri.php @@ -0,0 +1,11 @@ +