<?php
namespace App\Controller\Api;
use App\Entity\Country;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class CountryController extends ApiController
{
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/public/country", name="api_country_list", methods={"POST"})
*/
public function listCountry(Request $request)
{
$prefixFile = $this->getParameter("app.path.country_images");
$baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath();
$countries = [];
$spain = null;
$uk = null;
/** @var Country $country */
foreach ($this->em->getRepository(Country::class)->findBy([],['name'=>'ASC']) as $country){
$code = strtoupper($country->getCode());
if($code != "ES" && $code != "GB")
array_push($countries,$country->__toArray($baseurl . $prefixFile . "/"));
else{
if($code == "ES")
$spain = $country->__toArray($baseurl . $prefixFile . "/");
else{
$uk = $country->__toArray($baseurl . $prefixFile . "/");
}
}
}
if(!empty($uk))
array_unshift($countries,$uk);
if(!empty($spain))
array_unshift($countries,$spain);
return $this->response($countries);
}
}