src/Entity/UserProjets.php line 15

  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserProjetsRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. #[ORM\Entity(repositoryClassUserProjetsRepository::class)]
  9. #[UniqueEntity(fields: ['projet''user'], message'This user is already linked to this project.')]
  10. class UserProjets
  11. {
  12.     #[ORM\Id]
  13.     #[ORM\GeneratedValue]
  14.     #[ORM\Column]
  15.     private ?int $id null;
  16.     #[ORM\ManyToOne(inversedBy'userProjets')]
  17.     #[ORM\JoinColumn(nullablefalse)]
  18.     private ?User $user null;
  19.     #[ORM\ManyToOne(inversedBy'userProjets')]
  20.     #[ORM\JoinColumn(nullablefalse)]
  21.     private ?Projets $projet null;
  22.     #[ORM\Column]
  23.     private ?\DateTimeImmutable $createdAt null;
  24.     #[ORM\Column]
  25.     private ?bool $Completed null;
  26.     #[ORM\OneToMany(mappedBy'projet'targetEntityCommentaires::class, cascade: ['persist''remove'])]
  27.     private Collection $commentaires;
  28.     #[ORM\Column(nullabletrue)]
  29.     private ?\DateTimeImmutable $finishedAt null;
  30.     #[ORM\Column(length255nullabletrue)]
  31.     private ?string $github null;
  32.     private $notesRepository;
  33.     public function __construct()
  34.     {
  35.         $this->Completed false;
  36.         $this->createdAt = new \DateTimeImmutable();
  37.         $this->commentaires = new ArrayCollection();
  38.     }
  39.     public function getId(): ?int
  40.     {
  41.         return $this->id;
  42.     }
  43.     public function getUser(): ?User
  44.     {
  45.         return $this->user;
  46.     }
  47.     public function setUser(?User $user): self
  48.     {
  49.         $this->user $user;
  50.         return $this;
  51.     }
  52.     public function getProjet(): ?Projets
  53.     {
  54.         return $this->projet;
  55.     }
  56.     public function setProjet(?Projets $projet): self
  57.     {
  58.         $this->projet $projet;
  59.         return $this;
  60.     }
  61.     public function getCreatedAt(): ?\DateTimeImmutable
  62.     {
  63.         return $this->createdAt;
  64.     }
  65.     public function setCreatedAt(\DateTimeImmutable $createdAt): self
  66.     {
  67.         $this->createdAt $createdAt;
  68.         return $this;
  69.     }
  70.     public function isCompleted(): ?bool
  71.     {
  72.         return $this->Completed;
  73.     }
  74.     public function setCompleted(bool $Completed): self
  75.     {
  76.         $this->Completed $Completed;
  77.         return $this;
  78.     }
  79.     /**
  80.      * @return Collection<int, Commentaires>
  81.      */
  82.     public function getCommentaires(): Collection
  83.     {
  84.         return $this->commentaires;
  85.     }
  86.     public function getCommentairesJS(): string
  87.     {
  88.         $commentaires $this->commentaires->getValues();
  89.         $commentairesJS = [];
  90.         foreach ($commentaires as $commentaire) {
  91.             $commentairesJS[] = [
  92.                 'id' => $commentaire->getId(),
  93.                 'text' => $commentaire->getText(),
  94.                 'user' => $commentaire->getUser()->getUserInfos()->getName(),
  95.                 'image' => $commentaire->getUser()->getUserInfos()->getImage(),
  96.                 'rating' => $commentaire->getNotes()->isEmpty() ? null $commentaire->getNotes()[0]->getRating(),
  97.                 'createdAt' => $commentaire->getCreatedAt()->format('d/m/Y - H:i'),
  98.             ];
  99.         }
  100.         return json_encode($commentairesJS);
  101.     }
  102.     public function getAverageRatings(): array
  103.     {
  104.         $commentaires $this->commentaires->getValues();
  105.         $ratings = [];
  106.         foreach ($commentaires as $commentaire) {
  107.             if (!$commentaire->getNotes()->isEmpty()) {
  108.                 $ratings[] = $commentaire->getNotes()[0]->getRating();
  109.             }
  110.         }
  111.         if (count($ratings) === 0) {
  112.             return [00];
  113.         }
  114.         return [array_sum($ratings) / count($ratings), count($ratings)];
  115.     }
  116.     public function addCommentaire(Commentaires $commentaire): self
  117.     {
  118.         if (!$this->commentaires->contains($commentaire)) {
  119.             $this->commentaires->add($commentaire);
  120.             $commentaire->setProjet($this);
  121.         }
  122.         return $this;
  123.     }
  124.     public function removeCommentaire(Commentaires $commentaire): self
  125.     {
  126.         if ($this->commentaires->removeElement($commentaire)) {
  127.             // set the owning side to null (unless already changed)
  128.             if ($commentaire->getProjet() === $this) {
  129.                 $commentaire->setProjet(null);
  130.             }
  131.         }
  132.         return $this;
  133.     }
  134.     public function getFinishedAt(): ?\DateTimeImmutable
  135.     {
  136.         return $this->finishedAt;
  137.     }
  138.     public function setFinishedAt(?\DateTimeImmutable $finishedAt): self
  139.     {
  140.         $this->finishedAt $finishedAt;
  141.         return $this;
  142.     }
  143.     public function getGithub(): ?string
  144.     {
  145.         return $this->github;
  146.     }
  147.     public function setGithub(?string $github): self
  148.     {
  149.         $this->github $github;
  150.         return $this;
  151.     }
  152. }