Skip to content

Commit a4ac739

Browse files
committed
Passing a list of objects allows for more precise types
Fixes ``` ------ ---------- --------------------------------------------------------------------------------------------------------------------------------- Line Compiled Www/Presenters/templates/Photo/default.latte rendered from MichalSpacekCz\Www\Presenters\PhotoPresenter::default line See compiled template: /tmp/phpstan-latte/app/Www/Presenters/templates/Photo/default.latte.b89322339c0f942538268c58cb66978a.php ------ ---------- --------------------------------------------------------------------------------------------------------------------------------- 8 184 Binary operation "." between 'photos/' and array<string, string>|Nette\Utils\Html|string results in an error. 12 210 Argument of an invalid type array<string, string>|Nette\Utils\Html|string supplied for foreach, only iterables are supported. ------ ---------- --------------------------------------------------------------------------------------------------------------------------------- ```
1 parent 2b89e90 commit a4ac739

File tree

3 files changed

+79
-28
lines changed

3 files changed

+79
-28
lines changed

site/app/Media/Photo.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace MichalSpacekCz\Media;
5+
6+
use Nette\Utils\Html;
7+
8+
class Photo
9+
{
10+
11+
/**
12+
* @param array<string, string> $sizes
13+
*/
14+
public function __construct(
15+
private readonly string $title,
16+
private readonly string $file,
17+
private readonly string|Html $description,
18+
private readonly array $sizes,
19+
) {
20+
}
21+
22+
23+
public function getTitle(): string
24+
{
25+
return $this->title;
26+
}
27+
28+
29+
public function getFile(): string
30+
{
31+
return $this->file;
32+
}
33+
34+
35+
public function getDescription(): string|Html
36+
{
37+
return $this->description;
38+
}
39+
40+
41+
/**
42+
* @return array<string, string>
43+
*/
44+
public function getSizes(): array
45+
{
46+
return $this->sizes;
47+
}
48+
49+
}

site/app/Www/Presenters/PhotoPresenter.php

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace MichalSpacekCz\Www\Presenters;
55

66
use MichalSpacekCz\Formatter\TexyFormatter;
7+
use MichalSpacekCz\Media\Photo;
78

89
class PhotoPresenter extends BasePresenter
910
{
@@ -18,44 +19,44 @@ public function __construct(
1819
public function renderDefault(): void
1920
{
2021
$photos = [
21-
[
22-
'header' => $this->translator->translate('messages.photo.trademark.header'),
23-
'file' => 'michalspacek-trademark-400x268.jpg',
24-
'desc' => $this->texyFormatter->translate('messages.photo.trademark.desc', ['https://twitter.com/spazef0rze', 'https://www.facebook.com/spaze', 'https://about.me/lukashudecek']),
25-
'sizes' => [
22+
new Photo(
23+
$this->translator->translate('messages.photo.trademark.header'),
24+
'michalspacek-trademark-400x268.jpg',
25+
$this->texyFormatter->translate('messages.photo.trademark.desc', ['https://twitter.com/spazef0rze', 'https://www.facebook.com/spaze', 'https://about.me/lukashudecek']),
26+
[
2627
'400×268' => 'michalspacek-trademark-400x268.jpg',
2728
'800×536' => 'michalspacek-trademark-800x536.jpg',
2829
'1600×1071' => 'michalspacek-trademark-1600x1071.jpg',
2930
],
30-
],
31-
[
32-
'header' => $this->translator->translate('messages.photo.lecturer.header'),
33-
'file' => 'michalspacek-codecamp2015-400x268.jpg',
34-
'desc' => $this->texyFormatter->translate('messages.photo.lecturer.desc', ['https://galerie.fotohavlin.cz/']),
35-
'sizes' => [
31+
),
32+
new Photo(
33+
$this->translator->translate('messages.photo.lecturer.header'),
34+
'michalspacek-codecamp2015-400x268.jpg',
35+
$this->texyFormatter->translate('messages.photo.lecturer.desc', ['https://galerie.fotohavlin.cz/']),
36+
[
3637
'400×268' => 'michalspacek-codecamp2015-400x268.jpg',
3738
'800×536' => 'michalspacek-codecamp2015-800x536.jpg',
3839
'1600×1071' => 'michalspacek-codecamp2015-1600x1071.jpg',
3940
],
40-
],
41-
[
42-
'header' => $this->translator->translate('messages.photo.office.header'),
43-
'file' => 'michalspacek-serious-400x268.jpg',
44-
'desc' => $this->translator->translate('messages.photo.office.desc'),
45-
'sizes' => [
41+
),
42+
new Photo(
43+
$this->translator->translate('messages.photo.office.header'),
44+
'michalspacek-serious-400x268.jpg',
45+
$this->translator->translate('messages.photo.office.desc'),
46+
[
4647
'400×268' => 'michalspacek-serious-400x268.jpg',
4748
'400×400' => 'michalspacek-serious-400x400.jpg',
4849
],
49-
],
50-
[
51-
'header' => $this->translator->translate('messages.photo.talk.header'),
52-
'file' => 'michalspacek-webtop100-400x268.jpg',
53-
'desc' => $this->texyFormatter->translate('messages.photo.talk.desc', ['link:Www:Talks:talk technicke-chyby-vas-pripravi-o-penize-webtop100']),
54-
'sizes' => [
50+
),
51+
new Photo(
52+
$this->translator->translate('messages.photo.talk.header'),
53+
'michalspacek-webtop100-400x268.jpg',
54+
$this->texyFormatter->translate('messages.photo.talk.desc', ['link:Www:Talks:talk technicke-chyby-vas-pripravi-o-penize-webtop100']),
55+
[
5556
'400×268' => 'michalspacek-webtop100-400x268.jpg',
5657
'720×480' => 'michalspacek-webtop100-720x480.jpg',
5758
],
58-
],
59+
),
5960
];
6061
$this->template->photos = $photos;
6162
$this->template->pageTitle = $this->translator->translate('messages.title.photo');

site/app/Www/Presenters/templates/Photo/default.latte

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
{varType MichalSpacekCz\Media\Photo[] $photos}
12
{define #menu}
23
&raquo; <a n:href="Homepage:">Michal Špaček</a>
34
{/define}
45

56
{define #content}
67
{foreach $photos as $photo}
7-
<h3>{$photo['header']}</h3>
8-
<p><img src="{='photos/' . $photo['file']|staticImageUrl}" width="400" height="268" alt="{$photo['header']}" title="{$photo['header']}"></p>
9-
<p n:ifset="$photo['desc']">{$photo['desc']}</p>
8+
<h3>{$photo->getTitle()}</h3>
9+
<p><img src="{='photos/' . $photo->getFile()|staticImageUrl}" width="400" height="268" alt="{$photo->getTitle()}" title="{$photo->getTitle()}"></p>
10+
<p>{$photo->getDescription()}</p>
1011
<p>
1112
<strong>{_messages.photo.downloadsizes}</strong>
12-
{foreach $photo['sizes'] as $size => $file}
13+
{foreach $photo->getSizes() as $size => $file}
1314
<a href="{='photos/' . $file|staticImageUrl}">{$size}</a>{sep}, {/sep}
1415
{/foreach}
1516
</p>

0 commit comments

Comments
 (0)