Skip to content

Commit

Permalink
Merge #323
Browse files Browse the repository at this point in the history
323: Fix `getKeys` method r=alallema a=alallema

The `Client::getKeys` method returns the correct number of keys but the same object for all of them.
This was due to the `newInstance` method which returned the instance instead of a new Instance as its name indicated.

Co-authored-by: alallema <amelie@meilisearch.com>
  • Loading branch information
bors[bot] and alallema committed May 5, 2022
2 parents 20a9e3e + e7076f4 commit c28fc1b
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/Endpoints/Keys.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,32 @@ public function __construct(Http $http, $key = null, $description = null, $actio
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 newInstance(array $attributes): self
protected function fill(array $attributes): self
{
$this->key = $attributes['key'];
$this->description = $attributes['description'];
Expand Down Expand Up @@ -94,7 +116,7 @@ public function get($key): self
{
$response = $this->http->get(self::PATH.'/'.$key);

return $this->newInstance($response);
return $this->fill($response);
}

public function all(): array
Expand All @@ -120,7 +142,7 @@ public function create(array $options = []): self
}
$response = $this->http->post(self::PATH, $options);

return $this->newInstance($response);
return $this->fill($response);
}

public function update(string $key, array $options = []): self
Expand All @@ -130,7 +152,7 @@ public function update(string $key, array $options = []): self
}
$response = $this->http->patch(self::PATH.'/'.$key, $options);

return $this->newInstance($response);
return $this->fill($response);
}

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

0 comments on commit c28fc1b

Please sign in to comment.