Skip to content

Commit

Permalink
Merge #298
Browse files Browse the repository at this point in the history
298: Changes the Keys Class r=alallema a=alallema

### Breaking Changes

The `class` `Keys` wasn't used as a proper class and did not return objects from the `Keys` but arrays. This has been changed. Setters and Getters have been added to the class.

- `client.getKey` return a Keys
- `client.createKey` return a Keys
- `client.updateKey` return a Keys

Co-authored-by: alallema <amelie@meilisearch.com>
  • Loading branch information
bors[bot] and alallema authored Mar 2, 2022
2 parents e8894ed + 9bc8b58 commit dab8f0a
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 72 deletions.
13 changes: 10 additions & 3 deletions src/Endpoints/Delegates/HandlesKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,31 @@

namespace MeiliSearch\Endpoints\Delegates;

use MeiliSearch\Endpoints\Keys;

trait HandlesKeys
{
public function getKeys(): array
{
return $this->keys->all();
}

public function getKey($key): array
public function getRawKeys(): array
{
return $this->keys->allRaw();
}

public function getKey($key): Keys
{
return $this->keys->get($key);
}

public function createKey(array $options = []): array
public function createKey(array $options = []): Keys
{
return $this->keys->create($options);
}

public function updateKey(string $key, array $options = []): array
public function updateKey(string $key, array $options = []): Keys
{
return $this->keys->update($key, $options);
}
Expand Down
115 changes: 109 additions & 6 deletions src/Endpoints/Keys.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,133 @@

namespace MeiliSearch\Endpoints;

use DateTime;
use MeiliSearch\Contracts\Endpoint;
use MeiliSearch\Contracts\Http;

class Keys extends Endpoint
{
protected const PATH = '/keys';

public function get($key): array
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);
}

/**
* @return $this
*/
protected function newInstance(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->http->get(self::PATH.'/'.$key);
return $this->createdAt;
}

public function getUpdatedAt(): ?DateTime
{
return $this->updatedAt;
}

public function get($key): self
{
$response = $this->http->get(self::PATH.'/'.$key);

return $this->newInstance($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 = []): array
public function create(array $options = []): self
{
return $this->http->post(self::PATH, $options);
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->newInstance($response);
}

public function update(string $key, array $options = []): array
public function update(string $key, array $options = []): self
{
return $this->http->patch(self::PATH.'/'.$key, $options);
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->newInstance($response);
}

public function delete(string $key): array
Expand Down
Loading

0 comments on commit dab8f0a

Please sign in to comment.