<?php
namespace App\Controller;
use App\Repository\Reference\ReferencePropertyTypeRepository;
use App\Repository\Reference\ReferencePropertyContractTypeRepository;
use App\Repository\Reference\ReferencePropertyLandTypeRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Reference\ReferencePropertyDestination;
class PropertiesTypesPublicController extends AbstractController
{
private ReferencePropertyTypeRepository $referencePropertyTypeRepository;
private ReferencePropertyContractTypeRepository $referencePropertyContractTypeRepository;
private ReferencePropertyLandTypeRepository $referencePropertyLandTypeRepository;
private EntityManagerInterface $entityManager;
public function __construct(
EntityManagerInterface $entityManager,
ReferencePropertyTypeRepository $referencePropertyTypeRepository,
ReferencePropertyContractTypeRepository $referencePropertyContractTypeRepository,
ReferencePropertyLandTypeRepository $referencePropertyLandTypeRepository
) {
$this->entityManager = $entityManager;
$this->referencePropertyTypeRepository = $referencePropertyTypeRepository;
$this->referencePropertyContractTypeRepository = $referencePropertyContractTypeRepository;
$this->referencePropertyLandTypeRepository = $referencePropertyLandTypeRepository;
}
/**
* @Route("/api/internal/properties_types",name="properties_types")
*/
public function __invoke()
{
// Récupérer toutes les destinations.
$referencePropertyDestinations = $this->entityManager
->getRepository(ReferencePropertyDestination::class)->findAll();
// Simplifier le tableau des destinations.
$referencePropertyDestinationsCode = array_map(function ($childArray) {
return $childArray->getCode();
}, $referencePropertyDestinations);
// Récupérer toutes les types.
$propertiesTypesArray = [
'PropertyType' => $this->referencePropertyTypeRepository->findAll(),
'ContractType' => $this->referencePropertyContractTypeRepository->findAll(),
'LandType' => $this->referencePropertyLandTypeRepository->findAll()
];
// Simplifier les tableaux des types.
foreach ($propertiesTypesArray as $key => $value) {
$arrayName = $key . 'Simplified';
$$arrayName = array_map(function ($childArray) {
return ["code" => $childArray->getCode(), "name" => $childArray->getName()];
}, $value);
}
// Concatenation des code domaines avec les types associées.
$propertiesTypes = array_map(function ($domaine)
use ($PropertyTypeSimplified, $ContractTypeSimplified, $LandTypeSimplified) {
if (
$domaine === ReferencePropertyDestination::REFERENCE_PROPERTY_DESTINATION_TYPE_CODE_DWELLING
) {
return array_map(function ($type) use ($domaine) {
return ["domaineCode" => $domaine, "typeCode" => $type["code"], "typeName" => $type["name"]];
}, $PropertyTypeSimplified);
} elseif (
$domaine === ReferencePropertyDestination::REFERENCE_PROPERTY_DESTINATION_TYPE_CODE_COMMERCIAL_PROPERTY
) {
return array_map(function ($type) use ($domaine) {
return ["domaineCode" => $domaine, "typeCode" => $type["code"], "typeName" => $type["name"]];
}, $ContractTypeSimplified);
} elseif (
$domaine === ReferencePropertyDestination::REFERENCE_PROPERTY_DESTINATION_TYPE_CODE_LOT
) {
return array_map(function ($type) use ($domaine) {
return ["domaineCode" => $domaine, "typeCode" => $type["code"], "typeName" => $type["name"]];
}, $LandTypeSimplified);
}
}, $referencePropertyDestinationsCode);
// Simplification de résultats.
$simplifiedPropertiesTypeArray = [];
foreach ($propertiesTypes as $subArray) {
$domaineCode = $subArray[0]['domaineCode'];
foreach ($subArray as $item) {
$typeCode = $item['typeCode'];
$typeName = $item['typeName'];
$simplifiedPropertiesTypeArray[$domaineCode . '.' . $typeCode] = $typeName;
}
}
return new JsonResponse($simplifiedPropertiesTypeArray);
}
}