Skip to content

Commit f90430c

Browse files
soyukaSarahshr
andauthored
feat: deprecate not setting formats manually (#5808)
introduces documentation formats Co-authored-by: Sarahshr <[email protected]>
1 parent 04b846b commit f90430c

File tree

3 files changed

+121
-2
lines changed

3 files changed

+121
-2
lines changed

Tests/YamlEncoderTest.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Serializer\Tests;
15+
16+
use ApiPlatform\Serializer\YamlEncoder;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class YamlEncoderTest extends TestCase
20+
{
21+
private YamlEncoder $encoder;
22+
23+
protected function setUp(): void
24+
{
25+
$this->encoder = new YamlEncoder('yamlopenapi');
26+
}
27+
28+
public function testSupportEncoding(): void
29+
{
30+
$this->assertTrue($this->encoder->supportsEncoding('yamlopenapi'));
31+
$this->assertFalse($this->encoder->supportsEncoding('json'));
32+
}
33+
34+
public function testEncode(): void
35+
{
36+
$data = ['foo' => 'bar'];
37+
38+
$this->assertSame('{ foo: bar }', $this->encoder->encode($data, 'yamlopenapi'));
39+
}
40+
41+
public function testSupportDecoding(): void
42+
{
43+
$this->assertTrue($this->encoder->supportsDecoding('yamlopenapi'));
44+
$this->assertFalse($this->encoder->supportsDecoding('json'));
45+
}
46+
47+
public function testDecode(): void
48+
{
49+
$this->assertEquals(['foo' => 'bar'], $this->encoder->decode('{ foo: bar }', 'yamlopenapi'));
50+
}
51+
52+
public function testUTF8EncodedString(): void
53+
{
54+
$data = ['foo' => 'Über'];
55+
56+
$this->assertEquals('{ foo: Über }', $this->encoder->encode($data, 'yamlopenapi'));
57+
}
58+
}

YamlEncoder.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Serializer;
15+
16+
use Symfony\Component\Serializer\Encoder\DecoderInterface;
17+
use Symfony\Component\Serializer\Encoder\EncoderInterface;
18+
use Symfony\Component\Serializer\Encoder\YamlEncoder as BaseYamlEncoder;
19+
20+
/**
21+
* A YAML encoder with appropriate default options to embed the generated document into HTML.
22+
*/
23+
final class YamlEncoder implements EncoderInterface, DecoderInterface
24+
{
25+
public function __construct(private readonly string $format = 'yamlopenapi', private readonly EncoderInterface&DecoderInterface $yamlEncoder = new BaseYamlEncoder())
26+
{
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function supportsEncoding($format, array $context = []): bool
33+
{
34+
return $this->format === $format;
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function encode($data, $format, array $context = []): string
41+
{
42+
return $this->yamlEncoder->encode($data, $format, $context);
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function supportsDecoding($format, array $context = []): bool
49+
{
50+
return $this->format === $format;
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function decode($data, $format, array $context = []): mixed
57+
{
58+
return $this->yamlEncoder->decode($data, $format, $context);
59+
}
60+
}

composer.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@
3030
"symfony/validator": "^6.3"
3131
},
3232
"require-dev": {
33+
"api-platform/symfony": "*@dev || ^3.1",
3334
"phpspec/prophecy-phpunit": "^2.0",
34-
"symfony/phpunit-bridge": "^6.1",
3535
"symfony/mercure-bundle": "*",
36-
"api-platform/symfony": "*@dev || ^3.1"
36+
"symfony/phpunit-bridge": "^6.1",
37+
"symfony/yaml": "^6.3"
3738
},
3839
"autoload": {
3940
"psr-4": {

0 commit comments

Comments
 (0)