Skip to content

Commit

Permalink
Add Jsonable interface
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Oct 4, 2024
1 parent 543fcee commit 6af0a53
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/Exceptions/JsonEncodingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ class JsonEncodingException extends RuntimeException
{
/**
* Create a new JSON encoding exception for an attribute.
*
* @param mixed $model
* @param mixed $key
* @return static
*/
public static function forAttribute(string $model, string $key, string $message)
public static function forAttribute(mixed $model, string $key, string $message): static
{
$class = get_class($model);
return new static('Unable to encode attribute ['.$key.'] for model ['.get_class($model).'] to JSON: '.$message);
}

return new static("Unable to encode attribute [{$key}] for model [{$class}] to JSON: {$message}.");
/**
* Create a new JSON encoding exception for the model.
*/
public static function forModel(mixed $model, string $message): static
{
return new static('Error encoding model ['.get_class($model).'] with ID ['.$model->getKey().'] to JSON: '.$message);
}
}
27 changes: 26 additions & 1 deletion src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@
use DirectoryTree\ActiveRedis\Concerns\Routable;
use DirectoryTree\ActiveRedis\Exceptions\DuplicateKeyException;
use DirectoryTree\ActiveRedis\Exceptions\InvalidKeyException;
use DirectoryTree\ActiveRedis\Exceptions\JsonEncodingException;
use DirectoryTree\ActiveRedis\Repositories\RedisRepository;
use DirectoryTree\ActiveRedis\Repositories\Repository;
use Illuminate\Contracts\Redis\Connection;
use Illuminate\Contracts\Routing\UrlRoutable;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Redis\RedisManager;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\ForwardsCalls;
use JsonException;

abstract class Model implements Arrayable, ArrayAccess, UrlRoutable
abstract class Model implements Arrayable, ArrayAccess, Jsonable, UrlRoutable
{
use Bootable;
use ForwardsCalls;
Expand Down Expand Up @@ -563,6 +566,28 @@ public function toArray(): array
return $this->attributesToArray();
}

/**
* Convert the model instance to JSON.
*/
public function toJson(mixed $options = 0): string
{
try {
$json = json_encode($this->jsonSerialize(), $options | JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
throw JsonEncodingException::forModel($this, $e->getMessage());
}

return $json;
}

/**
* Convert the object into something JSON serializable.
*/
public function jsonSerialize(): mixed
{
return $this->toArray();
}

/**
* Dynamically retrieve attributes on the model.
*/
Expand Down
8 changes: 8 additions & 0 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,11 @@
'id', 'name', 'created_at', 'updated_at',
]);
});

it('can be converted to json', function () {
$model = ModelStub::create([
'name' => 'John Doe',
]);

expect($model->toJson())->toBe(json_encode($model->toArray()));
});

0 comments on commit 6af0a53

Please sign in to comment.