Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
stevebauman committed Sep 23, 2024
1 parent c17758d commit 974a982
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 9 deletions.
19 changes: 18 additions & 1 deletion src/Concerns/HasCasts.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Brick\Math\RoundingMode;
use Carbon\CarbonInterface;
use DateTimeInterface;
use DirectoryTree\ActiveRedis\Exceptions\JsonEncodingException;
use DirectoryTree\ActiveRedis\Model;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -247,6 +248,22 @@ protected function fromDateTime(mixed $value): ?string
);
}

/**
* Cast the given attribute to JSON.
*/
protected function castAttributeAsJson(string $key, mixed $value): string
{
$value = $this->asJson($value);

if ($value === false) {
throw JsonEncodingException::forAttribute(
$this, $key, json_last_error_msg()
);
}

return $value;
}

/**
* Return a decimal as string.
*/
Expand All @@ -262,7 +279,7 @@ protected function asDecimal(float|string $value, int $decimals): string
/**
* Encode the given value as JSON.
*/
protected function asJson(mixed $value): string
protected function asJson(mixed $value): string|false
{
return json_encode($value);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DirectoryTree\ActiveRedis;
namespace DirectoryTree\ActiveRedis\Exceptions;

use RuntimeException;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DirectoryTree\ActiveRedis;
namespace DirectoryTree\ActiveRedis\Exceptions;

use RuntimeException;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DirectoryTree\ActiveRedis;
namespace DirectoryTree\ActiveRedis\Exceptions;

use RuntimeException;

Expand Down
22 changes: 22 additions & 0 deletions src/Exceptions/JsonEncodingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace DirectoryTree\ActiveRedis\Exceptions;

use RuntimeException;

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)
{
$class = get_class($model);

return new static("Unable to encode attribute [{$key}] for model [{$class}] to JSON: {$message}.");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DirectoryTree\ActiveRedis;
namespace DirectoryTree\ActiveRedis\Exceptions;

use Illuminate\Support\Arr;
use RuntimeException;
Expand Down
2 changes: 2 additions & 0 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use DirectoryTree\ActiveRedis\Concerns\HasAttributes;
use DirectoryTree\ActiveRedis\Concerns\HasCasts;
use DirectoryTree\ActiveRedis\Concerns\HasTimestamps;
use DirectoryTree\ActiveRedis\Exceptions\DuplicateKeyException;
use DirectoryTree\ActiveRedis\Exceptions\InvalidKeyException;
use Illuminate\Contracts\Redis\Connection;
use Illuminate\Redis\RedisManager;
use Illuminate\Support\Arr;
Expand Down
2 changes: 2 additions & 0 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace DirectoryTree\ActiveRedis;

use Closure;
use DirectoryTree\ActiveRedis\Exceptions\AttributeNotQueryableException;
use DirectoryTree\ActiveRedis\Exceptions\ModelNotFoundException;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

Expand Down
13 changes: 13 additions & 0 deletions tests/Fixtures/ModelStubWithCasts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace DirectoryTree\ActiveRedis\Tests\Fixtures;

use DirectoryTree\ActiveRedis\Model;

class ModelStubWithCasts extends Model
{
protected array $casts = [
'date' => 'date',
'datetime' => 'datetime',
];
}
6 changes: 3 additions & 3 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

use Carbon\Carbon;
use DirectoryTree\ActiveRedis\DuplicateKeyException;
use DirectoryTree\ActiveRedis\InvalidKeyException;
use DirectoryTree\ActiveRedis\ModelNotFoundException;
use DirectoryTree\ActiveRedis\Exceptions\DuplicateKeyException;
use DirectoryTree\ActiveRedis\Exceptions\InvalidKeyException;
use DirectoryTree\ActiveRedis\Exceptions\ModelNotFoundException;
use DirectoryTree\ActiveRedis\Query;
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStub;
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStubWithCustomKey;
Expand Down
2 changes: 1 addition & 1 deletion tests/QueryTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

use DirectoryTree\ActiveRedis\AttributeNotQueryableException;
use DirectoryTree\ActiveRedis\Exceptions\AttributeNotQueryableException;
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStub;
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStubWithCustomKey;
use DirectoryTree\ActiveRedis\Tests\Fixtures\ModelStubWithQueryable;
Expand Down

0 comments on commit 974a982

Please sign in to comment.