diff --git a/src/Alias.php b/src/Alias.php index 6f1e21d4..9c1560f6 100644 --- a/src/Alias.php +++ b/src/Alias.php @@ -41,7 +41,7 @@ public function __construct(string $name, ApiCall $apiCall) */ public function endPointPath(): string { - return sprintf('%s/%s', Aliases::RESOURCE_PATH, $this->name); + return sprintf('%s/%s', Aliases::RESOURCE_PATH, encodeURIComponent($this->name)); } /** diff --git a/src/Aliases.php b/src/Aliases.php index 70b9798a..157fbc49 100644 --- a/src/Aliases.php +++ b/src/Aliases.php @@ -43,7 +43,7 @@ public function __construct(ApiCall $apiCall) */ public function endPointPath(string $aliasName): string { - return sprintf('%s/%s', static::RESOURCE_PATH, $aliasName); + return sprintf('%s/%s', static::RESOURCE_PATH, encodeURIComponent($aliasName)); } /** diff --git a/src/AnalyticsRule.php b/src/AnalyticsRule.php index 9ea6f3f5..a574e301 100644 --- a/src/AnalyticsRule.php +++ b/src/AnalyticsRule.php @@ -25,6 +25,6 @@ public function delete() private function endpointPath() { - return AnalyticsRules::RESOURCE_PATH . '/' . $this->ruleName; + return AnalyticsRules::RESOURCE_PATH . '/' . encodeURIComponent($this->ruleName); } } diff --git a/src/AnalyticsRules.php b/src/AnalyticsRules.php index 412aea0d..205f7d4f 100644 --- a/src/AnalyticsRules.php +++ b/src/AnalyticsRules.php @@ -34,6 +34,6 @@ public function retrieve() private function endpoint_path($operation = null) { - return self::RESOURCE_PATH . ($operation === null ? '' : "/$operation"); + return self::RESOURCE_PATH . ($operation === null ? '' : "/" . encodeURIComponent($operation)); } } diff --git a/src/Client.php b/src/Client.php index df2a7a2f..086fa493 100644 --- a/src/Client.php +++ b/src/Client.php @@ -5,6 +5,7 @@ use Typesense\Exceptions\ConfigError; use Typesense\Lib\Configuration; +include('utils/utils.php'); /** * Class Client * diff --git a/src/Collection.php b/src/Collection.php index 786c0e9e..bc859663 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -64,7 +64,7 @@ public function __construct(string $name, ApiCall $apiCall) */ public function endPointPath(): string { - return sprintf('%s/%s', Collections::RESOURCE_PATH, $this->name); + return sprintf('%s/%s', Collections::RESOURCE_PATH, encodeURIComponent($this->name)); } /** diff --git a/src/Conversation.php b/src/Conversation.php index 855368b0..fa384c48 100644 --- a/src/Conversation.php +++ b/src/Conversation.php @@ -68,6 +68,6 @@ public function delete(): array */ public function endPointPath(): string { - return sprintf('%s/%s', Conversations::RESOURCE_PATH, $this->id); + return sprintf('%s/%s', Conversations::RESOURCE_PATH, encodeURIComponent($this->id)); } } diff --git a/src/ConversationModel.php b/src/ConversationModel.php index 305fa39c..69ec03ee 100644 --- a/src/ConversationModel.php +++ b/src/ConversationModel.php @@ -68,6 +68,6 @@ public function delete(): array */ public function endPointPath(): string { - return sprintf('%s/%s', ConversationModels::RESOURCE_PATH, $this->id); + return sprintf('%s/%s', ConversationModels::RESOURCE_PATH, encodeURIComponent($this->id)); } } diff --git a/src/Document.php b/src/Document.php index ba970f68..5b3647d1 100644 --- a/src/Document.php +++ b/src/Document.php @@ -51,9 +51,9 @@ private function endpointPath(): string return sprintf( '%s/%s/%s/%s', Collections::RESOURCE_PATH, - $this->collectionName, + encodeURIComponent($this->collectionName), Documents::RESOURCE_PATH, - $this->documentId + encodeURIComponent($this->documentId) ); } diff --git a/src/Documents.php b/src/Documents.php index 4c0c166c..09fc201d 100644 --- a/src/Documents.php +++ b/src/Documents.php @@ -53,7 +53,7 @@ private function endPointPath(string $action = ''): string return sprintf( '%s/%s/%s/%s', Collections::RESOURCE_PATH, - $this->collectionName, + encodeURIComponent($this->collectionName), static::RESOURCE_PATH, $action ); @@ -125,7 +125,7 @@ public function createMany(array $documents, array $options = []): array { $this->apiCall->getLogger()->warning( "createMany is deprecated and will be removed in a future version. " . - "Use import instead, which now takes both an array of documents or a JSONL string of documents" + "Use import instead, which now takes both an array of documents or a JSONL string of documents" ); return $this->import($documents, $options); } diff --git a/src/Key.php b/src/Key.php index 2539de16..203cd2f2 100644 --- a/src/Key.php +++ b/src/Key.php @@ -41,7 +41,7 @@ public function __construct(string $keyId, ApiCall $apiCall) */ private function endpointPath(): string { - return sprintf('%s/%s', Keys::RESOURCE_PATH, $this->keyId); + return sprintf('%s/%s', Keys::RESOURCE_PATH, encodeURIComponent($this->keyId)); } /** diff --git a/src/Override.php b/src/Override.php index 6061f5bb..cf75f28e 100644 --- a/src/Override.php +++ b/src/Override.php @@ -51,9 +51,9 @@ private function endPointPath(): string return sprintf( '%s/%s/%s/%s', Collections::RESOURCE_PATH, - $this->collectionName, + encodeURIComponent($this->collectionName), Overrides::RESOURCE_PATH, - $this->overrideId + encodeURIComponent($this->overrideId) ); } diff --git a/src/Overrides.php b/src/Overrides.php index 46de7b6d..6c15daec 100644 --- a/src/Overrides.php +++ b/src/Overrides.php @@ -53,9 +53,9 @@ public function endPointPath(string $overrideId = ''): string return sprintf( '%s/%s/%s/%s', Collections::RESOURCE_PATH, - $this->collectionName, + encodeURIComponent($this->collectionName), static::RESOURCE_PATH, - $overrideId + encodeURIComponent($overrideId) ); } diff --git a/src/Presets.php b/src/Presets.php index 29edc84c..cf8cfb46 100644 --- a/src/Presets.php +++ b/src/Presets.php @@ -89,7 +89,7 @@ private function endpointPath($presetsName) return sprintf( '%s/%s', static::PRESETS_PATH, - $presetsName + encodeURIComponent($presetsName) ); } diff --git a/src/Stopwords.php b/src/Stopwords.php index 116c14cc..3afe0b7a 100644 --- a/src/Stopwords.php +++ b/src/Stopwords.php @@ -88,7 +88,7 @@ private function endpointPath($stopwordsName) return sprintf( '%s/%s', static::STOPWORDS_PATH, - $stopwordsName + encodeURIComponent($stopwordsName) ); } } diff --git a/src/Synonym.php b/src/Synonym.php index 0857423a..3b11fa4e 100644 --- a/src/Synonym.php +++ b/src/Synonym.php @@ -49,9 +49,9 @@ private function endPointPath(): string return sprintf( '%s/%s/%s/%s', Collections::RESOURCE_PATH, - $this->collectionName, + encodeURIComponent($this->collectionName), synonyms::RESOURCE_PATH, - $this->synonymId + encodeURIComponent($this->synonymId) ); } diff --git a/src/Synonyms.php b/src/Synonyms.php index 57c8d1a9..a4c00364 100644 --- a/src/Synonyms.php +++ b/src/Synonyms.php @@ -51,9 +51,9 @@ public function endPointPath(string $synonymId = ''): string return sprintf( '%s/%s/%s/%s', Collections::RESOURCE_PATH, - $this->collectionName, + encodeURIComponent($this->collectionName), static::RESOURCE_PATH, - $synonymId + encodeURIComponent($synonymId) ); } diff --git a/src/utils/utils.php b/src/utils/utils.php new file mode 100644 index 00000000..219f8584 --- /dev/null +++ b/src/utils/utils.php @@ -0,0 +1,8 @@ + '!', '%2A' => '*', '%27' => "'", '%28' => '(', '%29' => ')'); + return strtr(rawurlencode($str), $revert); +} diff --git a/tests/Feature/AliasesTest.php b/tests/Feature/AliasesTest.php index a1e1a5d8..6d7cccb8 100644 --- a/tests/Feature/AliasesTest.php +++ b/tests/Feature/AliasesTest.php @@ -36,6 +36,15 @@ public function testCanUpsertAnAlias(): void $this->assertEquals($this->sampleAliasResponse, $this->upsertResponse); } + public function testCanUpsertAnAliasUrlEncodedName(): void + { + $aliasedCollection = [ + 'collection_name' => 'companies_june11' + ]; + $res = $this->client()->aliases->upsert("abc123?=+/~-_- &| test'", $aliasedCollection); + $this->assertEquals(["name"=>"abc123?=+/~-_- &| test'", ...$aliasedCollection], $res); + } + public function testCanRetrieveAlias(): void { $response = $this->client()->aliases['companies']->retrieve();