/**
  * {@inheritdoc}
  */
 public function buildResponse(AutocompleteResults $results, AutocompleteContextInterface $context)
 {
     $data['results'] = [];
     $templateResult = $context->getParameter('select2_template_result');
     $templateSelection = $context->getParameter('select2_template_selection');
     foreach ($results as $id => $result) {
         $data['results'][] = ['id' => $id, 'text' => $this->renderSelect2Item($id, $result, $templateResult), 'selection_text' => $this->renderSelect2Item($id, $result, $templateSelection)];
     }
     if ($max = $context->getParameter('max_results', false)) {
         $data['pagination'] = ['more' => $results->count() == $max];
     }
     return new JsonResponse($data);
 }
 /**
  * {@inheritdoc}
  */
 public function buildResponse(AutocompleteResults $results, AutocompleteContextInterface $context)
 {
     $accessor = new PropertyAccessor();
     $data['results'] = [];
     $property = $context->getParameter('autocomplete');
     foreach ($results as $id => $result) {
         if ($accessor->isReadable($result, $property)) {
             $value = $accessor->getValue($result, $property);
             $data['results'][] = ['id' => $value, 'text' => $value];
         }
     }
     if ($max = $context->getParameter('max_results', false)) {
         $data['pagination'] = ['more' => $results->count() == $max];
     }
     return new JsonResponse($data);
 }
 /**
  * {@inheritdoc}
  */
 public function fetchResults(Request $request, AutocompleteContextInterface $context)
 {
     $maxResults = $context->getParameter('max_results');
     $class = $context->getParameter('class');
     $results = new AutocompleteResults();
     if ($class) {
         $qb = $this->createQueryBuilder($context);
         $qb->setMaxResults($maxResults);
         $this->orderBy($qb, $context);
         $this->filter($qb, $request->get('q'), $request->get('related_fields', []), $context);
         if ($request->get('page')) {
             $offset = $qb->getMaxResults() * ($request->get('page') - 1);
             $qb->setFirstResult($offset);
         }
         $rawResults = $qb->getQuery()->getResult();
         foreach ($rawResults as $result) {
             $ids = $qb->getEntityManager()->getClassMetadata($class)->getIdentifierValues($result);
             $id = current(array_values($ids));
             $results->offsetSet($id, $result);
         }
     }
     return $results;
 }