Skip to content

Commit 2aff7bf

Browse files
committed
feat: add fetchpriority attribute to Image class and update related tests
1 parent 494018b commit 2aff7bf

File tree

3 files changed

+76
-18
lines changed

3 files changed

+76
-18
lines changed

src/Image.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,18 @@ public function __construct(
3737
protected ?int $height = null,
3838
protected string $format = 'web',
3939
protected string $loading = 'lazy',
40-
protected string $decoding = 'async'
40+
protected string $decoding = 'async',
41+
protected string $fetchpriority = 'auto'
4142
) {
4243
}
4344

44-
public static function attributes(string|Responsive $src, $alt, $width = null, $height = null, $format = 'webp', $loading = 'lazy', $decoding = 'async'): string
45+
public static function attributes(string|Responsive $src, $alt, $width = null, $height = null, $format = 'webp', $loading = 'lazy', $decoding = 'async', $fetchpriority = 'auto'): string
4546
{
4647

47-
return (new self($src, $alt, $width, $height, $format, $loading, $decoding))->toAttributes();
48+
return (new self($src, $alt, $width, $height, $format, $loading, $decoding, $fetchpriority))->toAttributes();
4849
}
4950

50-
public static function fromObject(object $image, int $width, int $height, ?string $alt = null, string $format = 'webp', string $loading = 'lazy', string $decoding = 'async'): self
51+
public static function fromObject(object $image, int $width, int $height, ?string $alt = null, string $format = 'webp', string $loading = 'lazy', string $decoding = 'async', string $fetchpriority = 'auto'): self
5152
{
5253
if (!property_exists($image, 'source') || empty($image->source)) {
5354
throw new InvalidArgumentException('Image object must have a non-empty "source" property.');
@@ -57,7 +58,11 @@ public static function fromObject(object $image, int $width, int $height, ?strin
5758
$image->source,
5859
(property_exists($image, 'caption') && !empty($image->caption)) ? $image->caption : $alt,
5960
$width,
60-
$height
61+
$height,
62+
$format,
63+
$loading,
64+
$decoding,
65+
$fetchpriority
6166
);
6267
}
6368

@@ -67,9 +72,9 @@ public static function source(string $src, $width = null, $height = null, $forma
6772
return $image->getSrc();
6873
}
6974

70-
public static function tag(string|Responsive $src, $alt, $width = null, $height = null, $format = 'webp', $loading = 'lazy', $decoding = 'async', array $options = []): string
75+
public static function tag(string|Responsive $src, $alt, $width = null, $height = null, $format = 'webp', $loading = 'lazy', $decoding = 'async', array $options = [], $fetchpriority = 'auto'): string
7176
{
72-
$attributes = self::attributes($src, $alt, $width, $height, $format, $loading, $decoding);
77+
$attributes = self::attributes($src, $alt, $width, $height, $format, $loading, $decoding, $fetchpriority);
7378

7479
foreach ($options as $key => $value) {
7580
$attributes .= sprintf(' %s="%s"', $key, $value);
@@ -178,6 +183,26 @@ public function getLoading(): string
178183
return strtolower($this->loading) === 'lazy' ? 'lazy' : 'eager';
179184
}
180185

186+
public function setFetchpriority(string $fetchpriority): self
187+
{
188+
$this->fetchpriority = $fetchpriority;
189+
return $this;
190+
}
191+
192+
/**
193+
* @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/fetchPriority
194+
*/
195+
public function getFetchpriority(): string
196+
{
197+
$fetchpriority = strtolower($this->fetchpriority);
198+
199+
if (!in_array($fetchpriority, ['high', 'low', 'auto'])) {
200+
$fetchpriority = 'auto';
201+
}
202+
203+
return $fetchpriority;
204+
}
205+
181206
public function setWidth($width): self
182207
{
183208
$this->width = $width;
@@ -196,6 +221,7 @@ public function toAttributes(): string
196221
sprintf('alt="%s"', $this->getAlt()),
197222
sprintf('loading="%s"', $this->getLoading()),
198223
sprintf('decoding="%s"', $this->getDecoding()),
224+
sprintf('fetchpriority="%s"', $this->getFetchpriority()),
199225
];
200226

201227
if ($this->getwidth()) {

tests/ImageTest.php

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ class ImageTest extends TestCase
77
{
88
public function testAltTagEncoding()
99
{
10-
$this->assertSame('<img src="https://storage.flyo.cloud/foo.jpg?format=webp" alt="&lt;h1&gt;hoi&lt;/h1&gt;" loading="lazy" decoding="async" />', Image::tag('foo.jpg', '<h1>hoi</h1>'));
10+
$this->assertSame('<img src="https://storage.flyo.cloud/foo.jpg?format=webp" alt="&lt;h1&gt;hoi&lt;/h1&gt;" loading="lazy" decoding="async" fetchpriority="auto" />', Image::tag('foo.jpg', '<h1>hoi</h1>'));
1111
}
1212

1313
public function testDefaultValues()
1414
{
15-
$this->assertSame('src="https://storage.flyo.cloud/foo.png?format=webp" alt="alt" loading="lazy" decoding="async"', Image::attributes('foo.png', 'alt'));
15+
$this->assertSame('src="https://storage.flyo.cloud/foo.png?format=webp" alt="alt" loading="lazy" decoding="async" fetchpriority="auto"', Image::attributes('foo.png', 'alt'));
1616
}
1717

1818
public function testExternalOtherSource()
@@ -31,8 +31,8 @@ public function testFromCaptionButOwnAltTag()
3131
], 100, 100, 'Own Alt Tag');
3232

3333
$this->assertSame('https://storage.flyo.cloud/test.jpg/thumb/100x100?format=webp', $image->getSrc());
34-
$this->assertSame('src="https://storage.flyo.cloud/test.jpg/thumb/100x100?format=webp" alt="Own Alt Tag" loading="lazy" decoding="async" width="100" height="100"', $image->toAttributes());
35-
$this->assertSame('<img src="https://storage.flyo.cloud/test.jpg/thumb/100x100?format=webp" alt="Own Alt Tag" loading="lazy" decoding="async" width="100" height="100" />', $image->toTag());
34+
$this->assertSame('src="https://storage.flyo.cloud/test.jpg/thumb/100x100?format=webp" alt="Own Alt Tag" loading="lazy" decoding="async" fetchpriority="auto" width="100" height="100"', $image->toAttributes());
35+
$this->assertSame('<img src="https://storage.flyo.cloud/test.jpg/thumb/100x100?format=webp" alt="Own Alt Tag" loading="lazy" decoding="async" fetchpriority="auto" width="100" height="100" />', $image->toTag());
3636
}
3737

3838
public function testFromCaptionButOwnAltTagWithEncoding()
@@ -42,7 +42,7 @@ public function testFromCaptionButOwnAltTagWithEncoding()
4242
], 100, 100, '"Test Encoding"');
4343

