Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| Images | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
6 | |
100.00% |
1 / 1 |
| render | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Block; |
| 6 | |
| 7 | use Cosray\Contract\Block; |
| 8 | use Cosray\Field; |
| 9 | use Cosray\Schema\Label; |
| 10 | use Cosray\Schema\Required; |
| 11 | use Cosray\Schema\Translate; |
| 12 | use Cosray\Value\Block as BlockValue; |
| 13 | |
| 14 | use function Cosray\escape; |
| 15 | |
| 16 | #[Label('block:images')] |
| 17 | final class Images implements Block |
| 18 | { |
| 19 | private const string THUMB = 'block-thumb'; |
| 20 | |
| 21 | #[Label('block:images'), Required, Translate] |
| 22 | protected Field\Image $images; |
| 23 | |
| 24 | public function render(BlockValue $block, RenderContext $ctx): string |
| 25 | { |
| 26 | $name = (string) ($ctx->args['thumbSize'] ?? self::THUMB); |
| 27 | // Validate the name up front — a typo should fail the render, |
| 28 | // not silently emit URLs the fallback route will 404. |
| 29 | $ctx->owner->config()->media->sizes->get($name); |
| 30 | $prefix = $ctx->prefix(); |
| 31 | $images = $block->images; |
| 32 | $result = ''; |
| 33 | |
| 34 | foreach ($images->unwrap() as $index => $item) { |
| 35 | $asset = $ctx->asset((string) ($item['uid'] ?? '')); |
| 36 | |
| 37 | if ($asset === null) { |
| 38 | continue; |
| 39 | } |
| 40 | |
| 41 | $image = $images->get($index); |
| 42 | $alt = escape($image->alt() ?: strip_tags($image->title())); |
| 43 | $path = escape($asset->path()); |
| 44 | $url = $asset->resizable() ? escape($asset->sizePath($name)) : $path; |
| 45 | |
| 46 | $result .= "<div class=\"{$prefix}-blocks-images-image\"><img src=\"{$url}\" alt=\"{$alt}\" data-path-original=\"{$path}\"></div>"; |
| 47 | } |
| 48 | |
| 49 | return $result === '' ? '' : "<div class=\"{$prefix}-blocks-images\">{$result}</div>"; |
| 50 | } |
| 51 | } |