Archived
1
0
Fork 0
This repository has been archived on 2026-06-16. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
wow-raid-bingo/app/Models/Casts/GameSettingsCaster.php
2021-10-18 11:56:52 +02:00

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());
}
}