4444
$this->assertSame('&quot;Test Encoding&quot;', $image->getAlt());
45-
$this->assertSame('<img src="https://storage.flyo.cloud/test.jpg/thumb/100x100?format=webp" alt="&quot;Test Encoding&quot;" loading="lazy" decoding="async" width="100" height="100" />', $image->toTag());
45+
$this->assertSame('<img src="https://storage.flyo.cloud/test.jpg/thumb/100x100?format=webp" alt="&quot;Test Encoding&quot;" loading="lazy" decoding="async" fetchpriority="auto" width="100" height="100" />', $image->toTag());
4646
}
4747

4848
public function testFromObject()
@@ -53,8 +53,8 @@ public function testFromObject()
5353
], 100, 100);
5454

5555
$this->assertSame('https://storage.flyo.cloud/test.jpg/thumb/100x100?format=webp', $image->getSrc());
56-
$this->assertSame('src="https://storage.flyo.cloud/test.jpg/thumb/100x100?format=webp" alt="Test Image" loading="lazy" decoding="async" width="100" height="100"', $image->toAttributes());
57-
$this->assertSame('<img src="https://storage.flyo.cloud/test.jpg/thumb/100x100?format=webp" alt="Test Image" loading="lazy" decoding="async" width="100" height="100" />', $image->toTag());
56+
$this->assertSame('src="https://storage.flyo.cloud/test.jpg/thumb/100x100?format=webp" alt="Test Image" loading="lazy" decoding="async" fetchpriority="auto" width="100" height="100"', $image->toAttributes());
57+
$this->assertSame('<img src="https://storage.flyo.cloud/test.jpg/thumb/100x100?format=webp" alt="Test Image" loading="lazy" decoding="async" fetchpriority="auto" width="100" height="100" />', $image->toTag());
5858
}
5959

6060
public function testImageConstructor()
@@ -68,14 +68,14 @@ public function testImageOptions()
6868
{
6969
$tag = Image::tag('test.jpg', 'Test Image', 100, 200, 'png', 'auto', 'sync', ['class' => 'img-fluid', 'foo' => 1]);
7070

71-
$this->assertEquals('<img src="https://storage.flyo.cloud/test.jpg/thumb/100x200?format=png" alt="Test Image" loading="eager" decoding="sync" width="100" height="200" class="img-fluid" foo="1" />', $tag);
71+
$this->assertEquals('<img src="https://storage.flyo.cloud/test.jpg/thumb/100x200?format=png" alt="Test Image" loading="eager" decoding="sync" fetchpriority="auto" width="100" height="200" class="img-fluid" foo="1" />', $tag);
7272
}
7373

