Skip to content

Commit 6aa7816

Browse files
committed
feat: preset enum
1 parent e9114d9 commit 6aa7816

File tree

3 files changed

+80
-12
lines changed

3 files changed

+80
-12
lines changed

src/ImageBossBuilder.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,29 +70,32 @@ public function interval(int $interval): self
7070
return $this;
7171
}
7272

73-
public function preset(string $name): self
73+
public function preset(Preset|string $preset): self
7474
{
75-
$presets = config('statamic-imageboss.presets', []);
76-
$preset = $presets[$name] ?? null;
75+
if ($preset instanceof Preset) {
76+
$config = $preset->config();
77+
} else {
78+
$config = config("statamic-imageboss.presets.{$preset}", []);
79+
}
7780

78-
if (! $preset) {
81+
if (empty($config)) {
7982
return $this;
8083
}
8184

82-
if (isset($preset['min'])) {
83-
$this->min = $preset['min'];
85+
if (isset($config['min'])) {
86+
$this->min = $config['min'];
8487
}
8588

86-
if (isset($preset['max'])) {
87-
$this->max = $preset['max'];
89+
if (isset($config['max'])) {
90+
$this->max = $config['max'];
8891
}
8992

90-
if (isset($preset['ratio'])) {
91-
$this->ratio = $preset['ratio'];
93+
if (isset($config['ratio'])) {
94+
$this->ratio = $config['ratio'];
9295
}
9396

94-
if (isset($preset['interval'])) {
95-
$this->interval = $preset['interval'];
97+
if (isset($config['interval'])) {
98+
$this->interval = $config['interval'];
9699
}
97100

98101
return $this;

src/Preset.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Noo\StatamicImageboss;
4+
5+
enum Preset: string
6+
{
7+
case Default = 'default';
8+
case Thumbnail = 'thumbnail';
9+
case Card = 'card';
10+
case Hero = 'hero';
11+
12+
/**
13+
* @return array{min: int, max: int, ratio?: float, interval?: int}
14+
*/
15+
public function config(): array
16+
{
17+
return match ($this) {
18+
self::Default => ['min' => 320, 'max' => 2560],
19+
self::Thumbnail => ['min' => 200, 'max' => 700, 'ratio' => 1, 'interval' => 250],
20+
self::Card => ['min' => 300, 'max' => 800, 'ratio' => 4 / 5],
21+
self::Hero => ['min' => 640, 'max' => 3840],
22+
};
23+
}
24+
}

tests/ImageBossBuilderTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Noo\StatamicImageboss\ImageBoss;
44
use Noo\StatamicImageboss\ImageBossBuilder;
5+
use Noo\StatamicImageboss\Preset;
56

67
beforeEach(function () {
78
config()->set('statamic-imageboss.source', 'test-source');
@@ -170,3 +171,43 @@ function createMockAsset(bool $hasFocus = false, ?string $focusValue = null): Mo
170171

171172
expect($url)->toContain('width/500');
172173
});
174+
175+
it('accepts Preset enum for type-safe preset selection', function () {
176+
$asset = createMockAsset();
177+
178+
$builder = (new ImageBossBuilder($asset))->preset(Preset::Card);
179+
180+
$srcset = $builder->srcset();
181+
182+
expect($srcset)->toBeArray();
183+
expect($srcset[0]['width'])->toBe(300);
184+
expect(end($srcset)['width'])->toBe(800);
185+
});
186+
187+
it('Preset enum has correct configuration for each case', function () {
188+
expect(Preset::Default->config())->toBe(['min' => 320, 'max' => 2560]);
189+
expect(Preset::Thumbnail->config())->toBe(['min' => 200, 'max' => 700, 'ratio' => 1, 'interval' => 250]);
190+
expect(Preset::Card->config())->toBe(['min' => 300, 'max' => 800, 'ratio' => 4 / 5]);
191+
expect(Preset::Hero->config())->toBe(['min' => 640, 'max' => 3840]);
192+
});
193+
194+
it('applies ratio from Preset enum', function () {
195+
$asset = createMockAsset();
196+
197+
$builder = (new ImageBossBuilder($asset))->preset(Preset::Thumbnail)->width(400);
198+
199+
$url = $builder->url();
200+
201+
expect($url)->toContain('cover/400x400');
202+
});
203+
204+
it('applies interval from Preset enum', function () {
205+
$asset = createMockAsset();
206+
207+
$builder = (new ImageBossBuilder($asset))->preset(Preset::Thumbnail);
208+
209+
$srcset = $builder->srcset();
210+
$widths = array_column($srcset, 'width');
211+
212+
expect($widths)->toBe([200, 450, 700]);
213+
});

0 commit comments

Comments
 (0)