/** * Compile SearchParameterBag * * @param SearchParameterBag $parameterBag * @return \HealthCareAbroad\SearchBundle\Services\CompiledSearchParameter */ public function compile(SearchParameterBag $parameterBag) { $urlGenerator = new SearchUrlGenerator(); $variables = $this->_extractVariables($parameterBag); $searchState = 0; // search form parameter to search url route key mapping $searchUrlParameterKeyMapping = SearchUrlGenerator::getSearchParameterKeyToSearchUrlKeyMapping(); // parameter key to search state value mapping $searchStateMapping = SearchStates::getSearchParameterKeyMappingToSearchStateValues(); // get the current state of the search based on current variables, prepare also the url parameters for url generator foreach ($variables as $key => $variableObj) { // add the state value for this key $searchState += $searchStateMapping[$key]; // map to the route key $searchUrlKey = \array_key_exists($key, $searchUrlParameterKeyMapping) ? $searchUrlParameterKeyMapping[$key] : $key; // TODO: this will fail if slug will not be used as a URL parameter value if (\method_exists($variableObj, 'getSlug')) { $urlGenerator->addParameter($searchUrlKey, $variableObj->getSlug()); } } // route to state mapping $routeToStateMapping = SearchStates::getStateToRouteMapping(); $routeName = $routeToStateMapping[SearchStates::getSearchStateFromValue($searchState)]; $url = $urlGenerator->generateByRouteName($routeName, false); // generate the url $compiledSearch = new CompiledSearchParameter($variables, $url, $searchState); return $compiledSearch; }
public function testGenerateByRouteNameForResultCityTreatment() { $generator = new SearchUrlGenerator(); $generator->addParameter(SearchUrlGenerator::SEARCH_URL_PARAMETER_COUNTRY, 'c1'); $generator->addParameter(SearchUrlGenerator::SEARCH_URL_PARAMETER_CITY, 'ct1'); $generator->addParameter(SearchUrlGenerator::SEARCH_URL_PARAMETER_SPECIALIZATION, 's1'); $generator->addParameter(SearchUrlGenerator::SEARCH_URL_PARAMETER_TREATMENT, 'tr1'); $url = $generator->generateByRouteName(SearchUrlRoutes::RESULTS_CITY_TREATMENT); $expectedUrl = '/c1/ct1/s1/tr1/treatment'; $this->assertEquals($expectedUrl, $url, "Failed asserting that expected url: {$expectedUrl} is equal to generated url: {$url} "); }
public function ajaxProcessSearchParametersAction(Request $request) { $searchParameterService = $this->get('services.search.parameters'); $compiledSearch = $searchParameterService->compileRequest($request); // no app_dev.php $url = $compiledSearch->getUrl(); $metaConfigurationService = $this->get('services.helper.page_meta_configuration'); $metaConfiguration = $metaConfigurationService->findOneByUrl($url); // no meta configuration saved, yet, create from builder if (!$metaConfiguration) { $searchVariables = array(); // convert variable keys to the one accepted by the meta configuration service $searchUrlParameterKeyMapping = SearchUrlGenerator::getSearchParameterKeyToSearchUrlKeyMapping(); foreach ($compiledSearch->getVariables() as $key => $searchVariable) { if (!\array_key_exists($key, $searchUrlParameterKeyMapping)) { // this is a predefined set, so something must have gone wrong in the search parameter compiler, or the mapping has changed throw new \Exception(\sprintf('Cannot map search parameter key "%s" to a search url key', $key)); } $searchVariables[$searchUrlParameterKeyMapping[$key]] = $searchVariable; } $metaConfiguration = $metaConfigurationService->buildFromSearchObjects($searchVariables); $metaConfiguration->setUrl($url); // save this new configuration $metaConfigurationService->save($metaConfiguration); } $form = $this->createForm(new PageMetaConfigurationFormType(), $metaConfiguration); $html = $this->renderView('AdminBundle:PageMetaConfiguration:form.html.twig', array('form' => $form->createView())); return new Response(\json_encode(array('html' => $html)), 200, array('content-type' => 'application/json')); }