Skip to content

Commit c0af081

Browse files
Code clean up
BREAKING CHANGE: fix: removed $databade argument from getDatabases() docs: expanded user schema documentation docs: expanded view schema documentation BREAKING CHANGE: refactor: added HttpRequestOptions DTO to better typehinting of request options. Requests now only support the query, headers, body and handlers options. refactor: improved jsonEncode code. tests: added tests some uncovered code chore: composer update to the latest dependencies.
1 parent d904acf commit c0af081

18 files changed

+424
-118
lines changed

composer.lock

Lines changed: 18 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/schema-collections.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,37 @@ Get the requested collection.
1616
$arangoClient->schema()->getCollection('_fishbowl');
1717
```
1818

19-
### getCollectionProperties(string $collection): array
19+
### getCollectionProperties(string $name): array
2020
Get the properties of the requested collection.
2121
```
2222
$arangoClient->schema()->getCollectionProperties('_fishbowl');
2323
```
2424

25-
### getCollectionWithDocumentCount(string $collection): array
25+
### getCollectionWithDocumentCount(string $name): array
2626
Get the properties of the requested collection.
2727
```
2828
$arangoClient->schema()->getCollectionWithDocumentCount('_fishbowl');
2929
```
3030

31-
### getCollectionDocumentCount(string $collection): int
31+
### getCollectionDocumentCount(string $name): int
3232
Get the number of documents within the requested collection.
3333
```
3434
$arangoClient->schema()->getCollectionDocumentCount('users');
3535
```
3636

37-
### getCollectionStatistics(string $collection, bool $details = false): array
37+
### getCollectionStatistics(string $name, bool $details = false): array
3838
Get the properties of the requested collection.
3939
```
4040
$arangoClient->schema()->getCollectionStatistics('_fishbowl');
4141
```
4242

43-
### hasCollection(string $collection): bool
43+
### hasCollection(string $name): bool
4444
Check if a collection exists.
4545
```
4646
$arangoClient->schema()->hasCollection('_fishbowl');
4747
```
4848

49-
### createCollection(string $collection, array $config = [], $waitForSyncReplication = null, $enforceReplicationFactor = null): bool
49+
### createCollection(string $name, array $config = [], $waitForSyncReplication = null, $enforceReplicationFactor = null): bool
5050
Create a collection
5151
```
5252
$arangoClient->schema()->createCollection('users');

docs/schema-users.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,74 @@ You can use the schema manager to perform CRUD actions on database users.
44
## User functions
55
The schema manager supports the following index functions:
66

7+
### getUser(string $username): array
8+
Get user properties.
9+
```
10+
$arangoClient->schema()->getUser('kimiko');
11+
```
12+
13+
### getUsers(): array
14+
Get user properties of all visible users.
15+
```
16+
$arangoClient->schema()->getUsers();
17+
```
18+
19+
### hasUser(string $username): bool
20+
Check if a user exists.
21+
```
22+
$arangoClient->schema()->hasUser('kimiko');
23+
```
24+
25+
### createUser(array $user): array
26+
Create a user.
27+
```
28+
$arangoClient->schema()->createUser([
29+
'user' => 'kimiko',
30+
'password' => 'highly secretive password'
31+
]);
32+
```
33+
34+
### updateUser(string $username, array $properties): array
35+
Update a user's properties
36+
```
37+
$arangoClient->schema()->updateUser([
38+
'user' => 'kimiko',
39+
['active => false]
40+
]);
41+
```
42+
43+
### replaceUser(string $username, array $user): array
44+
Replace a user. Used to change the username.
45+
```
46+
$arangoClient->schema()->replaceUser([
47+
'user' => 'kimiko',
48+
[
49+
'user' => 'newUserName',
50+
'active' => true
51+
]
52+
]);
53+
```
54+
55+
### deleteUser(string $username): bool
56+
Delete a user.
57+
```
58+
$arangoClient->schema()->deleteUser('newUserName');
59+
```
60+
61+
### getDatabaseAccessLevel(string $username, string $database): string
62+
Get the access level a user has on a specific database.
63+
```
64+
$this->schemaManager->getDatabaseAccessLevel('root', '_system');
65+
```
66+
67+
### setDatabaseAccessLevel(string $username, string $database, string $grant): array
68+
Set the access level a user has for a specific database.
69+
```
70+
$this->schemaManager->getDatabaseAccessLevel('kimiko', 'the_boys', 'rw');
71+
```
72+
73+
### clearDatabaseAccessLevel(string $username, string $database): bool
74+
Remove the access level of a user for a specific database.
75+
```
76+
$this->schemaManager->clearDatabaseAccessLevel('kimiko', 'the_boys');
77+
```

docs/schema-views.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,63 @@ You can use the schema manager to perform CRUD actions on indexes within a colle
44
## View functions
55
The schema manager supports the following index functions:
66

