Skip to content

Commit 66fadf0

Browse files
committed
Add types to keys
1 parent 9c4ce8c commit 66fadf0

10 files changed

Lines changed: 539 additions & 423 deletions

File tree

src/Contracts/CreateKeyRequest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Meilisearch\Contracts;
6+
7+
final class CreateKeyRequest
8+
{
9+
/**
10+
* @param list<KeyAction> $actions a list of actions permitted for the key
11+
* @param list<non-empty-string> $indexes A list of accessible indexes permitted for the key. ["*"] for all
12+
* indexes. The * character can be used as a wildcard when located at
13+
* the last position. e.g. products_* to allow access to all indexes
14+
* whose names start with products_.
15+
* @param non-empty-string|null $name A human-readable name for the key. null if empty.
16+
* @param non-empty-string|null $description A description for the key. null if empty.
17+
* @param non-empty-string|null $uid A uuid v4 to identify the API Key. If not specified, it's generated by Meilisearch.
18+
* @param \DateTimeImmutable|null $expiresAt Represent the expiration date and time as RFC 3339 format. null equals to no expiration time.
19+
*/
20+
public function __construct(
21+
private readonly array $actions,
22+
private readonly array $indexes,
23+
private readonly ?string $name = null,
24+
private readonly ?string $description = null,
25+
private readonly ?string $uid = null,
26+
private readonly ?\DateTimeImmutable $expiresAt = null,
27+
) {
28+
}
29+
30+
/**
31+
* @return array{
32+
* actions: list<non-empty-string>,
33+
* indexes: list<non-empty-string>,
34+
* description: non-empty-string,
35+
* name: non-empty-string,
36+
* expiresAt: non-empty-string,
37+
* uid?: non-empty-string
38+
* }
39+
*/
40+
public function toArray(): array
41+
{
42+
$payload = [
43+
'actions' => array_column($this->actions, 'value'),
44+
'indexes' => $this->indexes,
45+
'description' => $this->description,
46+
'name' => $this->name,
47+
'expiresAt' => $this->expiresAt?->format('Y-m-d\TH:i:s.vu\Z'),
48+
];
49+
if (null !== $this->uid) {
50+
$payload['uid'] = $this->uid;
51+
}
52+
53+
return $payload;
54+
}
55+
}

src/Contracts/Key.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Meilisearch\Contracts;
6+
7+
final class Key
8+
{
9+
/**
10+
* @param non-empty-string $uid
11+
* @param non-empty-string $key
12+
* @param list<KeyAction> $actions
13+
* @param list<non-empty-string> $indexes
14+
* @param non-empty-string|null $name
15+
* @param non-empty-string|null $description
16+
*/
17+
public function __construct(
18+
private readonly string $uid,
19+
private readonly string $key,
20+
private readonly array $actions,
21+
private readonly array $indexes,
22+
private readonly ?string $name,
23+
private readonly ?string $description,
24+
private readonly ?\DateTimeInterface $expiresAt,
25+
private readonly ?\DateTimeInterface $createdAt,
26+
private readonly ?\DateTimeInterface $updatedAt,
27+
) {
28+
}
29+
30+
public function getUid(): string
31+
{
32+
return $this->uid;
33+
}
34+
35+
public function getKey(): string
36+
{
37+
return $this->key;
38+
}
39+
40+
public function getActions(): array
41+
{
42+
return $this->actions;
43+
}
44+
45+
public function getIndexes(): array
46+
{
47+
return $this->indexes;
48+
}
49+
50+
public function getName(): ?string
51+
{
52+
return $this->name;
53+
}
54+
55+
public function getDescription(): ?string
56+
{
57+
return $this->description;
58+
}
59+
60+
public function getExpiresAt(): ?\DateTimeInterface
61+
{
62+
return $this->expiresAt;
63+
}
64+
65+
public function getCreatedAt(): ?\DateTimeInterface
66+
{
67+
return $this->createdAt;
68+
}
69+
70+
public function getUpdatedAt(): ?\DateTimeInterface
71+
{
72+
return $this->updatedAt;
73+
}
74+
75+
/**
76+
* @param array{
77+
* uid: non-empty-string,
78+
* key: non-empty-string,
79+
* actions: list<non-empty-string>,
80+
* indexes: list<non-empty-string>,
81+
* name?: non-empty-string,
82+
* description?: non-empty-string,
83+
* expiresAt?: non-empty-string,
84+
* createdAt: non-empty-string,
85+
* updatedAt: non-empty-string
86+
* } $data
87+
*/
88+
public static function fromArray(array $data): self
89+
{
90+
return new self(
91+
$data['uid'],
92+
$data['key'],
93+
array_map(static fn (string $action) => KeyAction::from($action), $data['actions']),
94+
$data['indexes'],
95+
$data['name'] ?? null,
96+
$data['description'] ?? null,
97+
isset($data['expiresAt']) ? new \DateTimeImmutable($data['expiresAt']) : null,
98+
new \DateTimeImmutable($data['createdAt']),
99+
new \DateTimeImmutable($data['updatedAt']),
100+
);
101+
}
102+
}

