@@ -169,43 +169,53 @@ A [normalizer](serialization.md#normalization) could be used to set the `content
169
169
170
170
` ` ` php
171
171
<?php
172
- // api/src/Serializer/MediaObjectNormalizer.php
173
172
174
173
namespace App\S erializer;
175
174
176
175
use App\E ntity\M ediaObject;
177
- use Symfony\C omponent\S erializer\N ormalizer\N ormalizerAwareInterface;
178
- use Symfony\C omponent\S erializer\N ormalizer\N ormalizerAwareTrait;
179
176
use Vich\UploaderB undle\S torage\S torageInterface;
177
+ use Symfony\C omponent\D ependencyInjection\A ttribute\A utowire;
178
+ use Symfony\C omponent\S erializer\N ormalizer\N ormalizerInterface;
180
179
181
- final class MediaObjectNormalizer implements NormalizerAwareInterface
180
+ class MediaObjectNormalizer implements NormalizerInterface
182
181
{
183
- use NormalizerAwareTrait;
184
182
185
- private const ALREADY_CALLED = 'MEDIA_OBJECT_NORMALIZER_ALREADY_CALLED';
183
+ private const ALREADY_CALLED = 'MEDIA_OBJECT_NORMALIZER_ALREADY_CALLED';
186
184
187
- public function __construct(private StorageInterface $storage)
188
- {
189
- }
185
+ public function __construct(
186
+ #[Autowire(service: 'serializer.normalizer.object')]
187
+ private readonly NormalizerInterface $normalizer,
188
+ private readonly StorageInterface $storage
189
+ ) {
190
+ }
190
191
191
- public function normalize($object, ?string $format = null, array $context = []): array|string|int|float|bool|\A rrayObject|null
192
- {
193
- $context[self::ALREADY_CALLED] = true;
192
+ public function normalize($object, ?string $format = null, array $context = []): array|string|int|float|bool|\A rrayObject|null
193
+ {
194
+ $context[self::ALREADY_CALLED] = true;
194
195
195
- $object->contentUrl = $this->storage->resolveUri($object, 'file');
196
+ $object->contentUrl = $this->storage->resolveUri($object, 'file');
196
197
197
- return $this->normalizer->normalize($object, $format, $context);
198
- }
198
+ return $this->normalizer->normalize($object, $format, $context);
199
+ }
199
200
200
- public function supportsNormalization($data, ?string $format = null, array $context = []): bool
201
- {
202
- if (isset($context[self::ALREADY_CALLED])) {
203
- return false;
204
- }
201
+ public function supportsNormalization($data, ?string $format = null, array $context = []): bool
202
+ {
205
203
206
- return $data instanceof MediaObject;
204
+ if (isset($context[self::ALREADY_CALLED])) {
205
+ return false;
207
206
}
207
+
208
+ return $data instanceof MediaObject;
209
+ }
210
+
211
+ public function getSupportedTypes(?string $format): array
212
+ {
213
+ return [
214
+ MediaObject::class => true,
215
+ ];
216
+ }
208
217
}
218
+
209
219
` ` `
210
220
211
221
# ## Making a Request to the `/media_objects` Endpoint
@@ -234,8 +244,8 @@ You will need to modify your `Caddyfile` to allow the above `contentUrl` to be a
234
244
@pwa expression ` (
235
245
header({'Accept': '*text/html*'})
236
246
&& !path(
237
- - ' /docs*' , '/graphql*', '/bundles*', '/media*', '/ contexts*', '/_profiler*', '/_wdt*',
238
- + '/media*', '/docs*', '/graphql*', '/bundles*', '/media*', '/ contexts*', '/_profiler*', '/_wdt*',
247
+ - ' /docs*' , '/graphql*', '/bundles*', '/contexts*', '/_profiler*', '/_wdt*',
248
+ + '/media*', '/docs*', '/graphql*', '/bundles*', '/contexts*', '/_profiler*', '/_wdt*',
239
249
' *.json*' , '*.html', '*.csv', '*.yml', '*.yaml', '*.xml'
240
250
)
241
251
)
@@ -313,25 +323,26 @@ class MediaObjectTest extends ApiTestCase
313
323
314
324
public function testCreateAMediaObject(): void
315
325
{
316
- $file = new UploadedFile('fixtures/files/image.png', 'image.png');
326
+ // The file "image.jpg" is the folder fixtures which is in the project dir
327
+ $file = new UploadedFile(__DIR__ . '/../fixtures/image.jpg', 'image.jpg');
317
328
$client = self::createClient();
318
329
319
- $client->request('POST', '/media_objects', [
320
- 'headers' => ['Content-Type' => 'multipart/form-data'],
321
- 'extra' => [
322
- // If you have additional fields in your MediaObject entity, use the parameters.
323
- 'parameters' => [
324
- 'title' => 'My file uploaded',
325
- ],
326
- 'files' => [
327
- 'file' => $file,
328
- ],
329
- ]
330
+ $client->request('POST', 'http://localhost:8888/api /media_objects', [
331
+ 'headers' => ['Content-Type' => 'multipart/form-data'],
332
+ 'extra' => [
333
+ // If you have additional fields in your MediaObject entity, use the parameters.
334
+ 'parameters' => [
335
+ // 'title' => 'title'
336
+ ],
337
+ 'files' => [
338
+ 'file' => $file,
339
+ ],
340
+ ]
330
341
]);
331
342
$this->assertResponseIsSuccessful();
332
343
$this->assertMatchesResourceItemJsonSchema(MediaObject::class);
333
344
$this->assertJsonContains([
334
- 'title' => 'My file uploaded',
345
+ // 'title' => 'My file uploaded',
335
346
]);
336
347
}
337
348
}
@@ -451,19 +462,28 @@ We also need to make sure the field containing the uploaded file is not denormal
451
462
452
463
namespace App\Serializer;
453
464
454
- use Symfony\Component\HttpFoundation\File\UploadedFile ;
465
+ use Symfony\Component\HttpFoundation\File\File ;
455
466
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
456
467
457
468
final class UploadedFileDenormalizer implements DenormalizerInterface
458
469
{
459
- public function denormalize($data, string $type, string $format = null, array $context = []): UploadedFile
470
+ public function denormalize($data, string $type, string $format = null, array $context = []): File
460
471
{
461
472
return $data;
462
473
}
463
474
464
- public function supportsDenormalization($data, $type, $format = null): bool
475
+ public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
476
+ {
477
+ return $data instanceof File;
478
+ }
479
+
480
+ public function getSupportedTypes(?string $format): array
465
481
{
466
- return $data instanceof UploadedFile;
482
+ return [
483
+ 'object' => null,
484
+ '*' => false,
485
+ File::class => true,
486
+ ];
467
487
}
468
488
}
469
489
```
0 commit comments