Refactor models to use new attribute cast

This commit is contained in:
Jonny Barnes 2022-11-26 10:50:19 +00:00
parent a8de52077f
commit cfca6a1de5
Signed by: jonny
SSH key fingerprint: SHA256:CTuSlns5U7qlD9jqHvtnVmfYV3Zwl2Z7WnJ4/dqOaL8
9 changed files with 218 additions and 292 deletions

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Models;
use App\Traits\FilterHtml;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
@ -17,46 +18,38 @@ class Like extends Model
protected $fillable = ['url'];
/**
* Normalize the URL of a Like.
*
* @param string $value The provided URL
*/
public function setUrlAttribute(string $value)
protected function url(): Attribute
{
$this->attributes['url'] = normalize_url($value);
return Attribute::set(
set: fn ($value) => normalize_url($value),
);
}
/**
* Normalize the URL of the author of the like.
*
* @param string|null $value The authors url
*/
public function setAuthorUrlAttribute(?string $value)
protected function authorUrl(): Attribute
{
$this->attributes['author_url'] = normalize_url($value);
return Attribute::set(
set: fn ($value) => 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
protected function content(): Attribute
{
if ($value === null) {
return null;
}
return Attribute::get(
get: function ($value, $attributes) {
if ($value === null) {
return null;
}
$mf2 = Mf2\parse($value, $this->url);
$mf2 = Mf2\parse($value, $attributes['url']);
if (Arr::get($mf2, 'items.0.properties.content.0.html')) {
return $this->filterHtml(
$mf2['items'][0]['properties']['content'][0]['html']
);
}
if (Arr::get($mf2, 'items.0.properties.content.0.html')) {
return $this->filterHtml(
$mf2['items'][0]['properties']['content'][0]['html']
);
}
return $value;
return $value;
}
);
}
}