Skip to content

Commit

Permalink
Merge pull request #30 from CristalTeam/hotfix/find_one_with_null_arg…
Browse files Browse the repository at this point in the history
…ument

Allow passing null to find method
  • Loading branch information
ffouchier authored Dec 14, 2021
2 parents 914335c + 378804f commit 945137e
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cristal\ApiWrapper;

use Cristal\ApiWrapper\Concerns\HasCache;
use Cristal\ApiWrapper\Exceptions\ApiEntityNotFoundException;
use Cristal\ApiWrapper\Transports\TransportInterface;

/**
Expand Down Expand Up @@ -55,7 +56,7 @@ public function __call($name, $arguments)

$endpoint = strtolower($matches[2]);
if ('get' === $matches[1]) {
if (!is_array($arguments[0] ?? [])) {
if (array_key_exists(0, $arguments) && !is_array($arguments[0])) {
return $this->findOne($endpoint, ...$arguments);
}

Expand Down Expand Up @@ -98,6 +99,12 @@ protected function findAll(string $endpoint, array $filters = []): array
*/
protected function findOne(string $endpoint, $id, array $filters = [])
{
// Makes no sense to proceed API call if we passed null id.
// It would mean that we would like to do a findAll call instead and it could cause side effects.
if ($id === null) {
throw new ApiEntityNotFoundException([]);
}

$uri = '/'.$endpoint.'/'.$id;
$key = $uri.'?'.http_build_query($filters);
if ($this->hasCache($key)) {
Expand Down

0 comments on commit 945137e

Please sign in to comment.