-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathKeys.php
162 lines (133 loc) · 4.25 KB
/
Keys.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
declare(strict_types=1);
namespace MeiliSearch\Endpoints;
use DateTime;
use MeiliSearch\Contracts\Endpoint;
use MeiliSearch\Contracts\Http;
class Keys extends Endpoint
{
protected const PATH = '/keys';
private ?string $key;
private ?string $description;
private ?array $actions;
private ?array $indexes;
private ?DateTime $expiresAt;
private ?DateTime $createdAt;
private ?DateTime $updatedAt;
public function __construct(Http $http, $key = null, $description = null, $actions = null, $indexes = null, $expiresAt = null, $createdAt = null, $updatedAt = null)
{
$this->key = $key;
$this->description = $description;
$this->actions = $actions;
$this->indexes = $indexes;
$this->expiresAt = $expiresAt;
$this->createdAt = $createdAt;
$this->updatedAt = $updatedAt;
parent::__construct($http);
}
protected function newInstance(array $attributes): self
{
$key = new self(
$this->http,
$attributes['key'],
$attributes['description'],
$attributes['actions'],
$attributes['indexes'],
);
if ($attributes['expiresAt']) {
$key->expiresAt = date_create_from_format('Y-m-d\TH:i:s\Z', $attributes['expiresAt']);
}
if ($attributes['createdAt']) {
$key->createdAt = date_create_from_format('Y-m-d\TH:i:s.vu\Z', $attributes['createdAt']);
}
if ($attributes['updatedAt']) {
$key->updatedAt = date_create_from_format('Y-m-d\TH:i:s.vu\Z', $attributes['updatedAt']);
}
return $key;
}
/**
* @return $this
*/
protected function fill(array $attributes): self
{
$this->key = $attributes['key'];
$this->description = $attributes['description'];
$this->actions = $attributes['actions'];
$this->indexes = $attributes['indexes'];
if ($attributes['expiresAt']) {
$this->expiresAt = date_create_from_format('Y-m-d\TH:i:s\Z', $attributes['expiresAt']);
}
if ($attributes['createdAt']) {
$this->createdAt = date_create_from_format('Y-m-d\TH:i:s.vu\Z', $attributes['createdAt']);
}
if ($attributes['updatedAt']) {
$this->updatedAt = date_create_from_format('Y-m-d\TH:i:s.vu\Z', $attributes['updatedAt']);
}
return $this;
}
public function getKey(): ?string
{
return $this->key;
}
public function getDescription(): ?string
{
return $this->description;
}
public function getActions(): ?array
{
return $this->actions;
}
public function getIndexes(): ?array
{
return $this->indexes;
}
public function getExpiresAt(): ?DateTime
{
return $this->expiresAt;
}
public function getCreatedAt(): ?DateTime
{
return $this->createdAt;
}
public function getUpdatedAt(): ?DateTime
{
return $this->updatedAt;
}
public function get($key): self
{
$response = $this->http->get(self::PATH.'/'.$key);
return $this->fill($response);
}
public function all(): array
{
$keys = [];
foreach ($this->allRaw()['results'] as $key) {
$keys[] = $this->newInstance($key);
}
return $keys;
}
public function allRaw(): array
{
return $this->http->get(self::PATH.'/');
}
public function create(array $options = []): self
{
if ($options['expiresAt'] && $options['expiresAt'] instanceof DateTime) {
$options['expiresAt'] = $options['expiresAt']->format('Y-m-d\TH:i:s.vu\Z');
}
$response = $this->http->post(self::PATH, $options);
return $this->fill($response);
}
public function update(string $key, array $options = []): self
{
if ($options['expiresAt'] && $options['expiresAt'] instanceof DateTime) {
$options['expiresAt'] = $options['expiresAt']->format('Y-m-d\TH:i:s.vu\Z');
}
$response = $this->http->patch(self::PATH.'/'.$key, $options);
return $this->fill($response);
}
public function delete(string $key): array
{
return $this->http->delete(self::PATH.'/'.$key) ?? [];
}
}