|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?php
-
- namespace Lc\ShopBundle\Controller ;
-
- use Lc\ShopBundle\Services\Cities;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\JsonResponse;
- use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
-
- class CitiesController extends AbstractController
- {
- protected $cities ;
-
- public function __construct(Cities $cities)
- {
- $this->cities = $cities ;
- }
-
- public function index(Request $request) : JsonResponse
- {
- $term = $request->get('term') ;
- $context = $request->get('context') ;
- $data = [
- 'boost' => 'population',
- ] ;
-
- if(strlen($term) == 5) {
- $data['codePostal'] = $term ;
- }
- else {
- $data['nom'] = $term;
- }
-
- $result = array_merge(
- json_decode($this->cities->callApi('get', 'communes', array_merge($data, ['codeRegion' => 27]))),
- json_decode($this->cities->callApi('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) ;
- }
-
-
- }
|