Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
News
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 comments
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 text
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Models;
6
7use Carbon\Carbon;
8use Engelsystem\Models\User\UsesUserModel;
9use Illuminate\Database\Eloquent\Collection;
10use Illuminate\Database\Eloquent\Factories\HasFactory;
11use Illuminate\Database\Eloquent\Relations\HasMany;
12use Illuminate\Database\Query\Builder as QueryBuilder;
13use Illuminate\Support\Str;
14
15/**
16 * @property int                           $id
17 * @property string                        $title
18 * @property string                        $text
19 * @property bool                          $is_highlighted
20 * @property bool                          $is_meeting
21 * @property bool                          $is_pinned
22 * @property Carbon|null                   $created_at
23 * @property Carbon|null                   $updated_at
24 *
25 * @property-read Collection|NewsComment[] $comments
26 * @property-read int|null                 $comments_count
27 *
28 * @method static QueryBuilder|News[] whereId($value)
29 * @method static QueryBuilder|News[] whereTitle($value)
30 * @method static QueryBuilder|News[] whereText($value)
31 * @method static QueryBuilder|News[] whereIsMeeting($value)
32 * @method static QueryBuilder|News[] whereIsPinned($value)
33 * @method static QueryBuilder|News[] whereIsHighlighted($value)
34 * @method static QueryBuilder|News[] whereCreatedAt($value)
35 * @method static QueryBuilder|News[] whereUpdatedAt($value)
36 */
37class News extends BaseModel
38{
39    use HasFactory;
40    use UsesUserModel;
41
42    /** @var bool Enable timestamps */
43    public $timestamps = true; // phpcs:ignore
44
45    /** @var array<string, string> */
46    protected $casts = [ // phpcs:ignore
47        'user_id'        => 'integer',
48        'is_meeting'     => 'boolean',
49        'is_pinned'      => 'boolean',
50        'is_highlighted' => 'boolean',
51    ];
52
53    /** @var array<string, bool> Default attributes */
54    protected $attributes = [ // phpcs:ignore
55        'is_meeting'     => false,
56        'is_pinned'      => false,
57        'is_highlighted' => false,
58    ];
59
60    /** @var array<string> */
61    protected $fillable = [ // phpcs:ignore
62        'title',
63        'text',
64        'is_meeting',
65        'is_pinned',
66        'is_highlighted',
67        'user_id',
68    ];
69
70    public function comments(): HasMany
71    {
72        return $this->hasMany(NewsComment::class)
73            ->orderBy('created_at');
74    }
75
76    public function text(bool $showMore = true): string
77    {
78        if ($showMore || !Str::contains($this->text, 'more')) {
79            // Remove more tag
80            return preg_replace('/(.*)\[\s*more\s*\](.*)/is', '$1$2', $this->text);
81        }
82
83        // Only show text before more tag
84        $text = preg_replace('/(.*)(\s*\[\s*more\s*\].*)/is', '$1', $this->text);
85        return rtrim($text);
86    }
87}