NelmioApiDocBundle/Routing/FilteredRouteCollectionBuilder.php

49 lines
1.1 KiB
PHP
Raw Normal View History

<?php
/*
2016-12-29 12:09:26 +01:00
* This file is part of the NelmioApiDocBundle package.
*
2016-12-29 12:09:26 +01:00
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2016-12-29 12:09:26 +01:00
namespace Nelmio\ApiDocBundle\Routing;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
final class FilteredRouteCollectionBuilder
{
private $pathPatterns;
public function __construct(array $pathPatterns = [])
{
$this->pathPatterns = $pathPatterns;
}
public function filter(RouteCollection $routes): RouteCollection
{
$filteredRoutes = new RouteCollection();
foreach ($routes->all() as $name => $route) {
if ($this->match($route)) {
$filteredRoutes->add($name, $route);
}
}
return $filteredRoutes;
}
private function match(Route $route): bool
{
foreach ($this->pathPatterns as $pathPattern) {
if (preg_match('{'.$pathPattern.'}', $route->getPath())) {
return true;
}
}
return false;
}
}