<?php
namespace App\Entity;
use App\Repository\AddressRepository;
use DateTime;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
#[ORM\Entity(repositoryClass: AddressRepository::class)]
#[ORM\Table(name: 'address')]
/**
* @Vich\Uploadable
*/
class Address implements UserObjectInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: Types::INTEGER)]
private ?int $id = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private DateTime $createdAt;
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
private ?string $locationMore = null;
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
private ?string $tgiBp = null;
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
private ?string $tgiPostalCode;
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
private ?string $tgiCommune;
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
private ?string $tgiName = null;
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
private ?string $tgiAdresses;
#[ORM\Column(type: Types::BOOLEAN)]
private bool $isDeleted = false;
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
private ?string $gmapsAddress;
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
private ?string $city = null;
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
private ?string $zipCode = null;
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
private ?string $streetNumber = null;
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
private ?string $roadType = null;
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
private ?string $roadName = null;
#[Assert\NotBlank(message: 'Ce champ est obligatoire')]
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
private ?string $houseType = null;
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
private ?string $placeName = null;
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
private ?string $roomNumber;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?DateTime $arrivalDate = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private $departureDate = null;
#[Assert\Choice(callback: 'getTypes', multiple: false)]
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
private ?string $type = null;
#[Assert\Choice(callback: 'getResidentTypes', multiple: false)]
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
private ?string $residentType;
#[ORM\Column(type: Types::STRING, length: 255, nullable: true)]
private ?string $addressFileName = null;
#[ORM\Column(type: Types::INTEGER, nullable: true)]
private ?int $addressFileSize = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private DateTime|null $addressFileUpdatedAt = null;
#[ORM\ManyToOne(targetEntity: User::class, fetch: 'EXTRA_LAZY', inversedBy: 'addressUser')]
private ?User $user = null;
/**
* @var Collection<Insurance>
*/
#[Assert\Valid]
#[ORM\OneToMany(mappedBy: 'addressInsurance', targetEntity: Insurance::class)]
private Collection $insurance;
/**
* @var Collection<Video>
*/
#[ORM\OneToMany(mappedBy: 'address', targetEntity: Video::class)]
private Collection $videos;
/**
* @var Collection<DisasterDeclaration>
*/
#[ORM\OneToMany(mappedBy: 'address', targetEntity: DisasterDeclaration::class)]
private Collection $disasterDeclarations;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
* @Vich\UploadableField(mapping="insurance", fileNameProperty="addressFileName", size="addressFileSize")
*/
private ?File $addressFile = null;
public function __construct()
{
$this->createdAt = new DateTime();
}
public function __toString()
{
return strval($this->id);
}
public function getId(): ?int
{
return $this->id;
}
public function setCreatedAt(DateTime $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
public function getCreatedAt(): DateTime
{
return $this->createdAt;
}
public function setLocationMore(?string $locationMore = null): static
{
$this->locationMore = $locationMore;
return $this;
}
public function getLocationMore(): ?string
{
return $this->locationMore;
}
public function setTgiBp(?string $tgiBp = null): static
{
$this->tgiBp = $tgiBp;
return $this;
}
public function getTgiBp(): ?string
{
return $this->tgiBp;
}
public function setTgiPostalCode(?String $tgiPostalCode = null): static
{
$this->tgiPostalCode = $tgiPostalCode;
return $this;
}
public function getTgiPostalCode(): ?string
{
return $this->tgiPostalCode;
}
public function setTgiCommune(?string $tgiCommune = null): static
{
$this->tgiCommune = $tgiCommune;
return $this;
}
public function getTgiCommune(): ?string
{
return $this->tgiCommune;
}
public function setTgiName(?string $tgiName = null): static
{
$this->tgiName = $tgiName;
return $this;
}
public function getTgiName(): ?string
{
return $this->tgiName;
}
public function setTgiAdresses(?string $tgiAdresses = null): static
{
$this->tgiAdresses = $tgiAdresses;
return $this;
}
public function getTgiAdresses(): ?string
{
return $this->tgiAdresses;
}
public function setIsDeleted(bool $isDeleted): static
{
$this->isDeleted = $isDeleted;
return $this;
}
public function getIsDeleted(): bool
{
return $this->isDeleted;
}
public function setGmapsAddress(?string $gmapsAddress = null): static
{
$this->gmapsAddress = $gmapsAddress;
return $this;
}
public function getGmapsAddress(): ?string
{
return $this->gmapsAddress;
}
public function setCity(?string $city = null): static
{
$this->city = $city;
return $this;
}
public function getCity(): ?string
{
return $this->city;
}
public function setZipCode(?string $zipCode = null): static
{
$this->zipCode = $zipCode;
return $this;
}
public function getZipCode(): ?string
{
return $this->zipCode;
}
public function setStreetNumber(?string $streetNumber = null): static
{
$this->streetNumber = $streetNumber;
return $this;
}
public function getStreetNumber(): ?string
{
return $this->streetNumber;
}
public function setRoadType(?string $roadType = null): static
{
$this->roadType = $roadType;
return $this;
}
public function getRoadType(): ?string
{
return $this->roadType;
}
public function setRoadName(?string $roadName = null): static
{
$this->roadName = $roadName;
return $this;
}
public function getRoadName(): ?string
{
return $this->roadName;
}
public function setHouseType(?string $houseType = null): static
{
$this->houseType = $houseType;
return $this;
}
public function getHouseType(): ?string
{
return $this->houseType;
}
public function setType(?string $type = null): static
{
$this->type = $type;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setResidentType(?string $residentType = null): string
{
$this->residentType = $residentType;
return $this;
}
public function getResidentType(): ?string
{
return $this->residentType;
}
public function setAddressFileName(?string $addressFileName): static
{
$this->addressFileName = $addressFileName;
return $this;
}
public function getAddressFileName(): ?string
{
return $this->addressFileName;
}
public function setAddressFileSize(?int $addressFileSize): static
{
$this->addressFileSize = $addressFileSize;
return $this;
}
public function getAddressFileSize(): ?int
{
return $this->addressFileSize;
}
public function setAddressFileUpdatedAt(DateTime|null $addressFileUpdatedAt): static
{
$this->addressFileUpdatedAt = $addressFileUpdatedAt;
return $this;
}
public function getAddressFileUpdatedAt(): ?DateTime
{
return $this->addressFileUpdatedAt;
}
public function setUser(?User $user = null): static
{
$this->user = $user;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function addInsurance(Insurance $insurance): static
{
$this->insurance[] = $insurance;
return $this;
}
public function removeInsurance(Insurance $insurance): bool
{
return $this->insurance->removeElement($insurance);
}
/**
* @return Collection<Insurance>
*/
public function getInsurance(): Collection
{
return $this->insurance;
}
public function addVideo(Video $video): static
{
$this->videos[] = $video;
return $this;
}
public function removeVideo(Video $video): bool
{
return $this->videos->removeElement($video);
}
/**
* @return Collection<Video>
*/
public function getVideos(): Collection
{
return $this->videos;
}
public function addDisasterDeclaration(DisasterDeclaration $disasterDeclaration): static
{
$this->disasterDeclarations[] = $disasterDeclaration;
return $this;
}
public function removeDisasterDeclaration(DisasterDeclaration $disasterDeclaration): bool
{
return $this->disasterDeclarations->removeElement($disasterDeclaration);
}
/**
* @return Collection<DisasterDeclaration>
*/
public function getDisasterDeclarations(): Collection
{
return $this->disasterDeclarations;
}
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*/
public function setAddressFile(File|UploadedFile $image = null): static
{
$this->addressFile = $image;
if (null !== $image) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->addressFileUpdatedAt = new DateTime();
}
return $this;
}
public function getAddressFile(): ?File
{
return $this->addressFile;
}
public function getHouseTypes(): array
{
$arr = [
"Appartement",
"Maison",
"Chalet",
"Ferme",
"Mobile home",
"Caravane",
"Loft",
"Villa",
];
return array_combine($arr, $arr);
}
public function getOtherPlaceTypes(): array
{
$arr = [
"Atelier",
"Box",
"Cabanon",
"Dépendance",
"Entrepôt de stockage",
"Garage",
"Grange",
"Hangar",
"Remise",
"Appartement à usage locatif",
"Maison à usage locatif",
"Immeuble à usage locatif",
];
if($this->houseType && !in_array($this->houseType, $arr)) {
$arr[] = $this->houseType;
}
return array_combine($arr, $arr);
}
public function getVacancyPlaceTypes()
{
$arr = [
"Appartement",
"Maison",
"Villa",
"Chalet",
"Bungalow",
"Mobile home",
"Cabanon",
];
return array_combine($arr, $arr);
}
public static function getTypes(bool $isMain = false, bool $hasMain = false, bool $isEdit = false): array
{
if ($hasMain) {
return [
"Domicile secondaire" => "second",
"Autre local" => "other",
];
}
if ($isMain) {
return [
"Domicile principal" => "main",
];
}
if ($isEdit) {
return [
"Domicile principal" => "main",
"Domicile secondaire" => "second",
"Autre local" => "other",
];
}
return [
"Domicile principal" => "main",
"hôtel" => "hotel",
"Domicile secondaire" => "second",
"Lieu de résidence" => "vacancy",
"Autre local" => "other",
];
}
public static function getResidentTypes(): array
{
return [
'Propriétaire' => 'owner',
'Locataire' => 'tenant',
'Occupant à titre gratuit' => 'freeOccupant',
'Propriétaire non occupant' => 'nonOccupantOwner',
];
}
public function getResidentTypeLabel(): string
{
$arr = [
"owner" => "Propriétaire",
"tenant" => "Locataire",
"freeOccupant" => "Occupant à titre gratuit",
"nonOccupantOwner" => "Propriétaire non occupant",
];
return $arr[$this->residentType] ?? '';
}
public function getPlaceName(): ?string
{
return $this->placeName;
}
public function setPlaceName(?string $placeName): self
{
$this->placeName = $placeName;
return $this;
}
public function getRoomNumber(): ?string
{
return $this->roomNumber;
}
public function setRoomNumber(?string $roomNumber): self
{
$this->roomNumber = $roomNumber;
return $this;
}
public function getArrivalDate(): ?\DateTimeInterface
{
return $this->arrivalDate;
}
public function setArrivalDate(?\DateTimeInterface $arrivalDate): self
{
$this->arrivalDate = $arrivalDate;
return $this;
}
public function getDepartureDate(): ?\DateTimeInterface
{
return $this->departureDate;
}
public function setDepartureDate(?\DateTimeInterface $departureDate): self
{
$this->departureDate = $departureDate;
return $this;
}
public function getActiveInsurance(): ?Insurance
{
$insurance = $this->insurance->filter(fn(Insurance $insurance) => !$insurance->getIsDeleted())->first();
return !$insurance ? null : $insurance;
}
public function hasActiveInsurance(): bool
{
return $this->getActiveInsurance() !== null;
}
public function inTypes(): bool
{
return in_array($this->type, $this->getTypes());
}
public function getCountry(): string
{
$parts = explode(',', $this->gmapsAddress);
return $parts[count($parts) - 1];
}
public function getResidentTypeKey(): int
{
$types = $this->getResidentTypes();
$k = 0;
foreach ($types as $type) {
if ($type === $this->residentType)
return $k;
$k++;
}
return -1;
}
public function getFullAddress(): string
{
$road = implode(' ', array_filter([
$this->streetNumber,
$this->roadName,
]));
$city = implode(' ', array_filter([
$this->zipCode,
$this->city,
]));
return implode(', ', array_filter([
$road,
$city,
$this->getCountry(),
]));
}
}