<?php

namespace App\Models;

use App\Models\Traits\BelongsToCategory;
use App\Models\Traits\HasOneCover;

class Article extends Model
{

    use HasOneCover, BelongsToCategory;

    public function getLinkAttribute()
    {
        return route('article.show', $this);
    }
    
    public function get_content_cover()
    {
        preg_match("/<img.*?src=\"([^\"]+)\"[^>].*?>/isU", str_ireplace("\\", "", $this->content), $matches);

        if (isset($matches[1])) {
            return $matches[1];
        } else {
            return '';
        }
    }

    /**
     * Notes: 获取一个默认图片
     * @Author: 玄尘
     * @Date  : 2020/6/3 16:29
     * @return mixed|string
     */
    public function get_one_cover()
    {
        if ($this->cover_path) {
            $path = $this->cover_path;
        } else {
            $path = $this->get_content_cover();
            // if ($path) {
            //     $this->cover = str_replace("/storage", "", $path);
            //     $this->save();
            // }
            $path = config('app.url') . $path;

        }

        return $path;

    }

}
