|
4 | 4 |
|
5 | 5 | namespace MeiliSearch\Endpoints; |
6 | 6 |
|
| 7 | +use DateTime; |
7 | 8 | use MeiliSearch\Contracts\Endpoint; |
| 9 | +use MeiliSearch\Contracts\Http; |
8 | 10 |
|
9 | 11 | class Keys extends Endpoint |
10 | 12 | { |
11 | 13 | protected const PATH = '/keys'; |
12 | 14 |
|
13 | | - public function get($key): array |
| 15 | + private ?string $key; |
| 16 | + private ?string $description; |
| 17 | + private ?array $actions; |
| 18 | + private ?array $indexes; |
| 19 | + private ?DateTime $expiresAt; |
| 20 | + private ?DateTime $createdAt; |
| 21 | + private ?DateTime $updatedAt; |
| 22 | + |
| 23 | + public function __construct(Http $http, $key = null, $description = null, $actions = null, $indexes = null, $expiresAt = null, $createdAt = null, $updatedAt = null) |
| 24 | + { |
| 25 | + $this->key = $key; |
| 26 | + $this->description = $description; |
| 27 | + $this->actions = $actions; |
| 28 | + $this->indexes = $indexes; |
| 29 | + $this->expiresAt = $expiresAt; |
| 30 | + $this->createdAt = $createdAt; |
| 31 | + $this->updatedAt = $updatedAt; |
| 32 | + |
| 33 | + parent::__construct($http); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @return $this |
| 38 | + */ |
| 39 | + protected function newInstance(array $attributes): self |
| 40 | + { |
| 41 | + $this->key = $attributes['key']; |
| 42 | + $this->description = $attributes['description']; |
| 43 | + $this->actions = $attributes['actions']; |
| 44 | + $this->indexes = $attributes['indexes']; |
| 45 | + if ($attributes['expiresAt']) { |
| 46 | + $this->expiresAt = date_create_from_format('Y-m-d\TH:i:s\Z', $attributes['expiresAt']); |
| 47 | + } |
| 48 | + if ($attributes['createdAt']) { |
| 49 | + $this->createdAt = date_create_from_format('Y-m-d\TH:i:s.vu\Z', $attributes['createdAt']); |
| 50 | + } |
| 51 | + if ($attributes['updatedAt']) { |
| 52 | + $this->updatedAt = date_create_from_format('Y-m-d\TH:i:s.vu\Z', $attributes['updatedAt']); |
| 53 | + } |
| 54 | + |
| 55 | + return $this; |
| 56 | + } |
| 57 | + |
| 58 | + public function getKey(): ?string |
| 59 | + { |
| 60 | + return $this->key; |
| 61 | + } |
| 62 | + |
| 63 | + public function getDescription(): ?string |
| 64 | + { |
| 65 | + return $this->description; |
| 66 | + } |
| 67 | + |
| 68 | + public function getActions(): ?array |
| 69 | + { |
| 70 | + return $this->actions; |
| 71 | + } |
| 72 | + |
| 73 | + public function getIndexes(): ?array |
| 74 | + { |
| 75 | + return $this->indexes; |
| 76 | + } |
| 77 | + |
| 78 | + public function getExpiresAt(): ?DateTime |
| 79 | + { |
| 80 | + return $this->expiresAt; |
| 81 | + } |
| 82 | + |
| 83 | + public function getCreatedAt(): ?DateTime |
14 | 84 | { |
15 | | - return $this->http->get(self::PATH.'/'.$key); |
| 85 | + return $this->createdAt; |
| 86 | + } |
| 87 | + |
| 88 | + public function getUpdatedAt(): ?DateTime |
| 89 | + { |
| 90 | + return $this->updatedAt; |
| 91 | + } |
| 92 | + |
| 93 | + public function get($key): self |
| 94 | + { |
| 95 | + $response = $this->http->get(self::PATH.'/'.$key); |
| 96 | + |
| 97 | + return $this->newInstance($response); |
16 | 98 | } |
17 | 99 |
|
18 | 100 | public function all(): array |
| 101 | + { |
| 102 | + $keys = []; |
| 103 | + |
| 104 | + foreach ($this->allRaw()['results'] as $key) { |
| 105 | + $keys[] = $this->newInstance($key); |
| 106 | + } |
| 107 | + |
| 108 | + return $keys; |
| 109 | + } |
| 110 | + |
| 111 | + public function allRaw(): array |
19 | 112 | { |
20 | 113 | return $this->http->get(self::PATH.'/'); |
21 | 114 | } |
22 | 115 |
|
23 | | - public function create(array $options = []): array |
| 116 | + public function create(array $options = []): self |
24 | 117 | { |
25 | | - return $this->http->post(self::PATH, $options); |
| 118 | + if ($options['expiresAt'] && $options['expiresAt'] instanceof DateTime) { |
| 119 | + $options['expiresAt'] = $options['expiresAt']->format('Y-m-d\TH:i:s.vu\Z'); |
| 120 | + } |
| 121 | + $response = $this->http->post(self::PATH, $options); |
| 122 | + |
| 123 | + return $this->newInstance($response); |
26 | 124 | } |
27 | 125 |
|
28 | | - public function update(string $key, array $options = []): array |
| 126 | + public function update(string $key, array $options = []): self |
29 | 127 | { |
30 | | - return $this->http->patch(self::PATH.'/'.$key, $options); |
| 128 | + if ($options['expiresAt'] && $options['expiresAt'] instanceof DateTime) { |
| 129 | + $options['expiresAt'] = $options['expiresAt']->format('Y-m-d\TH:i:s.vu\Z'); |
| 130 | + } |
| 131 | + $response = $this->http->patch(self::PATH.'/'.$key, $options); |
| 132 | + |
| 133 | + return $this->newInstance($response); |
31 | 134 | } |
32 | 135 |
|
33 | 136 | public function delete(string $key): array |
|
0 commit comments