本文整理匯總了PHP中Symfony\Component\HttpFoundation\Request::setFormat方法的典型用法代碼示例。如果您正苦於以下問題:PHP Request::setFormat方法的具體用法?PHP Request::setFormat怎麽用?PHP Request::setFormat使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Symfony\Component\HttpFoundation\Request
的用法示例。
在下文中一共展示了Request::setFormat方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: testAPriorityFormatIsFound
/**
* Tests the getContentType() method when a priority format is found.
*
* @dataProvider priorityFormatProvider
* @covers ::getContentType
*/
public function testAPriorityFormatIsFound($priority, $format)
{
$request = new Request();
$request->setFormat($format['format'], $format['mime_type']);
$request->headers->set('Accept', sprintf('%s,application/json', $format['mime_type']));
$this->assertSame($priority, $this->contentNegotiation->getContentType($request));
}
示例2: handle
/**
* {@inheritdoc}
*/
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
{
foreach ($this->formats as $format => $mime_type) {
$request->setFormat($format, $mime_type);
}
$request->setRequestFormat($this->negotiator->getContentType($request));
return $this->app->handle($request, $type, $catch);
}
示例3: handle
/**
* {@inheritdoc}
*/
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE)
{
// Register available mime types.
foreach ($this->formats as $format => $mime_type) {
$request->setFormat($format, $mime_type);
}
// Determine the request format using the negotiator.
$request->setRequestFormat($this->getContentType($request));
return $this->app->handle($request, $type, $catch);
}
示例4: testOnKernelRequest
/**
* @return void
*/
public function testOnKernelRequest()
{
$server = array('HTTP_ACCEPT' => 'application/vnd.com.swisscom.translation+json');
$request = new Request(array(), array(), array(), array(), array(), $server);
$request->setFormat('json', 'application/json');
$eventDouble = $this->getMockBuilder('\\Graviton\\RestBundle\\Event\\RestEvent')->disableOriginalConstructor()->setMethods(array('getRequest'))->getMock();
$eventDouble->expects($this->once())->method('getRequest')->willReturn($request);
$containerDouble = $this->getMockBuilder('\\Symfony\\Component\\DependencyInjection\\Container')->disableOriginalConstructor()->setMethods(array('getParameter'))->getMock();
$containerDouble->expects($this->once())->method('getParameter')->with($this->equalTo('graviton.rest.special_mimetypes'))->willReturn(array('json' => array('application/vnd.com.swisscom.translation+json')));
$listener = new SpecialMimetypeRequestListener($containerDouble);
$listener->onKernelRequest($eventDouble);
$this->assertEquals('json', $request->getFormat('application/vnd.com.swisscom.translation+json'));
}
示例5: testActionWithUncatchableException
public function testActionWithUncatchableException()
{
$serializerException = $this->prophesize(ExceptionInterface::class);
$serializerException->willExtend(\Exception::class);
$flattenException = FlattenException::create($serializerException->reveal());
$serializer = $this->prophesize(SerializerInterface::class);
$serializer->serialize($flattenException, 'jsonproblem')->willReturn();
$exceptionAction = new ExceptionAction($serializer->reveal(), ['jsonproblem' => ['application/problem+json'], 'jsonld' => ['application/ld+json']]);
$request = new Request();
$request->setFormat('jsonproblem', 'application/problem+json');
$expected = new Response('', Response::HTTP_INTERNAL_SERVER_ERROR, ['Content-Type' => 'application/problem+json; charset=utf-8', 'X-Content-Type-Options' => 'nosniff', 'X-Frame-Options' => 'deny']);
$this->assertEquals($expected, $exceptionAction($flattenException, $request));
}
示例6: testContentNegotiation
public function testContentNegotiation()
{
$eventProphecy = $this->prophesize(GetResponseEvent::class);
$request = new Request([], [], ['_api_resource_class' => 'Foo', '_api_collection_operation_name' => 'post'], [], [], [], '{}');
$request->setMethod(Request::METHOD_POST);
$request->headers->set('Content-Type', 'text/xml');
$request->setFormat('xml', 'text/xml');
// Workaround to avoid weird behaviors
$eventProphecy->getRequest()->willReturn($request)->shouldBeCalled();
$serializerProphecy = $this->prophesize(SerializerInterface::class);
$serializerProphecy->deserialize('{}', 'Foo', 'xml', [])->willReturn(new \stdClass())->shouldBeCalled();
$serializerContextBuilderProphecy = $this->prophesize(SerializerContextBuilderInterface::class);
$serializerContextBuilderProphecy->createFromRequest(Argument::type(Request::class), false, Argument::type('array'))->willReturn([])->shouldBeCalled();
$listener = new DeserializeListener($serializerProphecy->reveal(), $serializerContextBuilderProphecy->reveal(), ['jsonld' => ['application/ld+json'], 'xml' => ['text/xml']]);
$listener->onKernelRequest($eventProphecy->reveal());
}
示例7: testLivePreview
/**
* Tests the live preview output for json output.
*/
public function testLivePreview()
{
// We set up a request so it looks like an request in the live preview.
$request = new Request();
$request->setFormat('drupal_ajax', 'application/vnd.drupal-ajax');
$request->headers->set('Accept', 'application/vnd.drupal-ajax');
/** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */
$request_stack = \Drupal::service('request_stack');
$request_stack->push($request);
$view = Views::getView('test_serializer_display_entity');
$view->setDisplay('rest_export_1');
$this->executeView($view);
// Get the serializer service.
$serializer = $this->container->get('serializer');
$entities = array();
foreach ($view->result as $row) {
$entities[] = $row->_entity;
}
$expected = $serializer->serialize($entities, 'json');
$view->live_preview = TRUE;
$build = $view->preview();
$rendered_json = $build['#plain_text'];
$this->assertTrue(!isset($build['#markup']) && $rendered_json == $expected, 'Ensure the previewed json is escaped.');
$view->destroy();
$expected = $serializer->serialize($entities, 'xml');
// Change the request format to xml.
$view->setDisplay('rest_export_1');
$view->getDisplay()->setOption('style', array('type' => 'serializer', 'options' => array('uses_fields' => FALSE, 'formats' => array('xml' => 'xml'))));
$this->executeView($view);
$build = $view->preview();
$rendered_xml = $build['#plain_text'];
$this->assertEqual($rendered_xml, $expected, 'Ensure we preview xml when we change the request format.');
}
示例8: testGetFormatFromMimeType
/**
* @covers Symfony\Component\HttpFoundation\Request::getFormat
* @covers Symfony\Component\HttpFoundation\Request::setFormat
* @dataProvider getFormatToMimeTypeMapProvider
*/
public function testGetFormatFromMimeType($format, $mimeTypes)
{
$request = new Request();
foreach ($mimeTypes as $mime) {
$this->assertEquals($format, $request->getFormat($mime));
}
$request->setFormat($format, $mimeTypes);
foreach ($mimeTypes as $mime) {
$this->assertEquals($format, $request->getFormat($mime));
}
}
示例9: testGetFormatWithCustomMimeType
public function testGetFormatWithCustomMimeType()
{
$request = new Request();
$request->setFormat('custom', 'application/vnd.foo.api;myversion=2.3');
$this->assertEquals('custom', $request->getFormat('application/vnd.foo.api;myversion=2.3'));
}
示例10: testLivePreview
/**
* Tests the live preview output for json output.
*/
public function testLivePreview()
{
// We set up a request so it looks like an request in the live preview.
$request = new Request();
$request->setFormat('drupal_ajax', 'application/vnd.drupal-ajax');
$request->headers->set('Accept', 'application/vnd.drupal-ajax');
/** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */
$request_stack = \Drupal::service('request_stack');
$request_stack->push($request);
$view = Views::getView('test_serializer_display_entity');
$view->setDisplay('rest_export_1');
$this->executeView($view);
// Get the serializer service.
$serializer = $this->container->get('serializer');
$entities = array();
foreach ($view->result as $row) {
$entities[] = $row->_entity;
}
$expected = String::checkPlain($serializer->serialize($entities, 'json'));
$view->live_preview = TRUE;
$build = $view->preview();
$rendered_json = $build['#markup'];
$this->assertEqual($rendered_json, $expected, 'Ensure the previewed json is escaped.');
}
示例11: parseJsonRequest
private function parseJsonRequest(Request $request)
{
$request->setFormat('json', 'application/json');
if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
$data = json_decode($request->getContent(), true);
$request->request->replace(is_array($data) ? $data : array());
}
}
示例12: addRequestFormats
/**
* Adds API formats to the HttpFoundation Request.
*
* @param Request $request
* @param array $formats
*/
private function addRequestFormats(Request $request, array $formats)
{
foreach ($formats as $format => $mimeTypes) {
$request->setFormat($format, $mimeTypes);
}
}
示例13: validateRequestContentType
/**
* This before middleware validates that the request body is in a format
* that the application understands.
*
* @param Request $request
* @throws UnsupportedMediaTypeHttpException
*/
public function validateRequestContentType(Request $request, Application $app)
{
// Define the "form" format so we can use it for validation
$request->setFormat("form", array("application/x-www-form-urlencoded", "multipart/form-data"));
$format = $request->getContentType();
if (strlen($request->getContent()) > 0 && !in_array($format, $app["conneg.requestFormats"])) {
// The request has a body but it is not a supported media type
throw new UnsupportedMediaTypeHttpException();
}
}