<?php
|
|
|
|
namespace App\Form;
|
|
|
|
use App\Service\OverpassClient;
|
|
use Symfony\Component\Form\AbstractType;
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
|
use Symfony\Component\Form\FormBuilderInterface;
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
|
|
class CityToolType extends AbstractType
|
|
{
|
|
public function __construct(
|
|
private OverpassClient $overpass,
|
|
private RequestStack $requestStack,
|
|
) {
|
|
}
|
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options): void
|
|
{
|
|
$session = $this->requestStack->getSession();
|
|
$choices = $session->has('city_tool_choices') ? $session->get('city_tool_choices') : [];
|
|
|
|
if (empty($choices)) {
|
|
$entries = $this->overpass->rawQuery('[out:csv(::id,"ref","name")][timeout:25];area(3602202162)->.searchArea;relation["name"]["boundary"="administrative"]["border_type"="departement"]["admin_level"="6"](area.searchArea);out;');
|
|
$col = [];
|
|
$row = [];
|
|
$entries = explode("\n", $entries);
|
|
foreach ($entries as $index => $entry) {
|
|
$entry = explode("\t", $entry);
|
|
if (0 === $index) {
|
|
$col = $entry;
|
|
} else {
|
|
if (count($col) === count($entry)) {
|
|
$row = array_combine($col, $entry);
|
|
$key = sprintf('%s %s', $row['ref'], $row['name']);
|
|
$val = (int) $row['@id'];
|
|
$choices[$key] = $val;
|
|
}
|
|
}
|
|
}
|
|
ksort($choices);
|
|
}
|
|
|
|
$session->set('city_tool_choices', $choices);
|
|
|
|
$builder
|
|
->add('area', ChoiceType::class, [
|
|
'label' => 'Département',
|
|
'choices' => $choices,
|
|
])
|
|
;
|
|
}
|
|
}
|