Skip to content

Commit 120087b

Browse files
Merge branch '7.4' into 8.0
* 7.4: [Form][Intl] Allow the developer to choose if he want to include/exclude currencies that do not have validity dates. [Serializer] Allow forcing timezone in `DateTimeNormalizer` during denormalization [HttpFoundation] Allow Request::setFormat() to override predefined formats
2 parents 5a63b40 + 76c37a6 commit 120087b

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

Request.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,15 +1290,22 @@ public function getFormat(?string $mimeType, bool $subtypeFallback = false): ?st
12901290
static::initializeFormats();
12911291
}
12921292

1293+
$exactFormat = null;
1294+
$canonicalFormat = null;
1295+
12931296
foreach (static::$formats as $format => $mimeTypes) {
1294-
if (\in_array($mimeType, (array) $mimeTypes, true)) {
1295-
return $format;
1297+
if (\in_array($mimeType, $mimeTypes, true)) {
1298+
$exactFormat = $format;
12961299
}
1297-
if (null !== $canonicalMimeType && \in_array($canonicalMimeType, (array) $mimeTypes, true)) {
1298-
return $format;
1300+
if (null !== $canonicalMimeType && \in_array($canonicalMimeType, $mimeTypes, true)) {
1301+
$canonicalFormat = $format;
12991302
}
13001303
}
13011304

1305+
if ($format = $exactFormat ?? $canonicalFormat) {
1306+
return $format;
1307+
}
1308+
13021309
if (!$canonicalMimeType ??= $mimeType) {
13031310
return null;
13041311
}
@@ -1334,7 +1341,7 @@ public function setFormat(?string $format, string|array $mimeTypes): void
13341341
static::initializeFormats();
13351342
}
13361343

1337-
static::$formats[$format] = \is_array($mimeTypes) ? $mimeTypes : [$mimeTypes];
1344+
static::$formats[$format ?? ''] = (array) $mimeTypes;
13381345
}
13391346

13401347
/**

Tests/Test/Constraint/ResponseFormatSameTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,14 @@ public function testNullFormat()
4747

4848
$constraint->evaluate(new Response('', 200, ['Content-Type' => 'application/ld+json']));
4949
}
50+
51+
public function testOverriddenFormat()
52+
{
53+
$request = new Request();
54+
$request->setFormat('jsonapi', ['application/vnd.api+json']);
55+
$request->setFormat('apijson', ['application/vnd.api+json']);
56+
57+
$constraint = new ResponseFormatSame($request, 'apijson');
58+
$this->assertTrue($constraint->evaluate(new Response('', 200, ['Content-Type' => 'application/vnd.api+json']), '', true));
59+
}
5060
}

0 commit comments

Comments
 (0)