|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Blog\Widget; |
|
6
|
|
|
|
|
7
|
|
|
use App\Blog\Entity\Post; |
|
8
|
|
|
use Yiisoft\Router\UrlGeneratorInterface; |
|
9
|
|
|
use Yiisoft\Yii\Bootstrap4\Html; |
|
10
|
|
|
use Yiisoft\Yii\Bootstrap4\Widget; |
|
11
|
|
|
|
|
12
|
|
|
class PostCard extends Widget |
|
13
|
|
|
{ |
|
14
|
|
|
private ?Post $post = null; |
|
15
|
|
|
|
|
16
|
|
|
private array $options = []; |
|
17
|
|
|
|
|
18
|
|
|
private UrlGeneratorInterface $urlGenerator; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(UrlGeneratorInterface $urlGenerator) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->urlGenerator = $urlGenerator; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
protected function run(): string |
|
26
|
|
|
{ |
|
27
|
|
|
if (!isset($this->options['id'])) { |
|
28
|
|
|
$this->options['id'] = "{$this->getId()}-post-card"; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$this->initOptions(); |
|
32
|
|
|
|
|
33
|
|
|
$this->registerPlugin('page-card', $this->options); |
|
34
|
|
|
|
|
35
|
|
|
return implode("\n", [ |
|
36
|
|
|
Html::beginTag('div', $this->options), |
|
37
|
|
|
Html::beginTag('div', ['class' => 'card-body d-flex flex-column align-items-start']), |
|
38
|
|
|
$this->renderHead(), |
|
39
|
|
|
$this->renderBody(), |
|
40
|
|
|
$this->renderTags(), |
|
41
|
|
|
Html::endTag('div'), |
|
42
|
|
|
Html::endTag('div'), |
|
43
|
|
|
]); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
protected function renderHead(): string |
|
47
|
|
|
{ |
|
48
|
|
|
return Html::a( |
|
49
|
|
|
Html::encode($this->post->getTitle()), |
|
|
|
|
|
|
50
|
|
|
$this->urlGenerator->generate('blog/post', ['slug' => $this->post->getSlug()]), |
|
51
|
|
|
['class' => 'mb-0 h4 text-decoration-none'] // stretched-link |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
protected function renderBody(): string |
|
56
|
|
|
{ |
|
57
|
|
|
return Html::tag( |
|
58
|
|
|
'div', |
|
59
|
|
|
$this->post->getPublishedAt()->format('M, d') . ' by ' . Html::a( |
|
60
|
|
|
Html::encode($this->post->getUser()->getLogin()), |
|
61
|
|
|
$this->urlGenerator->generate('user/profile', ['login' => $this->post->getUser()->getLogin()]) |
|
62
|
|
|
), |
|
63
|
|
|
['class' => 'mb-1 text-muted'] |
|
64
|
|
|
) . Html::tag( |
|
65
|
|
|
'p', |
|
66
|
|
|
Html::encode(mb_substr($this->post->getContent(), 0, 400)) |
|
67
|
|
|
. (mb_strlen($this->post->getContent()) > 400 ? '…' : ''), |
|
68
|
|
|
['class' => 'card-text mb-auto'] |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function renderTags(): string |
|
73
|
|
|
{ |
|
74
|
|
|
$return = Html::beginTag('div', ['class' => 'mt-3']); |
|
75
|
|
|
foreach ($this->post->getTags() as $tag) { |
|
76
|
|
|
$return .= Html::a( |
|
77
|
|
|
Html::encode($tag->getLabel()), |
|
78
|
|
|
$this->urlGenerator->generate('blog/tag', ['label' => $tag->getLabel()]), |
|
79
|
|
|
['class' => 'btn btn-outline-secondary btn-sm mx-1 mt-1'] |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
return $return . Html::endTag('div'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
protected function initOptions(): void |
|
86
|
|
|
{ |
|
87
|
|
|
Html::addCssClass($this->options, ['widget' => 'card mb-4']); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function post(?Post $post): self |
|
91
|
|
|
{ |
|
92
|
|
|
$this->post = $post; |
|
93
|
|
|
|
|
94
|
|
|
if ($post !== null) { |
|
95
|
|
|
$this->options['data']['post-slug'] = $post->getSlug(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* The HTML attributes for the widget container tag. The following special options are recognized. |
|
103
|
|
|
* |
|
104
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
|
105
|
|
|
*/ |
|
106
|
|
|
public function options(array $value): self |
|
107
|
|
|
{ |
|
108
|
|
|
$this->options = $value; |
|
109
|
|
|
|
|
110
|
|
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.