src/Contracts/KeyAction.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Meilisearch\Contracts;
6+
7+
enum KeyAction: string
8+
{
9+
case Any = '*';
10+
case Search = 'search';
11+
case DocumentsAny = 'documents.*';
12+
case DocumentsAdd = 'documents.add';
13+
case DocumentsGet = 'documents.get';
14+
case DocumentsDelete = 'documents.delete';
15+
case IndexesAny = 'indexes.*';
16+
case IndexesCreate = 'indexes.create';
17+
case IndexesGet = 'indexes.get';
18+
case IndexesUpdate = 'indexes.update';
19+
case IndexesDelete = 'indexes.delete';
20+
case IndexesSwap = 'indexes.swap';
21+
case IndexesCompact = 'indexes.compact';
22+
case TasksAny = 'tasks.*';
23+
case TasksCancel = 'tasks.cancel';
24+
case TasksDelete = 'tasks.delete';
25+
case TasksGet = 'tasks.get';
26+
case TasksCompact = 'tasks.compact';
27+
case SettingsAny = 'settings.*';
28+
case SettingsGet = 'settings.get';
29+
case SettingsUpdate = 'settings.update';
30+
case StatsAny = 'stats.*';
31+
case StatsGet = 'stats.get';
32+
case MetricsAny = 'metrics.*';
33+
case MetricsGet = 'metrics.get';
34+
case DumpsAny = 'dumps.*';
35+
case DumpsCreate = 'dumps.create';
36+
case SnapshotsCreate = 'snapshots.create';
37+
case Version = 'version';
38+
case KeysCreate = 'keys.create';
39+
case KeysGet = 'keys.get';
40+
case KeysUpdate = 'keys.update';
41+
case KeysDelete = 'keys.delete';
42+
case ExperimentalGet = 'experimental.get';
43+
case ExperimentalUpdate = 'experimental.update';
44+
case Export = 'export';
45+
case NetworkGet = 'network.get';
46+
case NetworkUpdate = 'network.update';
47+
case ChatCompletions = 'chatCompletions';
48+
case ChatsAny = 'chats.*';
49+
case ChatsGet = 'chats.get';
50+
case ChatsDelete = 'chats.delete';
51+
case ChatsSettingsAny = 'chatsSettings.*';
52+
case ChatsSettingsGet = 'chatsSettings.get';
53+
case ChatsSettingsUpdate = 'chatsSettings.update';
54+
case GetAny = '*.get';
55+
case WebhooksAny = 'webhooks.*';
56+
case WebhooksGet = 'webhooks.get';
57+
case WebhooksCreate = 'webhooks.create';
58+
case WebhooksUpdate = 'webhooks.update';
59+
case WebhooksDelete = 'webhooks.delete';
60+
case FieldsPost = 'fields.post';
61+
case DynamicSearchRulesAny = 'dynamicSearchRules.*';
62+
case DynamicSearchRulesGet = 'dynamicSearchRules.get';
63+
case DynamicSearchRulesCreate = 'dynamicSearchRules.create';
64+
case DynamicSearchRulesUpdate = 'dynamicSearchRules.update';
65+
case DynamicSearchRulesDelete = 'dynamicSearchRules.delete';
66+
}

