|
- <?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 AddressApiController extends AbstractController
- {
- protected $utils ;
-
- public function __construct(Utils $utils)
- {
- $this->utils = $utils ;
- }
-
- public function cities(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) {
-
- $codesPostaux = $city->codesPostaux ;
-
- if($context == 'frontend') {
- $label = '<span class="city">'.$city->nom.'</span> <span class="zip">'.$codesPostaux[0].'</span>' ;
- }
- else {
- $label = $city->nom.' - '.$codesPostaux[0] ;
- }
-
- $return[] = [
- 'label' => $label,
- 'city' => $city->nom,
- 'value' => $city->code
- ] ;
- }
-
- if($context == 'frontend') {
- $return = [
- 'items' => $return
- ] ;
- }
-
- return new JsonResponse($return) ;
- }
-
- public function addresses(Request $request) : JsonResponse
- {
- $return = [] ;
- $address = $request->get('address') ;
- $context = $request->get('context') ;
- $results = $this->utils->callAddressApi($address) ;
-
- foreach($results as $result) {
- if($result->getStreetNumber() && strlen($result->getStreetNumber()) > 0) {
- $streetNameNumber = $result->getStreetNumber().' '.$result->getStreetName() ;
- $return[] = [
- 'label' => $streetNameNumber,
- 'value' => $streetNameNumber,
- 'latitude' => $result->getCoordinates()->getLatitude(),
- 'longitude' => $result->getCoordinates()->getLongitude()
- ] ;
- }
- }
-
- if($context == 'frontend') {
- $return = [
- 'items' => $return
- ] ;
- }
-
- return new JsonResponse($return) ;
- }
- }
|