From ae276aeebb69573c7697d707c01db815b0d86c62 Mon Sep 17 00:00:00 2001 From: Bas Date: Tue, 26 Nov 2024 00:18:07 +0100 Subject: [PATCH] Added deleteAllAnalyzers to SchemaManager. --- docs/schema-analyzers.md | 9 +++++++++ src/Schema/ManagesAnalyzers.php | 28 ++++++++++++++++++++++++++++ tests/SchemaManagerAnalyzersTest.php | 20 ++++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/docs/schema-analyzers.md b/docs/schema-analyzers.md index b7f9d22..e901b84 100644 --- a/docs/schema-analyzers.md +++ b/docs/schema-analyzers.md @@ -37,7 +37,16 @@ $arangoClient->schema()->replaceAnalyzer('myAnalyzer', [ ``` ### deleteAnalyzer(string $name): bool +Delete the analyzer by its name. + ``` $arangoClient->schema()->deleteAnalyzer('myAnalyzer'); ``` +### deleteAllAnalyzers(): bool +This method deletes all custom analyzers available on the current database. + +``` +$arangoClient->schema()->deleteAllAnalyzers(); +``` + diff --git a/src/Schema/ManagesAnalyzers.php b/src/Schema/ManagesAnalyzers.php index 81272db..581d37f 100644 --- a/src/Schema/ManagesAnalyzers.php +++ b/src/Schema/ManagesAnalyzers.php @@ -53,6 +53,34 @@ public function deleteAnalyzer(string $name): bool return (bool) $this->arangoClient->request('delete', $uri); } + /** + * Removes all custom analyzes defined for the current database. + * + * @see https://docs.arangodb.com/stable/develop/http-api/analyzers/#remove-an-analyzer + * + * @throws ArangoException + */ + public function deleteAllAnalyzers(): bool + { + $analyzers = $this->getAnalyzers(); + + $database = $this->arangoClient->getDatabase(); + + foreach ($analyzers as $analyzer) { + if (!str_starts_with($analyzer->name, "$database::")) { + continue; + } + + $uri = '/_api/analyzer/' . $analyzer->name; + + $this->arangoClient->request('delete', $uri); + } + + return true; + } + + + /** * @see https://docs.arangodb.com/stable/develop/http-api/analyzers/#list-all-analyzers * diff --git a/tests/SchemaManagerAnalyzersTest.php b/tests/SchemaManagerAnalyzersTest.php index 0195370..68d2ed8 100644 --- a/tests/SchemaManagerAnalyzersTest.php +++ b/tests/SchemaManagerAnalyzersTest.php @@ -84,3 +84,23 @@ $hasAnalyzer = $this->schemaManager->hasAnalyzer($fullName); expect($hasAnalyzer)->toBeFalse(); }); + +test('deleteAllAnalyzers', function () { + $initialAnalyzers = $this->schemaManager->getAnalyzers(); + + $this->schemaManager->createAnalyzer([ + 'name' => 'customAnalyzer1', + 'type' => 'identity', + ]); + $this->schemaManager->createAnalyzer([ + 'name' => 'customAnalyzer2', + 'type' => 'identity', + ]); + + $this->schemaManager->deleteAllAnalyzers(); + + $endAnalyzers = $this->schemaManager->getAnalyzers(); + + // We already have an analyzer that is created before all tests. So, three analyzers are actually deleted. + expect(count($initialAnalyzers) - 1)->toBe(count($endAnalyzers)); +});