eZ\Bundle\EzPublishRestBundle\Routing\OptionsLoader\RouteCollectionMapper::mapCollection PHP Method

mapCollection() public method

Iterates over $restRouteCollection, and returns the corresponding RouteCollection of OPTIONS REST routes.
public mapCollection ( RouteCollection $restRouteCollection ) : RouteCollection
$restRouteCollection Symfony\Component\Routing\RouteCollection
return Symfony\Component\Routing\RouteCollection
    public function mapCollection(RouteCollection $restRouteCollection)
    {
        $optionsRouteCollection = new RouteCollection();
        foreach ($restRouteCollection->all() as $restRoute) {
            $optionsRouteName = $this->mapper->getOptionsRouteName($restRoute);
            $optionsRoute = $optionsRouteCollection->get($optionsRouteName);
            if ($optionsRoute === null) {
                $optionsRoute = $this->mapper->mapRoute($restRoute);
            } else {
                $optionsRoute = $this->mapper->mergeMethodsDefault($optionsRoute, $restRoute);
            }
            $optionsRouteCollection->add($optionsRouteName, $optionsRoute);
        }
        return $optionsRouteCollection;
    }

Usage Example

 public function testAddRestRoutesCollection()
 {
     $restRoutesCollection = new RouteCollection();
     $restRoutesCollection->add('ezpublish_rest_route_one_get', $this->createRoute('/route/one', array('GET')));
     $restRoutesCollection->add('ezpublish_rest_route_one_post', $this->createRoute('/route/one', array('POST')));
     $restRoutesCollection->add('ezpublish_rest_route_two_delete', $this->createRoute('/route/two', array('DELETE')));
     $optionsRouteCollection = $this->collectionMapper->mapCollection($restRoutesCollection);
     self::assertEquals(2, $optionsRouteCollection->count());
     self::assertInstanceOf('Symfony\\Component\\Routing\\Route', $optionsRouteCollection->get('ezpublish_rest_options_route_one'));
     self::assertInstanceOf('Symfony\\Component\\Routing\\Route', $optionsRouteCollection->get('ezpublish_rest_options_route_two'));
     self::assertEquals('GET,POST', $optionsRouteCollection->get('ezpublish_rest_options_route_one')->getDefault('allowedMethods'));
     self::assertEquals('DELETE', $optionsRouteCollection->get('ezpublish_rest_options_route_two')->getDefault('allowedMethods'));
 }
RouteCollectionMapper