ApiPlatform\Core\Bridge\Symfony\Routing\IriConverter::getItemFromIri PHP Метод

getItemFromIri() публичный метод

public getItemFromIri ( string $iri, array $context = [] )
$iri string
$context array
    public function getItemFromIri(string $iri, array $context = [])
    {
        try {
            $parameters = $this->router->match($iri);
        } catch (RoutingExceptionInterface $e) {
            throw new InvalidArgumentException(sprintf('No route matches "%s".', $iri), $e->getCode(), $e);
        }
        if (!isset($parameters['_api_resource_class'], $parameters['id'])) {
            throw new InvalidArgumentException(sprintf('No resource associated to "%s".', $iri));
        }
        if ($item = $this->itemDataProvider->getItem($parameters['_api_resource_class'], $parameters['id'], null, $context)) {
            return $item;
        }
        throw new InvalidArgumentException(sprintf('Item not found for "%s".', $iri));
    }

Usage Example

Пример #1
0
 public function testGetItemFromIri()
 {
     $propertyNameCollectionFactoryProphecy = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
     $propertyMetadataFactoryProphecy = $this->prophesize(PropertyMetadataFactoryInterface::class);
     $itemDataProviderProphecy = $this->prophesize(ItemDataProviderInterface::class);
     $itemDataProviderProphecy->getItem('AppBundle\\Entity\\User', 3, null, ['fetch_data' => true])->willReturn('foo')->shouldBeCalledTimes(1);
     $routeNameResolverProphecy = $this->prophesize(RouteNameResolverInterface::class);
     $routerProphecy = $this->prophesize(RouterInterface::class);
     $routerProphecy->match('/users/3')->willReturn(['_api_resource_class' => 'AppBundle\\Entity\\User', 'id' => 3])->shouldBeCalledTimes(1);
     $converter = new IriConverter($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal(), $itemDataProviderProphecy->reveal(), $routeNameResolverProphecy->reveal(), $routerProphecy->reveal());
     $converter->getItemFromIri('/users/3', ['fetch_data' => true]);
 }