-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Asset.php
52 lines (44 loc) · 1.37 KB
/
Asset.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
declare(strict_types=1);
namespace BBSLab\NovaFileManager\Casts;
use BBSLab\NovaFileManager\Support\Asset as AssetObject;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use InvalidArgumentException;
class Asset implements CastsAttributes
{
/**
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param string|null $value
* @param array $attributes
* @return \BBSLab\NovaFileManager\Support\Asset|null
*
* @throws \JsonException
*/
public function get($model, string $key, $value, array $attributes): ?AssetObject
{
if ($value === null) {
return null;
}
return new AssetObject(...json_decode($value, true, 512, JSON_THROW_ON_ERROR));
}
/**
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param array|AssetObject|null $value
* @param array $attributes
* @return string|null
*
* @throws \JsonException
*/
public function set($model, string $key, $value, array $attributes): ?string
{
if ($value === null) {
return null;
}
if (is_array($value) || $value instanceof AssetObject) {
return json_encode($value, JSON_THROW_ON_ERROR);
}
throw new InvalidArgumentException('Invalid value for Asset cast');
}
}