/** * 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} "); }