<?php
namespace App\Service;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class DataService
{
public function __construct(HttpClientInterface $client)
{
$this->client = $client;
}
public function fetchPrestations(): array
{
$response = $this->client->request('GET', "{$_ENV['BASE_API']}prestations");
return $response->toArray()["prestations"];
}
public function fetchSpecialites(): array
{
$response = $this->client->request('GET', "{$_ENV['BASE_API']}specialites");
return $response->toArray()["specialites"];
}
public function fetchDiplomes(): array
{
$response = $this->client->request('GET', "{$_ENV['BASE_API']}diplomes");
return $response->toArray()["diplomes"];
}
public function fetchMutuelles(): array
{
$response = $this->client->request(
'GET',
"{$_ENV['BASE_API']}mutuelles"
);
return $response->toArray()["mutuelles"];
}
public function fetchStations(): array
{
$response = $this->client->request(
'GET',
"{$_ENV['BASE_API']}stations/city"
);
return $response->toArray()["stations"];
}
public function fetchCity(): array
{
$response = $this->client->request(
'GET',
"{$_ENV['BASE_API']}villes"
);
$data = $response->toArray();
if (isset($data['villes'])) {
$collator = new \Collator('fr_FR');
usort($data['villes'], function ($a, $b) use ($collator) {
preg_match('/^([^\d]+)\s*(\d*)$/u', $a['nom'], $matchesA);
preg_match('/^([^\d]+)\s*(\d*)$/u', $b['nom'], $matchesB);
$nameCmp = $collator->compare(trim($matchesA[1]), trim($matchesB[1]));
if ($nameCmp !== 0) {
return $nameCmp;
}
$numA = isset($matchesA[2]) && $matchesA[2] !== '' ? (int)$matchesA[2] : 0;
$numB = isset($matchesB[2]) && $matchesB[2] !== '' ? (int)$matchesB[2] : 0;
return $numA <=> $numB;
});
return $data['villes'];
}
return [];
}
public function fetchData(): array
{
$prestations = $this->fetchPrestations();
$specialites = $this->fetchSpecialites();
$diplomes = $this->fetchDiplomes();
$mutuelles = $this->fetchMutuelles();
$stations = $this->fetchStations();
return [
"prestations" => $prestations,
"specialites" => $specialites,
"diplomes" => $diplomes,
"mutuelles" => $mutuelles,
"stations" => $stations,
];
}
}