|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- <?php
-
- namespace Lc\ShopBundle\Controller ;
-
- use Lc\ShopBundle\Services\CityUtils;
- use Lc\ShopBundle\Services\Utils;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\JsonResponse;
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
-
- class CitiesController extends AbstractController
- {
- protected $utils ;
-
- public function __construct(Utils $utils)
- {
- $this->utils = $utils ;
- }
-
- public function index(Request $request) : JsonResponse
- {
- $term = $request->get('term') ;
- $context = $request->get('context') ;
- $data = [
- 'boost' => 'population',
- ] ;
-
- if(strlen($term) == 5 && is_numeric($term)) {
- $data['codePostal'] = $term ;
- }
- else {
- $data['nom'] = $term;
- }
-
- $result = array_merge(
- json_decode($this->utils->callCitiesApi('get', 'communes', array_merge($data, ['codeRegion' => 27]))),
- json_decode($this->utils->callCitiesApi('get', 'communes', array_merge($data, ['codeRegion' => 44])))
- );
-
- $return = [] ;
- foreach($result as $city) {
- $return[] = [
- 'label' => $city->nom,
- 'value' => $city->code
- ] ;
- }
-
- if($context == 'frontend') {
- $return = [
- 'items' => $return
- ] ;
- }
-
- return new JsonResponse($return) ;
- }
-
-
- }
|