Skip to content

Commit 59e0b50

Browse files
authored
Merge pull request #25 from square/l7
Separate integration for L7
2 parents 1f9e54d + ce5c471 commit 59e0b50

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Square\Pjson\Integrations\Laravel7;
4+
5+
trait JsonCastable
6+
{
7+
public static function castUsing()
8+
{
9+
return new JsonCaster(static::class);
10+
}
11+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Square\Pjson\Integrations\Laravel7;
4+
5+
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
6+
7+
class JsonCaster implements CastsAttributes
8+
{
9+
public function __construct(
10+
protected string $target
11+
) {
12+
}
13+
14+
public function get($model, $key, $value, $attributes)
15+
{
16+
if ($value === null || $value === '') {
17+
return $value;
18+
}
19+
20+
return $this->target::fromJsonString($value);
21+
}
22+
23+
public function set($model, $key, $value, $attributes)
24+
{
25+
return $value?->toJson();
26+
}
27+
}

0 commit comments

Comments
 (0)