Skip to content

Commit f279e40

Browse files
committed
add to and from methods
1 parent 4a1e0e9 commit f279e40

File tree

4 files changed

+86
-29
lines changed

4 files changed

+86
-29
lines changed

src/BreakpointType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ enum BreakpointType
1010

1111
public function srcset(int $screenSize, string $src): string
1212
{
13-
return "{$src} {$screenSize}w";
13+
return sprintf('%s %dw', $src, $screenSize);
1414
}
1515

1616
public function size(int $screenSize, int $maxWidth): string
1717
{
1818
$vw = floor(($screenSize / $maxWidth) * 100);
19-
return "({$this->width()}: {$screenSize}px) {$vw}vw";
19+
return sprintf('(%s: %dpx) %svw', $this->width(), $screenSize, $vw);
2020
}
2121

2222
public function width(): string

src/Image.php

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
class Image
3030
{
3131
public function __construct(
32-
protected string $src,
32+
protected string|Responsive $src,
3333
protected string $alt,
3434
protected ?int $width = null,
3535
protected ?int $height = null,
@@ -39,34 +39,46 @@ public function __construct(
3939
) {
4040
}
4141

42-
public static function attributes(string|Responsive $src, $alt, $width = null, $height = null, $format = 'webp', $loading = 'lazy', $decoding = 'async'): string
42+
public static function fromObject(object $image, int $width, int $height, ?string $alt = null): self
4343
{
44-
if ($src instanceof Responsive) {
45-
$targetSrc = $src->src;
46-
} else {
47-
$targetSrc = $src;
44+
if (!property_exists($image, 'source') || empty($image->source)) {
45+
throw new \InvalidArgumentException('Image object must have a non-empty "source" property.');
4846
}
4947

50-
$image = new Image($targetSrc, $alt, $width, $height, $format, $loading, $decoding);
48+
return new self(
49+
$image->source,
50+
(property_exists($image, 'caption') && !empty($image->caption)) ? $image->caption : $alt,
51+
$width,
52+
$height
53+
);
54+
}
55+
56+
public static function attributes(string|Responsive $src, $alt, $width = null, $height = null, $format = 'webp', $loading = 'lazy', $decoding = 'async'): string
57+
{
5158

59+
return (new self($src, $alt, $width, $height, $format, $loading, $decoding))->toAttributes();
60+
}
61+
62+
public function toAttributes(): string
63+
{
5264
$attributes = [
53-
sprintf('src="%s"', $image->getSrc()),
54-
sprintf('alt="%s"', $image->getAlt()),
55-
sprintf('loading="%s"', $image->getLoading()),
56-
sprintf('decoding="%s"', $image->getDecoding()),
65+
sprintf('src="%s"', $this->getSrc()),
66+
sprintf('alt="%s"', $this->getAlt()),
67+
sprintf('loading="%s"', $this->getLoading()),
68+
sprintf('decoding="%s"', $this->getDecoding()),
5769
];
5870

59-
if ($image->getwidth()) {
60-
$attributes[] = sprintf('width="%s"', $image->getwidth());
71+
if ($this->getwidth()) {
72+
$attributes[] = sprintf('width="%s"', $this->getwidth());
6173
}
6274

63-
if ($image->getHeight()) {
64-
$attributes[] = sprintf('height="%s"', $image->getHeight());
75+
if ($this->getHeight()) {
76+
$attributes[] = sprintf('height="%s"', $this->getHeight());
6577
}
6678

67-
if ($src instanceof Responsive) {
68-
$attributes[] = sprintf('srcset="%s"', $src->getSrcset($image));
69-
$attributes[] = sprintf('sizes="%s"', $src->getSizes($image));
79+
if ($this->src instanceof Responsive) {
80+
$attributes[] = sprintf('srcset="%s"', $this->src->getSrcset($this));
81+
$attributes[] = sprintf('sizes="%s"', $this->src->getSizes($this));
7082
}
7183

7284
return implode(" ", $attributes);
@@ -79,6 +91,18 @@ public static function tag(string|Responsive $src, $alt, $width = null, $height
7991
foreach ($options as $key => $value) {
8092
$attributes .= sprintf(' %s="%s"', $key, $value);
8193
}
94+
95+
return sprintf('<img %s />', $attributes);
96+
}
97+
98+
public function toTag(array $options = []): string
99+
{
100+
$attributes = $this->toAttributes();
101+
102+
foreach ($options as $key => $value) {
103+
$attributes .= sprintf(' %s="%s"', $key, $value);
104+
}
105+
82106
return sprintf('<img %s />', $attributes);
83107
}
84108

@@ -150,16 +174,17 @@ public function setHeight($height): self
150174

151175
public function getSrc(): string
152176
{
177+
$src = $this->src instanceof Responsive ? $this->src->src : $this->src;
153178
// If the URL starts with 'http://' or 'https://' and is not from 'storage.flyo.cloud', return it directly
154-
if (preg_match('/^https?:\/\//', $this->src) && !str_contains($this->src, 'storage.flyo.cloud')) {
155-
return $this->src;
179+
if (preg_match('#^https?:\/\/#', $src) && !str_contains($this->src, 'storage.flyo.cloud')) {
180+
return $src;
156181
}
157182

158-
if (str_starts_with($this->src, '/')) {
159-
return $this->src;
183+
if (str_starts_with($src, '/')) {
184+
return $src;
160185
}
161186

162-
$url = str_contains($this->src, 'https://storage.flyo.cloud') ? $this->src : 'https://storage.flyo.cloud/' . $this->src;
187+
$url = str_contains($src, 'https://storage.flyo.cloud') ? $src : 'https://storage.flyo.cloud/' . $src;
163188

164189
// If either width or height are defined, we add the /thumb/$widthx$height path to it.
165190
$width = $this->getWidth();
@@ -173,7 +198,7 @@ public function getSrc(): string
173198

174199
// if the original file name is already in the requested format, we don't add the format to the url.
175200
$orginalFormat = pathinfo($url, PATHINFO_EXTENSION) ?: '';
176-
if ($orginalFormat == $this->getFormat()) {
201+
if ($orginalFormat === $this->getFormat()) {
177202
return $url;
178203
}
179204

src/Responsive.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@
1212
*/
1313
class Responsive
1414
{
15-
public const PX_OR_LESS = 'max-width';
15+
/**
16+
* @var string
17+
*/
18+
final public const PX_OR_LESS = 'max-width';
1619

17-
public const PX_OR_MORE = 'min-width';
20+
/**
21+
* @var string
22+
*/
23+
final public const PX_OR_MORE = 'min-width';
1824

19-
private $_srcset = [];
25+
private array $_srcset = [];
2026

2127
public function __construct(public string $src)
2228
{
@@ -45,9 +51,11 @@ public function getSrcset(Image $image): string
4551
$hasLargeImage = true;
4652
}
4753
}
54+
4855
if (!$hasLargeImage) {
4956
$srcset[] = $fallback;
5057
}
58+
5159
return implode(", ", $srcset);
5260
}
5361

@@ -66,6 +74,7 @@ public function getSizes(Image $image): string
6674
$type = $src[1];
6775
$sizes[] = $type->size($src[0], $maxSize);
6876
}
77+
6978
$sizes[] = '100vw';
7079
return implode(", ", $sizes);
7180
}

tests/ImageTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,29 @@ public function testExternalOtherSource()
5252
$this->assertSame('/foobar.jpg', Image::source('/foobar.jpg', 400, 400));
5353
}
5454

55+
public function testFromObject()
56+
{
57+
$image = Image::fromObject((object)[
58+
'source' => 'test.jpg',
59+
'caption' => 'Test Image',
60+
], 100, 100);
61+
62+
$this->assertSame('https://storage.flyo.cloud/test.jpg/thumb/100x100?format=webp', $image->getSrc());
63+
$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());
64+
$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());
65+
}
66+
67+
public function testFromCaptionButOwnAltTag()
68+
{
69+
$image = Image::fromObject((object)[
70+
'source' => 'test.jpg',
71+
], 100, 100, 'Own Alt Tag');
72+
73+
$this->assertSame('https://storage.flyo.cloud/test.jpg/thumb/100x100?format=webp', $image->getSrc());
74+
$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());
75+
$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());
76+
}
77+
5578
/*
5679
public function testGetSrc()
5780
{

0 commit comments

Comments
 (0)