/**
  * @param Context $context
  */
 private function convertContext(Context $context)
 {
     $newContext = array();
     foreach ($context->getAttributes() as $key => $value) {
         $newContext[$key] = $value;
     }
     $newContext['groups'] = $context->getGroups();
     $newContext['version'] = $context->getVersion();
     $newContext['maxDepth'] = $context->getMaxDepth();
     return $newContext;
 }
 /**
  * @param Request $request
  * @param array $parameters
  * @param $viewContent
  * @param $itemsNumber
  * @param array $serializerGroups
  * @return View
  */
 public function create(Request $request, array $parameters, $viewContent, $itemsNumber, array $serializerGroups = [])
 {
     $view = View::create($viewContent);
     $headers = $view->getHeaders();
     $paginationHeader = $this->pagination->getHeader($request->get('_route'), $itemsNumber, $parameters);
     $headers = array_merge($headers, $paginationHeader);
     $view->setHeaders($headers);
     $context = new Context();
     $context->addGroups($serializerGroups);
     $view->setContext($context);
     return $view;
 }
 public function testApiWithoutSerializerGroups()
 {
     $this->parameterResolver->expects($this->once())->method('resolveApi')->will($this->returnValue(true));
     $this->parameterResolver->expects($this->once())->method('resolveSerializerGroups')->will($this->returnValue([]));
     $this->parameterResolver->expects($this->once())->method('resolveSerializerNull')->will($this->returnValue($null = true));
     $event = $this->createViewEventMock();
     $event->expects($this->once())->method('getView')->will($this->returnValue($view = $this->createViewMock()));
     $event->expects($this->once())->method('getResource')->will($this->returnValue($resource = $this->createResourceMock()));
     $resource->expects($this->once())->method('getName')->will($this->returnValue($name = 'name'));
     $view->expects($this->exactly(2))->method('getContext')->will($this->returnValue($context = new Context()));
     $this->subscriber->onApi($event);
     $this->assertSame([GroupsExclusionStrategy::DEFAULT_GROUP, 'lug.' . $name], $context->getGroups());
     $this->assertSame($null, $context->getSerializeNull());
 }
 public function testContextConversion()
 {
     $context = new Context();
     $context->setAttribute('groups', array('d'));
     $context->setAttribute('foo', 'bar');
     $context->setAttribute('version', 1);
     $context->addGroups(array('a', 'b', 'c'));
     $context->setVersion(1.3);
     $context->setMaxDepth(10);
     $context->setSerializeNull(false);
     $serializationContext = $this->adapter->convertSerializationContext($context);
     $this->assertTrue(is_array($serializationContext));
     $this->assertEquals(array('groups' => array('a', 'b', 'c'), 'foo' => 'bar', 'version' => 1.3, 'maxDepth' => 10, 'serializeNull' => false), $serializationContext);
     $deserializationContext = $this->adapter->convertDeserializationContext($context);
     $this->assertTrue(is_array($deserializationContext));
     $this->assertEquals(array('groups' => array('a', 'b', 'c'), 'foo' => 'bar', 'version' => 1.3, 'maxDepth' => 10), $deserializationContext);
 }
 public function testDeserializationContextConversion()
 {
     $context = new Context();
     $context->setAttribute('bar', 'foo');
     $context->setAttribute('version', 1);
     $context->addGroups(['e', 'f']);
     $context->setVersion(1.4);
     $context->setMaxDepth(10);
     $context->setSerializeNull(false);
     $JMSContext = $this->adapter->convertDeserializationContext($context);
     $this->assertInstanceOf(\JMS\Serializer\DeserializationContext::class, $JMSContext);
     $this->assertEquals('foo', $JMSContext->attributes->get('bar')->get());
     $this->assertEquals(['e', 'f'], $JMSContext->attributes->get('groups')->get());
     $this->assertEquals(1.4, $JMSContext->attributes->get('version')->get());
     $this->assertEquals(false, $JMSContext->shouldSerializeNull());
 }
 /**
  * @ApiDoc()
  */
 public function getAction($id)
 {
     $row = $this->_model->getRow($id);
     if (!$row) {
         $view = View::create(array(), Codes::HTTP_NOT_FOUND);
         $ctx = new Context();
         $view->setContext($ctx);
         return $view;
     }
     $this->denyAccessUnlessGranted('read', $row);
     $view = View::create(array('data' => $row));
     $ctx = new Context();
     $ctx->setGroups(array('rest_read', 'rest'));
     $view->setContext($ctx);
     return $view;
 }
 public function createView($data = null, $statusCode = null, array $headers = array())
 {
     $view = View::create($data, $statusCode, $headers);
     $ctx = new Context();
     $ctx->setGroups(array('rest_read', 'rest'));
     $view->setContext($ctx);
     return $view;
 }
 public function testContextConfiguration()
 {
     $converter = new RequestBodyParamConverter($this->serializer);
     $options = ['groups' => ['foo', 'bar'], 'version' => 'v1.2', 'maxDepth' => 5, 'serializeNull' => false, 'foo' => 'bar'];
     $contextConfigurationMethod = new \ReflectionMethod($converter, 'configureContext');
     $contextConfigurationMethod->setAccessible(true);
     $contextConfigurationMethod->invoke($converter, $context = new Context(), $options);
     $expectedContext = new Context();
     $expectedContext->addGroups($options['groups'])->setVersion($options['version'])->setMaxDepth($options['maxDepth'])->setSerializeNull($options['serializeNull'])->setAttribute('foo', 'bar');
     $this->assertEquals($expectedContext, $context);
 }
 /**
  * @param Context $context
  * @param array   $options
  */
 protected function configureContext(Context $context, array $options)
 {
     foreach ($options as $key => $value) {
         if ($key == 'groups') {
             $context->addGroups($options['groups']);
         } elseif ($key == 'version') {
             $context->setVersion($options['version']);
         } elseif ($key == 'maxDepth') {
             $context->setMaxDepth($options['maxDepth']);
         } elseif ($key == 'serializeNull') {
             $context->setSerializeNull($options['serializeNull']);
         } else {
             $context->setAttribute($key, $value);
         }
     }
 }
 /**
  * @param Context $context
  * @param int     $direction {@see self} constants
  *
  * @return JMSContext
  */
 private function convertContext(Context $context, $direction)
 {
     if ($direction === self::SERIALIZATION) {
         $jmsContext = JMSSerializationContext::create();
     } else {
         $jmsContext = JMSDeserializationContext::create();
         if (null !== $context->getMaxDepth()) {
             for ($i = 0; $i < $context->getMaxDepth(); ++$i) {
                 $jmsContext->increaseDepth();
             }
         }
     }
     foreach ($context->getAttributes() as $key => $value) {
         $jmsContext->attributes->set($key, $value);
     }
     if (null !== $context->getVersion()) {
         $jmsContext->setVersion($context->getVersion());
     }
     $groups = $context->getGroups();
     if (!empty($groups)) {
         $jmsContext->setGroups($context->getGroups());
     }
     if (null !== $context->getMaxDepth()) {
         $jmsContext->enableMaxDepthChecks();
     }
     if (null !== $context->getSerializeNull()) {
         $jmsContext->setSerializeNull($context->getSerializeNull());
     }
     return $jmsContext;
 }