<?php

namespace App\Models;

use App\Traits\ModelTrait;
use Illuminate\Database\Eloquent\Model;
use Laracasts\Presenter\PresentableTrait;

class BlockDescription extends Model
{
    use ModelTrait, PresentableTrait;

    protected $presenter = 'App\Presenters\ContentPresenter';

    protected $table = 'block_descriptions';

    protected $guarded = [];

    protected $casts = [
        'imgs' => 'collection',
    ];

    public    $timestamps = false;

    public function setImgsAttribute($value)
    {
        if (is_array($value)) {
            foreach ($value as $key => $val) {
                $value[$key] = str_replace('?x-oss-process=image/format,webp', '', $val);
            }
        }
        $this->attributes['imgs'] = collect($value);
    }

    public function getImgsAttribute($value)
    {
        $value = json_decode($value, true);
        if (Setting::get('is_webp') && config('visit') == 'frontend' && is_array($value)) {
            foreach ($value as $key => $val) {
                $value[$key] = $val . '?x-oss-process=image/format,webp';
            }
        }

        return $value;
    }

}
