Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(string $serviceDomain, string $apiKey, ?\GuzzleHttp\
*/
public function list(string $endpoint, array $options = [])
{
$path = $endpoint;
$path = $this->buildPath($endpoint);
$response = $this->client->get(
$path,
$this->buildOption([
Expand All @@ -45,7 +45,7 @@ public function list(string $endpoint, array $options = [])
*/
public function get(string $endpoint, string $contentId = "", array $options = [])
{
$path = $contentId ? implode("/", [$endpoint, $contentId]) : $endpoint;
$path = $contentId ? $this->buildPath($endpoint, $contentId) : $this->buildPath($endpoint);
$response = $this->client->get(
$path,
$this->buildOption([
Expand All @@ -65,10 +65,10 @@ public function create(string $endpoint, array $body = [], array $options = [])
{
if (array_key_exists("id", $body)) {
$method = "PUT";
$path = implode("/", [$endpoint, $body["id"]]);
$path = $this->buildPath($endpoint, $body["id"]);
} else {
$method = "POST";
$path = $endpoint;
$path = $this->buildPath($endpoint);
}

$response = $this->client->request(
Expand All @@ -90,7 +90,7 @@ public function create(string $endpoint, array $body = [], array $options = [])
public function update(string $endpoint, array $body = [])
{
$response = $this->client->patch(
array_key_exists("id", $body) ? implode("/", [$endpoint, $body["id"]]) : $endpoint,
array_key_exists("id", $body) ? $this->buildPath($endpoint, $body["id"]) : $this->buildPath($endpoint),
$this->buildOption([
"json" => array_filter(
$body,
Expand All @@ -106,10 +106,30 @@ function ($v, $k) {

public function delete(string $endpoint, string $id): void
{
$path = implode("/", [$endpoint, $id]);
$path = $this->buildPath($endpoint, $id);
$this->client->delete($path, $this->buildOption());
}

/**
* Endpoint names and content ids are each a single path segment. Anything
* outside this alphabet could add segments, dot segments, a query or an
* absolute URL, and send the API key to a different API or host.
*
* @param mixed ...$segments
*/
private function buildPath(...$segments): string
{
foreach ($segments as $segment) {
if (is_int($segment)) {
$segment = (string) $segment;
}
if (!is_string($segment) || preg_match('/\A[A-Za-z0-9_-]+\z/', $segment) !== 1) {
throw new \InvalidArgumentException('Invalid endpoint or content id');
}
}
return implode("/", $segments);
}

/**
* @param array<string, mixed> $option
*
Expand Down