Skip to content

Commit 71ec476

Browse files
authored
Merge pull request #18 from pboivin/feat/render-from-array
feat: Initialize image and imageset render from arrays
2 parents 7b1198f + 616210b commit 71ec476

File tree

6 files changed

+154
-10
lines changed

6 files changed

+154
-10
lines changed

src/ImageFile.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,30 @@ class ImageFile
66
{
77
protected $size;
88

9-
public function __construct(
9+
final public function __construct(
1010
protected string $fileName,
1111
protected string $path,
1212
protected string $url,
13-
protected ImageFileInspector $inspector
13+
protected ?ImageFileInspector $inspector = null
1414
) {
1515
}
1616

17+
public static function fromArray(array $data): static
18+
{
19+
$obj = new static(
20+
$data['fileName'] ?? '',
21+
$data['path'] ?? '',
22+
$data['url'] ?? '',
23+
);
24+
25+
$obj->size = [
26+
'width' => $data['width'] ?? 0,
27+
'height' => $data['height'] ?? 0,
28+
];
29+
30+
return $obj;
31+
}
32+
1733
public function fileName(): string
1834
{
1935
return $this->fileName;

src/ImageRender.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,26 @@
44

55
class ImageRender extends ImgRenderable
66
{
7-
public function __construct(protected Image $image, protected array $config = [])
7+
final public function __construct(protected array|Image $image, protected array $config = [])
88
{
99
if ($config) {
1010
$this->acceptRenderConfig($config);
1111
}
1212
}
1313

14+
public static function fromArray(array $image, array $config = []): static
15+
{
16+
return new static($image, $config);
17+
}
18+
1419
public function main(): ImageFile
1520
{
16-
return $this->image->source();
21+
return $this->resolveImageFile($this->image);
1722
}
1823

1924
public function lqip(): ImageFile
2025
{
21-
return $this->image->cached();
26+
return $this->resolveImageFile($this->image, cached: true);
2227
}
2328

2429
public function img(array $attributes = []): string

src/ImageSetRender.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,36 @@ class ImageSetRender extends ImgRenderable
66
{
77
protected $data;
88

9-
public function __construct(protected ImageSet $imageSet, protected array $config = [])
9+
final public function __construct(protected array|ImageSet $imageSet, protected array $config = [])
1010
{
11-
$this->data = $this->imageSet->data();
11+
if (is_array($imageSet)) {
12+
$this->data = $imageSet;
13+
} else {
14+
$this->data = $this->imageSet->data();
15+
}
1216

1317
if ($config) {
1418
$this->acceptRenderConfig($config);
1519
}
1620
}
1721

22+
public static function fromArray(array $imageSet, array $config = []): static
23+
{
24+
return new static($imageSet, $config);
25+
}
26+
1827
public function main(): ImageFile
1928
{
2029
$source = end($this->data['sources']);
2130

2231
$item = end($source['srcset']);
2332

24-
return $item['image']->cached();
33+
return $this->resolveImageFile($item['image'], cached: true);
2534
}
2635

2736
public function lqip(): ImageFile
2837
{
29-
return $this->data['lqip']->cached();
38+
return $this->resolveImageFile($this->data['lqip'], cached: true);
3039
}
3140

3241
public function picture(array $attributes = []): string
@@ -120,7 +129,10 @@ protected function getSrcset($source): string
120129
$srcset = [];
121130

122131
foreach ($source['srcset'] as $item) {
123-
$url = $item['image']->cached()->url();
132+
$cached = $this->resolveImageFile($item['image'], cached: true);
133+
134+
$url = $cached->url();
135+
124136
$width = $item['width'];
125137

126138
$srcset[] = "{$url}" . ($includeWidth ? " {$width}w" : '');

src/ImgRenderable.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,15 @@ protected function getPaddingTopValue(float $aspectRatio): string
252252

253253
return "{$padding}%";
254254
}
255+
256+
protected function resolveImageFile(array|Image $item, bool $cached = false): ImageFile
257+
{
258+
$role = $cached ? 'cached' : 'source';
259+
260+
if (is_array($item)) {
261+
return ImageFile::fromArray($item[$role]);
262+
}
263+
264+
return $item->$role();
265+
}
255266
}

tests/ImageRenderTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,40 @@ public function test_can_override_noscript_attributes()
236236
);
237237
}
238238

239+
public function test_can_initialize_from_array()
240+
{
241+
$imageArray = [
242+
"source" => [
243+
"fileName" => "01.jpg",
244+
"path" => "/path/to/images/source/01.jpg",
245+
"url" => "/images/source/01.jpg",
246+
"width" => 2932,
247+
"height" => 2000,
248+
"ratio" => 1.466,
249+
],
250+
"cached" => [
251+
"fileName" => "01.jpg/test.gif",
252+
"path" => "/path/to/images/cache/01.jpg/test.gif",
253+
"url" => "/images/cache/01.jpg/test.gif",
254+
"width" => 15,
255+
"height" => 10,
256+
"ratio" => 1.5,
257+
],
258+
];
259+
260+
$imageRender = ImageRender::fromArray($imageArray);
261+
262+
$output = $imageRender->img([
263+
'class' => 'test',
264+
'alt' => 'This is a test',
265+
]);
266+
267+
$this->assertEquals(
268+
'<img class="lazyload test" alt="This is a test" src="/images/cache/01.jpg/test.gif" data-src="/images/source/01.jpg" width="2932" height="2000">',
269+
$output
270+
);
271+
}
272+
239273
protected function prepareImageRender($options = []): object
240274
{
241275
$prepared = $this->prepareImage();

tests/ImageSetRenderTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,64 @@ public function test_can_override_noscript_attributes()
317317
);
318318
}
319319

320+
public function test_can_initialize_from_array()
321+
{
322+
$imageSetArray = array(
323+
'sources' => array(
324+
0 => array(
325+
'image' => '01.jpg',
326+
'widths' => array(0 => 500, 1 => 900, 2 => 1300, 3 => 1700),
327+
'srcset' => array(
328+
0 => array(
329+
'image' => array(
330+
'source' => array('fileName' => '01.jpg', 'path' => '/path/to/images/source/01.jpg', 'url' => '/images/source/01.jpg', 'width' => 2932, 'height' => 2000, 'ratio' => 1.466,),
331+
'cached' => array('fileName' => '01.jpg/one.jpg', 'path' => '/path/to/images/cache/01.jpg/one.jpg', 'url' => '/images/cache/01.jpg/one.jpg', 'width' => 500, 'height' => 341, 'ratio' => 1.466275659824047,),
332+
),
333+
'width' => 500,
334+
),
335+
1 => array(
336+
'image' => array(
337+
'source' => array('fileName' => '01.jpg', 'path' => '/path/to/images/source/01.jpg', 'url' => '/images/source/01.jpg', 'width' => 2932, 'height' => 2000, 'ratio' => 1.466,),
338+
'cached' => array('fileName' => '01.jpg/two.jpg', 'path' => '/path/to/images/cache/01.jpg/two.jpg', 'url' => '/images/cache/01.jpg/two.jpg', 'width' => 900, 'height' => 614, 'ratio' => 1.4657980456026058,),
339+
),
340+
'width' => 900,
341+
),
342+
2 => array(
343+
'image' => array(
344+
'source' => array('fileName' => '01.jpg', 'path' => '/path/to/images/source/01.jpg', 'url' => '/images/source/01.jpg', 'width' => 2932, 'height' => 2000, 'ratio' => 1.466,),
345+
'cached' => array('fileName' => '01.jpg/three.jpg', 'path' => '/path/to/images/cache/01.jpg/three.jpg', 'url' => '/images/cache/01.jpg/three.jpg', 'width' => 1300, 'height' => 887, 'ratio' => 1.4656144306651635,),
346+
),
347+
'width' => 1300,
348+
),
349+
3 => array(
350+
'image' => array(
351+
'source' => array('fileName' => '01.jpg', 'path' => '/path/to/images/source/01.jpg', 'url' => '/images/source/01.jpg', 'width' => 2932, 'height' => 2000, 'ratio' => 1.466,),
352+
'cached' => array('fileName' => '01.jpg/four.jpg', 'path' => '/path/to/images/cache/01.jpg/four.jpg', 'url' => '/images/cache/01.jpg/four.jpg', 'width' => 1700, 'height' => 1160, 'ratio' => 1.4655172413793103,),
353+
),
354+
'width' => 1700,
355+
),
356+
),
357+
),
358+
),
359+
'lqip' => array(
360+
'source' => array('fileName' => '01.jpg', 'path' => '/path/to/images/source/01.jpg', 'url' => '/images/source/01.jpg', 'width' => 2932, 'height' => 2000, 'ratio' => 1.466,),
361+
'cached' => array('fileName' => '01.jpg/lqip.gif', 'path' => '/path/to/images/cache/01.jpg/lqip.gif', 'url' => '/images/cache/01.jpg/lqip.gif', 'width' => 15, 'height' => 10, 'ratio' => 1.5,),
362+
),
363+
);
364+
365+
$imageRender = ImageSetRender::fromArray($imageSetArray);
366+
367+
$output = $imageRender->img([
368+
'class' => 'test',
369+
'alt' => 'This is a test',
370+
]);
371+
372+
$this->assertEquals(
373+
'<img class="lazyload test" alt="This is a test" src="/images/cache/01.jpg/lqip.gif" width="1700" height="1160" data-src="/images/cache/01.jpg/four.jpg" data-srcset="/images/cache/01.jpg/one.jpg 500w, /images/cache/01.jpg/two.jpg 900w, /images/cache/01.jpg/three.jpg 1300w, /images/cache/01.jpg/four.jpg 1700w" data-sizes="100vw">',
374+
$output
375+
);
376+
}
377+
320378
protected function prepareAllImages(): array
321379
{
322380
$image1 = $this->prepareImage();
@@ -500,3 +558,11 @@ protected function prepareForPictureFormats($options = []): object
500558
];
501559
}
502560
}
561+
562+
563+
564+
/*
565+
566+
567+
568+
*/

0 commit comments

Comments
 (0)