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 "\n" . Html::beginTag('div', $this->options) |
36
|
|
|
. "\n" . Html::beginTag('div', ['class' => 'card-body d-flex flex-column align-items-start']) |
37
|
|
|
. "\n" . $this->renderHead() |
38
|
|
|
. "\n" . $this->renderBody() |
39
|
|
|
. "\n" . $this->renderTags() |
40
|
|
|
. "\n" . Html::endTag('div') |
41
|
|
|
. "\n" . Html::endTag('div') |
42
|
|
|
. "\n"; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function renderHead(): string |
46
|
|
|
{ |
47
|
|
|
return Html::a( |
48
|
|
|
Html::encode($this->post->getTitle()), |
|
|
|
|
49
|
|
|
$this->urlGenerator->generate('blog/post', ['slug' => $this->post->getSlug()]), |
50
|
|
|
['class' => 'mb-0 h4 text-decoration-none'] // stretched-link |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function renderBody(): string |
55
|
|
|
{ |
56
|
|
|
return Html::tag( |
57
|
|
|
'div', |
58
|
|
|
$this->post->getPublishedAt()->format('M, d') . ' by ' . Html::a( |
59
|
|
|
Html::encode($this->post->getUser()->getLogin()), |
60
|
|
|
$this->urlGenerator->generate('user/profile', ['login' => $this->post->getUser()->getLogin()]) |
61
|
|
|
), |
62
|
|
|
['class' => 'mb-1 text-muted'] |
63
|
|
|
) . Html::tag( |
64
|
|
|
'p', |
65
|
|
|
Html::encode(mb_substr($this->post->getContent(), 0, 400)) |
66
|
|
|
. (mb_strlen($this->post->getContent()) > 400 ? '…' : ''), |
67
|
|
|
['class' => 'card-text mb-auto'] |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function renderTags(): string |
72
|
|
|
{ |
73
|
|
|
$return = Html::beginTag('div', ['class' => 'mt-3']); |
74
|
|
|
foreach ($this->post->getTags() as $tag) { |
75
|
|
|
$return .= Html::a( |
76
|
|
|
Html::encode($tag->getLabel()), |
77
|
|
|
$this->urlGenerator->generate('blog/tag', ['label' => $tag->getLabel()]), |
78
|
|
|
['class' => 'btn btn-outline-secondary btn-sm mx-1 mt-1'] |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
return $return . Html::endTag('div'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function initOptions(): void |
85
|
|
|
{ |
86
|
|
|
Html::addCssClass($this->options, ['widget' => 'card mb-4']); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function post(?Post $post): self |
90
|
|
|
{ |
91
|
|
|
$this->post = $post; |
92
|
|
|
|
93
|
|
|
if ($post !== null) { |
94
|
|
|
$this->options['data']['post-slug'] = $post->getSlug(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* The HTML attributes for the widget container tag. The following special options are recognized. |
102
|
|
|
* |
103
|
|
|
* {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered. |
104
|
|
|
*/ |
105
|
|
|
public function options(array $value): self |
106
|
|
|
{ |
107
|
|
|
$this->options = $value; |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
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.