Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
84.21% |
16 / 19 |
|
50.00% |
3 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Image | |
84.21% |
16 / 19 |
|
50.00% |
3 / 6 |
11.48 | |
0.00% |
0 / 1 |
| __toString | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| tag | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| size | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| publicPath | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
4.07 | |||
| link | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| alt | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Value; |
| 6 | |
| 7 | use Cosray\Assets\SizeSpec; |
| 8 | |
| 9 | use function Cosray\escape; |
| 10 | |
| 11 | class Image extends File |
| 12 | { |
| 13 | protected ?SizeSpec $spec = null; |
| 14 | |
| 15 | public function __toString(): string |
| 16 | { |
| 17 | return $this->tag(); |
| 18 | } |
| 19 | |
| 20 | public function tag(?string $class = null): string |
| 21 | { |
| 22 | return sprintf( |
| 23 | '<img %ssrc="%s" alt="%s" data-path-original="%s">', |
| 24 | $class ? sprintf('class="%s" ', escape($class)) : '', |
| 25 | $this->url(), |
| 26 | escape($this->alt() ?: strip_tags($this->title())), |
| 27 | $this->asset($this->index)?->path() ?? '', |
| 28 | ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Use a named rendition from the `media.sizes` config. Unknown names |
| 33 | * throw immediately; assets the resize pipeline cannot process (SVG, |
| 34 | * video posters) keep their original URL. |
| 35 | */ |
| 36 | public function size(string $name): static |
| 37 | { |
| 38 | $new = clone $this; |
| 39 | $new->spec = $this->owner->config()->media->sizes->get($name); |
| 40 | |
| 41 | return $new; |
| 42 | } |
| 43 | |
| 44 | public function publicPath(): string |
| 45 | { |
| 46 | $asset = $this->asset($this->index); |
| 47 | |
| 48 | if ($asset === null) { |
| 49 | return ''; |
| 50 | } |
| 51 | |
| 52 | if ($this->spec !== null && $asset->resizable()) { |
| 53 | return $asset->sizePath($this->spec->name); |
| 54 | } |
| 55 | |
| 56 | return $asset->path(); |
| 57 | } |
| 58 | |
| 59 | public function link(): string |
| 60 | { |
| 61 | return $this->textValue('link', $this->index); |
| 62 | } |
| 63 | |
| 64 | public function alt(): string |
| 65 | { |
| 66 | return $this->textValue('alt', $this->index); |
| 67 | } |
| 68 | } |