<?php
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use App\Repository\LivreursRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: LivreursRepository::class)]
#[ApiResource]
class Livreurs
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $nom = null;
#[ORM\Column(length: 255)]
private ?string $telephone = null;
#[ORM\Column(length: 255)]
private ?string $cni = null;
#[ORM\Column(length: 255)]
private ?string $adresse = null;
#[ORM\Column]
private ?bool $disponibilite = null;
#[ORM\Column(length: 255)]
private ?string $email = null;
#[ORM\Column(length: 180, unique: true)]
private ?string $userprofile ;
#[ORM\OneToMany(mappedBy: 'id_livreurs', targetEntity: Livraisons::class)]
private Collection $livraisons;
#[ORM\ManyToOne(inversedBy: 'livreurs')]
private ?TypeTransport $id_TypeTransport = null;
#[ORM\OneToMany(mappedBy: 'deliverer', targetEntity: Comment::class, orphanRemoval: true)]
private Collection $comments;
#[ORM\Column(length: 255, nullable: true)]
private ?string $prenom = null;
#[ORM\Column(nullable: true)]
private ?float $longitude = null;
#[ORM\Column(nullable: true)]
private ?float $latitude = null;
// Méthode pour calculer la moyenne des étoiles
public function getAverageRating(): ?float
{
if ($this->comments->isEmpty()) {
return null;
}
$total = array_reduce($this->comments->toArray(), function ($sum, Comment $comment) {
return $sum + $comment->getRating();
}, 0);
return $total / count($this->comments);
}
public function __construct()
{
$this->livraisons = new ArrayCollection();
$this->comments = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getNom(): ?string
{
return $this->nom;
}
public function setNom(string $nom)
{
$this->nom = $nom;
return $this;
}
public function isDisponibilite(): ?bool
{
return $this->disponibilite;
}
public function setDisponibilite(bool $disponibilite)
{
$this->disponibilite = $disponibilite;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email)
{
$this->email = $email;
return $this;
}
/**
* @return Collection<int, Livraisons>
*/
public function getLivraisons(): Collection
{
return $this->livraisons;
}
public function addLivraison(Livraisons $livraison)
{
if (!$this->livraisons->contains($livraison)) {
$this->livraisons->add($livraison);
$livraison->setIdLivreurs($this);
}
return $this;
}
public function removeLivraison(Livraisons $livraison)
{
if ($this->livraisons->removeElement($livraison)) {
// set the owning side to null (unless already changed)
if ($livraison->getIdLivreurs() === $this) {
$livraison->setIdLivreurs(null);
}
}
return $this;
}
public function getIdTypeTransport(): ?TypeTransport
{
return $this->id_TypeTransport;
}
public function setIdTypeTransport(?TypeTransport $id_TypeTransport)
{
$this->id_TypeTransport = $id_TypeTransport;
return $this;
}
public function getTelephone(): ?string
{
return $this->telephone;
}
public function setTelephone(string $telephone): static
{
$this->telephone = $telephone;
return $this;
}
public function getCni(): ?string
{
return $this->cni;
}
public function setCni(string $cni): static
{
$this->cni = $cni;
return $this;
}
public function getAdresse(): ?string
{
return $this->adresse;
}
public function setAdresse(string $adresse): static
{
$this->adresse = $adresse;
return $this;
}
public function __toString(): string
{
return $this->prenom.' '.$this->nom; // Return a string representation
}
/**
* @return Collection<int, Comment>
*/
public function getComments(): Collection
{
return $this->comments;
}
public function addComment(Comment $comment): static
{
if (!$this->comments->contains($comment)) {
$this->comments->add($comment);
$comment->setDeliverer($this);
}
return $this;
}
public function removeComment(Comment $comment): static
{
if ($this->comments->removeElement($comment)) {
// set the owning side to null (unless already changed)
if ($comment->getDeliverer() === $this) {
$comment->setDeliverer(null);
}
}
return $this;
}
public function getUserprofile(): ?string
{
return $this->userprofile;
}
public function setUserprofile(string $userprofile): static
{
$this->userprofile = $userprofile;
return $this;
}
public function getPrenom(): ?string
{
return $this->prenom;
}
public function setPrenom(?string $prenom): static
{
$this->prenom = $prenom;
return $this;
}
public function getLongitude(): ?float
{
return $this->longitude;
}
public function setLongitude(?float $longitude): static
{
$this->longitude = $longitude;
return $this;
}
public function getLatitude(): ?float
{
return $this->latitude;
}
public function setLatitude(?float $latitude): static
{
$this->latitude = $latitude;
return $this;
}
}