/**
  * @param GetResponseForControllerResultEvent $event
  */
 public function serializeResponse(GetResponseForControllerResultEvent $event)
 {
     if ($this->doSerialize) {
         $data = $event->getControllerResult();
         $apiResponse = new ApiResponse(200, $data);
         $data = array_merge($apiResponse->toArray(), $this->data->all());
         $data = array_filter($data);
         if (!isset($data['data'])) {
             $data['data'] = [];
         }
         $context = new SerializationContext();
         $context->setSerializeNull(true);
         if (method_exists($context, 'enableMaxDepthChecks')) {
             $context->enableMaxDepthChecks();
         }
         if ($action = $this->getAction($event)) {
             $context->setGroups($action->getSerializationGroups());
         }
         if ($fields = $event->getRequest()->query->get('fields')) {
             $context->addExclusionStrategy(new FieldsListExclusionStrategy($fields));
         }
         $json = $this->serializer->serialize($data, 'json', $context);
         $response = new Response($json, 200, ['Content-Type' => 'application/json']);
         $event->setResponse($response);
         $event->stopPropagation();
     }
 }
 /** @test */
 public function the_returned_data_is_sorted()
 {
     $response = new ApiResponse(200, ['my_stuff' => 'comes here']);
     $response->set('meta', 'will be here');
     $response->set('additional_data', 'will be here');
     $data = $response->toArray();
     $keys = array_keys($data);
     $this->assertEquals('status', $keys[0]);
     $this->assertEquals('type', $keys[1]);
     $this->assertEquals('data', $keys[2]);
     $this->assertEquals('additional_data', $keys[3]);
     $this->assertEquals('meta', $keys[4]);
 }
 /**
  * @return array
  */
 public function toArray()
 {
     return array_merge(parent::toArray(), ['title' => $this->title]);
 }