Skip to content

Commit 76c9d50

Browse files
Added deleteAllAnalyzers to SchemaManager. (#37)
1 parent 0eb3900 commit 76c9d50

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

docs/schema-analyzers.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,16 @@ $arangoClient->schema()->replaceAnalyzer('myAnalyzer', [
3737
```
3838

3939
### deleteAnalyzer(string $name): bool
40+
Delete the analyzer by its name.
41+
4042
```
4143
$arangoClient->schema()->deleteAnalyzer('myAnalyzer');
4244
```
4345

46+
### deleteAllAnalyzers(): bool
47+
This method deletes all custom analyzers available on the current database.
48+
49+
```
50+
$arangoClient->schema()->deleteAllAnalyzers();
51+
```
52+

src/Schema/ManagesAnalyzers.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,34 @@ public function deleteAnalyzer(string $name): bool
5353
return (bool) $this->arangoClient->request('delete', $uri);
5454
}
5555

56+
/**
57+
* Removes all custom analyzes defined for the current database.
58+
*
59+
* @see https://docs.arangodb.com/stable/develop/http-api/analyzers/#remove-an-analyzer
60+
*
61+
* @throws ArangoException
62+
*/
63+
public function deleteAllAnalyzers(): bool
64+
{
65+
$analyzers = $this->getAnalyzers();
66+
67+
$database = $this->arangoClient->getDatabase();
68+
69+
foreach ($analyzers as $analyzer) {
70+
if (!str_starts_with($analyzer->name, "$database::")) {
71+
continue;
72+
}
73+
74+
$uri = '/_api/analyzer/' . $analyzer->name;
75+
76+
$this->arangoClient->request('delete', $uri);
77+
}
78+
79+
return true;
80+
}
81+
82+
83+
5684
/**
5785
* @see https://docs.arangodb.com/stable/develop/http-api/analyzers/#list-all-analyzers
5886
*

tests/SchemaManagerAnalyzersTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,23 @@
8484
$hasAnalyzer = $this->schemaManager->hasAnalyzer($fullName);
8585
expect($hasAnalyzer)->toBeFalse();
8686
});
87+
88+
test('deleteAllAnalyzers', function () {
89+
$initialAnalyzers = $this->schemaManager->getAnalyzers();
90+
91+
$this->schemaManager->createAnalyzer([
92+
'name' => 'customAnalyzer1',
93+
'type' => 'identity',
94+
]);
95+
$this->schemaManager->createAnalyzer([
96+
'name' => 'customAnalyzer2',
97+
'type' => 'identity',
98+
]);
99+
100+
$this->schemaManager->deleteAllAnalyzers();
101+
102+
$endAnalyzers = $this->schemaManager->getAnalyzers();
103+
104+
// We already have an analyzer that is created before all tests. So, three analyzers are actually deleted.
105+
expect(count($initialAnalyzers) - 1)->toBe(count($endAnalyzers));
106+
});

0 commit comments

Comments
 (0)