engelsystem/src/Models/News.php

88 lines
2.7 KiB
PHP
Raw Normal View History

<?php
2019-11-12 22:04:39 +01:00
declare(strict_types=1);
namespace Engelsystem\Models;
use Carbon\Carbon;
use Engelsystem\Models\User\UsesUserModel;
2019-11-12 21:49:43 +01:00
use Illuminate\Database\Eloquent\Collection;
2021-07-10 00:59:20 +02:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2019-11-12 21:49:43 +01:00
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Query\Builder as QueryBuilder;
2021-12-10 22:24:18 +01:00
use Illuminate\Support\Str;
/**
2019-11-12 21:49:43 +01:00
* @property int $id
* @property string $title
* @property string $text
* @property bool $is_highlighted
2019-11-12 21:49:43 +01:00
* @property bool $is_meeting
2020-12-27 02:51:05 +01:00
* @property bool $is_pinned
2019-11-12 21:49:43 +01:00
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
*
* @property-read Collection|NewsComment[] $comments
2021-12-10 22:24:18 +01:00
* @property-read int|null $comments_count
*
2023-02-13 21:19:45 +01:00
* @method static QueryBuilder|News[] whereId($value)
* @method static QueryBuilder|News[] whereTitle($value)
* @method static QueryBuilder|News[] whereText($value)
* @method static QueryBuilder|News[] whereIsMeeting($value)
* @method static QueryBuilder|News[] whereIsPinned($value)
* @method static QueryBuilder|News[] whereIsHighlighted($value)
2023-02-13 21:19:45 +01:00
* @method static QueryBuilder|News[] whereCreatedAt($value)
* @method static QueryBuilder|News[] whereUpdatedAt($value)
*/
class News extends BaseModel
{
2021-07-10 00:59:20 +02:00
use HasFactory;
use UsesUserModel;
/** @var bool Enable timestamps */
public $timestamps = true; // phpcs:ignore
/** @var array<string, string> */
protected $casts = [ // phpcs:ignore
'user_id' => 'integer',
'is_meeting' => 'boolean',
'is_pinned' => 'boolean',
'is_highlighted' => 'boolean',
];
/** @var array<string, bool> Default attributes */
protected $attributes = [ // phpcs:ignore
'is_meeting' => false,
'is_pinned' => false,
'is_highlighted' => false,
];
/** @var array<string> */
protected $fillable = [ // phpcs:ignore
'title',
'text',
'is_meeting',
2020-12-27 02:51:05 +01:00
'is_pinned',
'is_highlighted',
'user_id',
];
2019-11-12 21:49:43 +01:00
public function comments(): HasMany
{
return $this->hasMany(NewsComment::class)
->orderBy('created_at');
}
2021-12-10 22:24:18 +01:00
public function text(bool $showMore = true): string
{
if ($showMore || !Str::contains($this->text, 'more')) {
// Remove more tag
return preg_replace('/(.*)\[\s*more\s*\](.*)/is', '$1$2', $this->text);
}
// Only show text before more tag
$text = preg_replace('/(.*)(\s*\[\s*more\s*\].*)/is', '$1', $this->text);
return rtrim($text);
}
}