7474
public function testImageTag()
7575
{
7676
$tag = Image::tag('test.jpg', 'Test Image', 100, 200, 'png', 'auto', 'sync');
7777

78-
$this->assertEquals('<img src="https://storage.flyo.cloud/test.jpg/thumb/100x200?format=png" alt="Test Image" loading="eager" decoding="sync" width="100" height="200" />', $tag);
78+
$this->assertEquals('<img src="https://storage.flyo.cloud/test.jpg/thumb/100x200?format=png" alt="Test Image" loading="eager" decoding="sync" fetchpriority="auto" width="100" height="200" />', $tag);
7979
}
8080

8181
public function testSourceOnly()
@@ -85,6 +85,38 @@ public function testSourceOnly()
8585
$this->assertSame('https://storage.flyo.cloud/foobar.jpg', Image::source('foobar.jpg', null, null, 'jpg'));
8686
}
8787

88+
public function testFetchpriority()
89+
{
90+
// Test default value (auto)
91+
$image = new Image('test.jpg', 'Test Image');
92+
$this->assertSame('auto', $image->getFetchpriority());
93+
94+
// Test high priority
95+
$image = new Image('test.jpg', 'Test Image', null, null, 'webp', 'lazy', 'async', 'high');
96+
$this->assertSame('high', $image->getFetchpriority());
97+
98+
// Test low priority
99+
$image = new Image('test.jpg', 'Test Image', null, null, 'webp', 'lazy', 'async', 'low');
100+
$this->assertSame('low', $image->getFetchpriority());
101+
102+
// Test invalid value falls back to auto
103+
$image = new Image('test.jpg', 'Test Image', null, null, 'webp', 'lazy', 'async', 'invalid');
104+
$this->assertSame('auto', $image->getFetchpriority());
105+
106+
// Test setter
107+
$image = new Image('test.jpg', 'Test Image');
108+
$image->setFetchpriority('high');
109+
$this->assertSame('high', $image->getFetchpriority());
110+
111+
// Test in tag output with high priority
112+
$tag = Image::tag('test.jpg', 'Test Image', 100, 100, 'webp', 'lazy', 'async', [], 'high');
113+
$this->assertStringContainsString('fetchpriority="high"', $tag);
114+
115+
// Test in attributes output with low priority
116+
$attributes = Image::attributes('test.jpg', 'Test Image', 100, 100, 'webp', 'lazy', 'async', 'low');
117+
$this->assertStringContainsString('fetchpriority="low"', $attributes);
118+
}
119+
88120
/*
89121
public function testGetSrc()
90122
{

tests/ResponsiveImageTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function testImageConstructor()
1515

1616
$image = Image::attributes($responsive, 'Test Image', 100, 200, 'png', 'auto', 'sync');
1717

18-
$this->assertEquals('src="https://storage.flyo.cloud/test.jpg/thumb/100x200?format=png" alt="Test Image" loading="eager" decoding="sync" width="100" height="200" srcset="https://storage.flyo.cloud/test.jpg/thumb/400x400?format=png 400w, https://storage.flyo.cloud/test.jpg/thumb/1200xnull?format=png 1000w" sizes="(max-width: 400px) 33vw, (min-width: 1000px) 83vw, 100vw"', $image);
18+
$this->assertEquals('src="https://storage.flyo.cloud/test.jpg/thumb/100x200?format=png" alt="Test Image" loading="eager" decoding="sync" fetchpriority="auto" width="100" height="200" srcset="https://storage.flyo.cloud/test.jpg/thumb/400x400?format=png 400w, https://storage.flyo.cloud/test.jpg/thumb/1200xnull?format=png 1000w" sizes="(max-width: 400px) 33vw, (min-width: 1000px) 83vw, 100vw"', $image);
1919
}
2020

2121
public function testSingleResponsiveImageWithRawOriginalSizeFallback()
@@ -25,6 +25,6 @@ public function testSingleResponsiveImageWithRawOriginalSizeFallback()
2525

2626
$image = Image::attributes($responsive, 'Test Image', 1000, 800, 'png', 'auto', 'sync');
2727

28-
$this->assertEquals('src="https://storage.flyo.cloud/test.jpg/thumb/1000x800?format=png" alt="Test Image" loading="eager" decoding="sync" width="1000" height="800" srcset="https://storage.flyo.cloud/test.jpg/thumb/400x400?format=png 400w, https://storage.flyo.cloud/test.jpg/thumb/1000x800?format=png 1000w" sizes="(max-width: 400px) 40vw, 100vw"', $image);
28+
$this->assertEquals('src="https://storage.flyo.cloud/test.jpg/thumb/1000x800?format=png" alt="Test Image" loading="eager" decoding="sync" fetchpriority="auto" width="1000" height="800" srcset="https://storage.flyo.cloud/test.jpg/thumb/400x400?format=png 400w, https://storage.flyo.cloud/test.jpg/thumb/1000x800?format=png 1000w" sizes="(max-width: 400px) 40vw, 100vw"', $image);
2929
}
3030
}

0 commit comments

Comments
 (0)