7+
### createView(array $view): array
8+
```
9+
$arangoClient->schema()->createView([
10+
'name' => 'testViewBasics',
11+
'type' => 'arangosearch'
12+
]);
13+
```
14+
15+
### getView(string $name): array
16+
```
17+
$arangoClient->schema()->getView('testViewBasics');
18+
```
19+
20+
### getViews(): array
21+
```
22+
$arangoClient->schema()->getViews();
23+
```
24+
25+
### hasView(string $name): bool
26+
```
27+
$arangoClient->schema()->hasView('testViewBasics');
28+
```
29+
30+
31+
### getViewProperties(string $name): array
32+
```
33+
$arangoClient->schema()->getViewProperties('testViewBasics');
34+
```
35+
36+
### renameView(string $old, string $new): array
37+
```
38+
$arangoClient->schema()->renameView('testViewBasics', 'pages');
39+
```
40+
41+
### updateView(string $name, array $properties): array
42+
```
43+
$arangoClient->schema()->updateView('pages', [
44+
'cleanupIntervalStep' => 3
45+
]);
46+
```
47+
48+
### replaceView(string $name, array $newView)
49+
Use replaceView if you want to update the primarySort or primarySortCompression.
50+
This will delete the old view and create a new one. The new view will need to be build from the data so might
51+
not be available right away.
52+
```
53+
$arangoClient->schema()->updateView('pages', [
54+
'cleanupIntervalStep' => 3,
55+
'primarySort' => [[
56+
'field' => 'email',
57+
'direction' => 'desc'
58+
]]
59+
]);
60+
```
61+
62+
### deleteView(string $name): bool
63+
```
64+
$arangoClient->schema()->deleteView('testViewBasics');
65+
```
66+

src/ArangoClient.php

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use ArangoClient\Exceptions\ArangoException;
88
use ArangoClient\Http\HttpClientConfig;
9+
use ArangoClient\Http\HttpRequestOptions;
910
use ArangoClient\Statement\Statement;
1011
use ArangoClient\Transactions\SupportsTransactions;
1112
use GuzzleHttp\Client as GuzzleClient;
@@ -72,25 +73,43 @@ public function generateEndpoint(array $config): string
7273
*
7374
* @param string $method
7475
* @param string $uri
75-
* @param array<mixed> $options
76+
* @param array<mixed>|HttpRequestOptions $options
7677
* @param string|null $database
7778
* @return array<mixed>
7879
* @throws ArangoException
7980
*/
80-
public function request(string $method, string $uri, array $options = [], ?string $database = null): array
81+
public function request(string $method, string $uri, $options = [], ?string $database = null): array
8182
{
8283
$uri = $this->prependDatabaseToUri($uri, $database);
8384

85+
if (is_array($options)) {
86+
$options = $this->prepareRequestOptions($options);
87+
}
88+
8489
$response = null;
8590
try {
86-
$response = $this->httpClient->request($method, $uri, $options);
91+
$response = $this->httpClient->request($method, $uri, $options->all());
8792
} catch (Throwable $e) {
8893
$this->handleGuzzleException($e);
8994
}
9095

9196
return $this->cleanupResponse($response);
9297
}
9398

99+
/**
100+
* @param array<mixed> $options
101+
* @return HttpRequestOptions
102+
* @throws ArangoException
103+
*/
104+
protected function prepareRequestOptions(array $options): HttpRequestOptions
105+
{
106+
if (isset($options['body'])) {
107+
$options['body'] = $this->jsonEncode($options['body']);
108+
}
109+
110+
return new HttpRequestOptions($options);
111+
}
112+
94113
/**
95114
* Return the response with debug information (for internal testing purposes).
96115
*
@@ -186,24 +205,23 @@ protected function decodeResponse(?ResponseInterface $response): array
186205
}
187206

188207
/**
189-
* @param array<mixed> $data
208+
* @param mixed $data
190209
* @return string
191210
* @throws ArangoException
192211
*/
193-
public function jsonEncode(array $data): string
212+
public function jsonEncode($data): string
194213
{
195-
$response = '';
196-
197-
if (! empty($data)) {
198-
$response = json_encode($data);
199-
}
214+
$options = 0;
200215
if (empty($data)) {
201-
$response = json_encode($data, JSON_FORCE_OBJECT);
216+
$options = JSON_FORCE_OBJECT;
202217
}
203218

219+
$response = json_encode($data, $options);
220+
204221
if ($response === false) {
205222
throw new ArangoException('JSON encoding failed with error: ' . json_last_error_msg(), json_last_error());
206223
}
224+
207225
return $response;
208226
}
209227

src/Http/HttpClientConfig.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace ArangoClient\Http;
66

7-
use Spatie\DataTransferObject\DataTransferObject;
7+
use Spatie\DataTransferObject\FlexibleDataTransferObject;
88

99
/**
1010
* Class HttpClientConfig
@@ -14,7 +14,7 @@
1414
*
1515
* @package ArangoClient\Http
1616
*/
17-
class HttpClientConfig extends DataTransferObject
17+
class HttpClientConfig extends FlexibleDataTransferObject
1818
{
1919
/**
2020
* @var string

0 commit comments

Comments
 (0)