45 lines
1.2 KiB
PHP
45 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Casts;
|
|
|
|
use App\Game\GameSettings;
|
|
|
|
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
|
|
use InvalidArgumentException;
|
|
|
|
class GameSettingsCaster implements CastsAttributes
|
|
{
|
|
/**
|
|
* Cast the given value.
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Model $model
|
|
* @param string $key
|
|
* @param mixed $value
|
|
* @param array $attributes
|
|
* @return GameSettings
|
|
*/
|
|
public function get($model, $key, $value, $attributes)
|
|
{
|
|
$value = json_decode($value, true);
|
|
return new GameSettings($value);
|
|
}
|
|
|
|
/**
|
|
* Prepare the given value for storage.
|
|
*
|
|
* @param \Illuminate\Database\Eloquent\Model $model
|
|
* @param string $key
|
|
* @param GameSettings|array $value
|
|
* @param array $attributes
|
|
* @return string
|
|
*/
|
|
public function set($model, $key, $value, $attributes)
|
|
{
|
|
if (is_array($value)) {
|
|
$value = new GameSettings($value);
|
|
} else if (!($value instanceof GameSettings)) {
|
|
throw new InvalidArgumentException('The given value must be an GameSettings instance or array.');
|
|
}
|
|
return json_encode($value->toArray());
|
|
}
|
|
}
|