src/Contracts/KeysResults.php

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,80 @@
44

55
namespace Meilisearch\Contracts;
66

7-
class KeysResults extends Data
7+
final class KeysResults extends Data
88
{
9+
/**
10+
* @var non-negative-int
11+
*/
912
private int $offset;
13+
14+
/**
15+
* @var non-negative-int
16+
*/
1017
private int $limit;
18+
19+
/**
20+
* @var non-negative-int
21+
*/
1122
private int $total;
1223

24+
/**
25+
* @param array{
26+
* results: array<int, Key>,
27+
* offset: non-negative-int,
28+
* limit: non-negative-int,
29+
* total: non-negative-int
30+
* } $params
31+
*/
1332
public function __construct(array $params)
1433
{
15-
parent::__construct($params['results'] ?? []);
34+
parent::__construct($params['results']);
1635

1736
$this->offset = $params['offset'];
1837
$this->limit = $params['limit'];
19-
$this->total = $params['total'] ?? 0;
38+
$this->total = $params['total'];
2039
}
2140

2241
/**
23-
* @return array<int, array>
42+
* @return array<int, Key>
2443
*/
2544
public function getResults(): array
2645
{
2746
return $this->data;
2847
}
2948

49+
/**
50+
* @return non-negative-int
51+
*/
3052
public function getOffset(): int
3153
{
3254
return $this->offset;
3355
}
3456

57+
/**
58+
* @return non-negative-int
59+
*/
3560
public function getLimit(): int
3661
{
3762
return $this->limit;
3863
}
3964

65+
/**
66+
* @return non-negative-int
67+
*/
4068
public function getTotal(): int
4169
{
4270
return $this->total;
4371
}
4472

73+
/**
74+
* @return array{
75+
* results: array<int, Key>,
76+
* offset: non-negative-int,
77+
* limit: non-negative-int,
78+
* total: non-negative-int
79+
* }
80+
*/
4581
public function toArray(): array
4682
{
4783
return [
@@ -51,9 +87,4 @@ public function toArray(): array
5187
'total' => $this->total,
5288
];
5389
}
54-
55-
public function count(): int
56-
{
57-
return \count($this->data);
58-
}
5990
}

src/Contracts/UpdateKeyRequest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Meilisearch\Contracts;
6+
7+
final class UpdateKeyRequest
8+
{
9+
/**
10+
* @param non-empty-string $keyOrUid the uid or key field of an existing API key
11+
* @param non-empty-string|null $name A new human-readable name for the API key. Pass null to remove the
12+
* existing name. Use this to identify keys by purpose, such as
13+
* "Production Search Key" or "CI/CD Indexing Key".
14+
* @param non-empty-string|null $description A new description for the API key. Pass null to remove the existing
15+
* description. Useful for documenting the purpose or usage of the key.
16+
*/
17+
public function __construct(
18+
public readonly string $keyOrUid,
19+
private readonly ?string $name = null,
20+
private readonly ?string $description = null,
21+
) {
22+
}
23+
24+
/**
25+
* @return array{
26+
* name: non-empty-string|null,
27+
* description: non-empty-string|null
28+
* }
29+
*/
30+
public function toArray(): array
31+
{
32+
return [
33+
'name' => $this->name,
34+
'description' => $this->description,
35+
];
36+
}
37+
}

0 commit comments

Comments
 (0)