- $default_format = 'html';
- if ($collection->count() === 1) {
- $only_route = $collection->getIterator()->current();
- $required_format = $only_route->getRequirement('_format');
- if (strpos($required_format, '|') === FALSE) {
- $default_format = $required_format;
- }
- }
- return $default_format;
+ $formats = static::getAvailableFormats($collection);
+
+ // The default format is 'html' unless ALL routes require the same format.
+ return count($formats) === 1
+ ? reset($formats)
+ : 'html';
+ }
+
+ /**
+ * Gets the set of formats across all routes in the collection.
+ *
+ * @param \Symfony\Component\Routing\RouteCollection $collection
+ * The route collection to filter.
+ *
+ * @return string[]
+ * All available formats.
+ */
+ protected static function getAvailableFormats(RouteCollection $collection) {
+ $all_formats = array_reduce($collection->all(), function (array $carry, Route $route) {
+ // Routes without a '_format' requirement are assumed to require HTML.
+ $route_formats = !$route->hasRequirement('_format')
+ ? ['html']
+ : explode('|', $route->getRequirement('_format'));
+ return array_merge($carry, $route_formats);
+ }, []);
+ return array_unique(array_filter($all_formats));