<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;

class Article extends Model
{
    use HasFactory;
    protected $table='article';
    protected $guarded=[];
    protected $appends =['all_audio','format_duration'];

    protected function serializeDate(DateTimeInterface $date): string
    {
        return $date->format('Y-m-d H:i:s');
    }
    public function getFormatDurationAttribute($key)
    {
        $seconds=$this->attributes['duration'];
        if ($seconds >= 3600) {
            $hours = intval($seconds / 3600);
            $time = $hours . ":" . gmstrftime('%M', $seconds) .":". gmstrftime('%S',ceil(fmod($seconds , 60)));
        } else {
            $time = gmstrftime('%M', $seconds) .":". gmstrftime('%S',ceil(fmod($seconds , 60)));
        }
        return $time;
    }
    public function getAllAudioAttribute($key)
    {
        $domain='https://weiliao.woaidakele.cn/storage/';
        $audio=$this->attributes['audio'];
        if(!empty($audio)){
            return $domain.$audio;
        }else{
            return '';
        }
    }
}
