Skip to content

Commit

Permalink
🏗 Improve json parsing by throwing exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
marcreichel committed Jun 6, 2021
1 parent e523eca commit 2f43408
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use InvalidArgumentException;
use JsonException;
use MarcReichel\IGDBLaravel\Exceptions\AuthenticationException;
use MarcReichel\IGDBLaravel\Exceptions\InvalidParamsException;
use MarcReichel\IGDBLaravel\Exceptions\MissingEndpointException;
Expand Down Expand Up @@ -93,6 +94,8 @@ class Builder
* Builder constructor.
*
* @param $model
*
* @throws ReflectionException
*/
public function __construct($model = null)
{
Expand Down Expand Up @@ -264,6 +267,8 @@ public function search(string $query): self
* @param string $boolean
*
* @return self
* @throws ReflectionException
* @throws JsonException
*/
public function where(
$key,
Expand Down Expand Up @@ -307,7 +312,7 @@ public function where(
$this->query->put('where', $where);
}
} else {
$value = !is_int($value) ? json_encode($value) : $value;
$value = !is_int($value) ? json_encode($value, JSON_THROW_ON_ERROR) : $value;
$where->push(($where->count() ? $boolean . ' ' : '') . $key . ' ' . $operator . ' ' . $value);
$this->query->put('where', $where);
}
Expand All @@ -324,6 +329,7 @@ public function where(
* @param string $boolean
*
* @return self
* @throws ReflectionException|JsonException
*/
public function orWhere(
$key,
Expand Down Expand Up @@ -354,6 +360,7 @@ public function orWhere(
* @param string $boolean
*
* @return self
* @throws JsonException
*/
public function whereLike(
string $key,
Expand All @@ -376,7 +383,7 @@ public function whereLike(
$operator = $caseSensitive ? '=' : '~';
$prefix = $hasPrefix ? '*' : '';
$suffix = $hasSuffix ? '*' : '';
$value = json_encode($value);
$value = json_encode($value, JSON_THROW_ON_ERROR);

$where->push(($where->count() ? $boolean . ' ' : '') . $key . ' ' . $operator . ' ' . $prefix . $value . $suffix);

Expand All @@ -394,6 +401,7 @@ public function whereLike(
* @param string $boolean
*
* @return self
* @throws JsonException
*/
public function orWhereLike(
string $key,
Expand All @@ -413,6 +421,7 @@ public function orWhereLike(
* @param string $boolean
*
* @return self
* @throws JsonException
*/
public function whereNotLike(
string $key,
Expand All @@ -435,7 +444,7 @@ public function whereNotLike(
$operator = $caseSensitive ? '!=' : '!~';
$prefix = $hasPrefix ? '*' : '';
$suffix = $hasSuffix ? '*' : '';
$value = json_encode($value);
$value = json_encode($value, JSON_THROW_ON_ERROR);

$where->push(($where->count() ? $boolean . ' ' : '') . $key . ' ' . $operator . ' ' . $prefix . $value . $suffix);

Expand All @@ -453,6 +462,7 @@ public function whereNotLike(
* @param string $boolean
*
* @return self
* @throws JsonException
*/
public function orWhereNotLike(
string $key,
Expand Down Expand Up @@ -511,6 +521,7 @@ private function invalidOperatorAndValue(string $operator, $value): bool
* @param string $method
*
* @return self
* @throws ReflectionException
*/
protected function addArrayOfWheres(
array $arrayOfWheres,
Expand Down Expand Up @@ -539,6 +550,7 @@ protected function addArrayOfWheres(
* @param string $boolean
*
* @return self
* @throws ReflectionException
*/
protected function whereNested(Closure $callback, string $boolean = '&'): self
{
Expand Down Expand Up @@ -885,6 +897,7 @@ public function orWhereNotInExact(
* @param string $boolean
*
* @return self
* @throws ReflectionException
*/
public function whereBetween(
string $key,
Expand Down Expand Up @@ -922,6 +935,7 @@ public function whereBetween(
* @param string $boolean
*
* @return self
* @throws ReflectionException
*/
public function orWhereBetween(
string $key,
Expand All @@ -944,6 +958,7 @@ public function orWhereBetween(
* @param string $boolean
*
* @return self
* @throws ReflectionException
*/
public function whereNotBetween(
string $key,
Expand Down Expand Up @@ -981,6 +996,7 @@ public function whereNotBetween(
* @param string $boolean
*
* @return self
* @throws ReflectionException
*/
public function orWhereNotBetween(
string $key,
Expand Down Expand Up @@ -1116,12 +1132,13 @@ public function orWhereNotNull(string $key, string $boolean = '|'): self
/**
* Add a "where date" statement to the query.
*
* @param string $key
* @param mixed $operator
* @param string $key
* @param mixed $operator
* @param mixed|null $value
* @param string $boolean
* @param string $boolean
*
* @return self
* @throws ReflectionException|JsonException
*/
public function whereDate(string $key, $operator, $value = null, string $boolean = '&'): self
{
Expand Down Expand Up @@ -1165,12 +1182,13 @@ public function whereDate(string $key, $operator, $value = null, string $boolean
/**
* Add a "or where date" statement to the query.
*
* @param string $key
* @param mixed $operator
* @param string $key
* @param mixed $operator
* @param mixed|null $value
* @param string $boolean
* @param string $boolean
*
* @return self
* @throws ReflectionException|JsonException
*/
public function orWhereDate(string $key, $operator, $value = null, string $boolean = '|'): self
{
Expand All @@ -1180,12 +1198,13 @@ public function orWhereDate(string $key, $operator, $value = null, string $boole
/**
* Add a "where year" statement to the query.
*
* @param string $key
* @param mixed $operator
* @param string $key
* @param mixed $operator
* @param mixed|null $value
* @param string $boolean
* @param string $boolean
*
* @return self
* @throws ReflectionException|JsonException
*/
public function whereYear(string $key, $operator, $value = null, string $boolean = '&'): self
{
Expand Down Expand Up @@ -1227,6 +1246,7 @@ public function whereYear(string $key, $operator, $value = null, string $boolean
* @param string $boolean
*
* @return self
* @throws ReflectionException|JsonException
*/
public function orWhereYear(string $key, $operator, $value, string $boolean = '|'): self
{
Expand Down Expand Up @@ -1262,6 +1282,7 @@ public function orderBy(string $key, string $direction = 'asc'): self
* @param string $key
*
* @return self
* @throws InvalidParamsException
*/
public function orderByDesc(string $key): self
{
Expand Down Expand Up @@ -1467,7 +1488,7 @@ function () use ($endpoint) {
* @param int $id
*
* @return mixed|string
* @throws MissingEndpointException|AuthenticationException
* @throws MissingEndpointException|AuthenticationException|ReflectionException|JsonException
*/
public function find(int $id)
{
Expand All @@ -1478,7 +1499,7 @@ public function find(int $id)
* @param int $id
*
* @return mixed
* @throws MissingEndpointException|ModelNotFoundException|AuthenticationException
* @throws MissingEndpointException|ModelNotFoundException|AuthenticationException|ReflectionException|JsonException
*/
public function findOrFail(int $id)
{
Expand Down

0 comments on commit 2f43408

Please sign in to comment.