Skip to content

Commit f55a05c

Browse files
committed
fix: backed enum support
1 parent b855261 commit f55a05c

File tree

4 files changed

+45
-54
lines changed

4 files changed

+45
-54
lines changed

README.md

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,22 @@ When `IMAGEBOSS_SOURCE` is not set, the package falls back to Statamic's Glide.
4444

4545
### Presets
4646

47-
| Preset | Min | Max | Ratio | Interval |
48-
|--------|-----|-----|-------|----------|
49-
| default | 320 | 2560 | - | 200 |
50-
| thumbnail | 200 | 700 | 1:1 | 250 |
51-
| card | 300 | 800 | 4:5 | 200 |
52-
| hero | 640 | 3840 | - | 200 |
47+
Define presets in `config/statamic-imageboss.php`:
48+
49+
```php
50+
'presets' => [
51+
'thumbnail' => [
52+
'min' => 200, // minimum srcset width
53+
'max' => 700, // maximum srcset width
54+
'ratio' => 1, // aspect ratio (optional)
55+
'interval' => 250, // width step (optional)
56+
],
57+
'hero' => [
58+
'min' => 640,
59+
'max' => 3840,
60+
],
61+
],
62+
```
5363

5464
## Usage
5565

@@ -80,15 +90,13 @@ Full example:
8090

8191
```php
8292
use Noo\StatamicImageboss\Facades\ImageBoss;
83-
use Noo\StatamicImageboss\Preset;
8493

8594
// Single URL
8695
$url = ImageBoss::from($asset)->width(800)->url();
8796
$url = ImageBoss::from($asset)->width(800)->ratio(16/9)->url();
8897

89-
// Responsive srcset
98+
// Responsive srcset with preset
9099
$srcset = ImageBoss::from($asset)->preset('hero')->srcsetString();
91-
$srcset = ImageBoss::from($asset)->preset(Preset::Card)->srcsetString();
92100

93101
// Custom configuration
94102
$srcset = ImageBoss::from($asset)
@@ -99,6 +107,18 @@ $srcset = ImageBoss::from($asset)
99107
->srcsetString();
100108
```
101109

110+
For type safety, create a backed enum matching your config presets:
111+
112+
```php
113+
enum Preset: string
114+
{
115+
case Hero = 'hero';
116+
case Thumbnail = 'thumbnail';
117+
}
118+
119+
$srcset = ImageBoss::from($asset)->preset(Preset::Hero)->srcsetString();
120+
```
121+
102122
### Builder Methods
103123

104124
| Method | Description |
@@ -109,7 +129,7 @@ $srcset = ImageBoss::from($asset)
109129
| `min(int)` | Minimum width for srcset |
110130
| `max(int)` | Maximum width for srcset |
111131
| `interval(int)` | Width step for srcset |
112-
| `preset(string\|Preset)` | Apply preset configuration |
132+
| `preset(string)` | Apply preset configuration |
113133
| `url()` | Generate single URL |
114134
| `srcset()` | Generate srcset array |
115135
| `srcsetString()` | Generate srcset string |

src/ImageBossBuilder.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,10 @@ public function interval(int $interval): self
7070
return $this;
7171
}
7272

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

8178
if (empty($config)) {
8279
return $this;

src/Preset.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

tests/Feature/ImageBossBuilderTest.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
use Noo\StatamicImageboss\ImageBoss;
44
use Noo\StatamicImageboss\ImageBossBuilder;
5-
use Noo\StatamicImageboss\Preset;
5+
6+
enum TestPreset: string
7+
{
8+
case Card = 'card';
9+
case Thumbnail = 'thumbnail';
10+
}
611

712
beforeEach(function () {
813
config()->set('statamic-imageboss.source', 'test-source');
@@ -172,10 +177,10 @@ function createMockAsset(bool $hasFocus = false, ?string $focusValue = null): Mo
172177
expect($url)->toContain('width/500');
173178
});
174179

175-
it('accepts Preset enum for type-safe preset selection', function () {
180+
it('accepts backed enum for type-safe preset selection', function () {
176181
$asset = createMockAsset();
177182

178-
$builder = (new ImageBossBuilder($asset))->preset(Preset::Card);
183+
$builder = (new ImageBossBuilder($asset))->preset(TestPreset::Card);
179184

180185
$srcset = $builder->srcset();
181186

@@ -184,27 +189,20 @@ function createMockAsset(bool $hasFocus = false, ?string $focusValue = null): Mo
184189
expect(end($srcset)['width'])->toBe(800);
185190
});
186191

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 () {
192+
it('applies ratio from preset', function () {
195193
$asset = createMockAsset();
196194

197-
$builder = (new ImageBossBuilder($asset))->preset(Preset::Thumbnail)->width(400);
195+
$builder = (new ImageBossBuilder($asset))->preset('thumbnail')->width(400);
198196

199197
$url = $builder->url();
200198

201199
expect($url)->toContain('cover/400x400');
202200
});
203201

204-
it('applies interval from Preset enum', function () {
202+
it('applies interval from preset', function () {
205203
$asset = createMockAsset();
206204

207-
$builder = (new ImageBossBuilder($asset))->preset(Preset::Thumbnail);
205+
$builder = (new ImageBossBuilder($asset))->preset('thumbnail');
208206

209207
$srcset = $builder->srcset();
210208
$widths = array_column($srcset, 'width');

0 commit comments

Comments
 (0)