jonnybarnes.uk/app/Models/Like.php

62 lines
1.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
namespace App\Models;
use App\Traits\FilterHtml;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Mf2;
class Like extends Model
{
use FilterHtml;
use HasFactory;
protected $fillable = ['url'];
/**
* Normalize the URL of a Like.
*
* @param string $value The provided URL
*/
public function setUrlAttribute(string $value)
{
$this->attributes['url'] = normalize_url($value);
}
/**
* Normalize the URL of the author of the like.
*
* @param string|null $value The authors url
*/
public function setAuthorUrlAttribute(?string $value)
{
$this->attributes['author_url'] = normalize_url($value);
}
/**
* If the content contains HTML, filter it.
*
* @param string|null $value The content of the like
* @return string|null
*/
public function getContentAttribute(?string $value): ?string
{
if ($value === null) {
return null;
}
$mf2 = Mf2\parse($value, $this->url);
if (Arr::get($mf2, 'items.0.properties.content.0.html')) {
return $this->filterHtml(
$mf2['items'][0]['properties']['content'][0]['html']
);
}
return $value;
}
}