Skip to content

Commit

Permalink
Update file-upload.md (#1909)
Browse files Browse the repository at this point in the history
Update normalization with the symfony 7 normalizer interface + update test
  • Loading branch information
Damsito committed Mar 18, 2024
1 parent 85f5b07 commit ad6c3eb
Showing 1 changed file with 45 additions and 35 deletions.
80 changes: 45 additions & 35 deletions core/file-upload.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,44 +169,53 @@ A [normalizer](serialization.md#normalization) could be used to set the `content

```php
<?php
// api/src/Serializer/MediaObjectNormalizer.php

namespace App\Serializer;

use App\Entity\MediaObject;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Vich\UploaderBundle\Storage\StorageInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class MediaObjectNormalizer implements NormalizerAwareInterface, NormalizerInterface
class MediaObjectNormalizer implements NormalizerInterface
{
use NormalizerAwareTrait;

private const ALREADY_CALLED = 'MEDIA_OBJECT_NORMALIZER_ALREADY_CALLED';
private const ALREADY_CALLED = 'MEDIA_OBJECT_NORMALIZER_ALREADY_CALLED';

public function __construct(private StorageInterface $storage)
{
}
public function __construct(
#[Autowire(service: 'serializer.normalizer.object')]
private readonly NormalizerInterface $normalizer,
private readonly StorageInterface $storage
) {
}

public function normalize($object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
{
$context[self::ALREADY_CALLED] = true;
public function normalize($object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
{
$context[self::ALREADY_CALLED] = true;

$object->contentUrl = $this->storage->resolveUri($object, 'file');
$object->contentUrl = $this->storage->resolveUri($object, 'file');

return $this->normalizer->normalize($object, $format, $context);
}
return $this->normalizer->normalize($object, $format, $context);
}

public function supportsNormalization($data, ?string $format = null, array $context = []): bool
{
if (isset($context[self::ALREADY_CALLED])) {
return false;
}
public function supportsNormalization($data, ?string $format = null, array $context = []): bool
{

return $data instanceof MediaObject;
if (isset($context[self::ALREADY_CALLED])) {
return false;
}

return $data instanceof MediaObject;
}

public function getSupportedTypes(?string $format): array
{
return [
MediaObject::class => true,
];
}
}

```

### Making a Request to the `/media_objects` Endpoint
Expand Down Expand Up @@ -314,25 +323,26 @@ class MediaObjectTest extends ApiTestCase

public function testCreateAMediaObject(): void
{
$file = new UploadedFile('fixtures/files/image.png', 'image.png');
// The file "image.jpg" is the folder fixtures which is in the project dir
$file = new UploadedFile(__DIR__ . '/../fixtures/image.jpg', 'image.jpg');
$client = self::createClient();

$client->request('POST', '/media_objects', [
'headers' => ['Content-Type' => 'multipart/form-data'],
'extra' => [
// If you have additional fields in your MediaObject entity, use the parameters.
'parameters' => [
'title' => 'My file uploaded',
],
'files' => [
'file' => $file,
],
]
$client->request('POST', 'http://localhost:8888/api/media_objects', [
'headers' => ['Content-Type' => 'multipart/form-data'],
'extra' => [
// If you have additional fields in your MediaObject entity, use the parameters.
'parameters' => [
// 'title' => 'title'
],
'files' => [
'file' => $file,
],
]
]);
$this->assertResponseIsSuccessful();
$this->assertMatchesResourceItemJsonSchema(MediaObject::class);
$this->assertJsonContains([
'title' => 'My file uploaded',
// 'title' => 'My file uploaded',
]);
}
}
Expand Down

0 comments on commit ad6c3eb

Please sign in to comment.