src/Entity/Partie.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\PartieRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. #[ORM\Entity(repositoryClass: PartieRepository::class)]
  8. class Partie
  9. {
  10. #[ORM\Id]
  11. #[ORM\GeneratedValue]
  12. #[ORM\Column(type: "integer")]
  13. public int $id;
  14. public ?Score $scoreMax = null;
  15. #[ORM\OneToMany(mappedBy: "partie", targetEntity: Score::class, cascade: ['persist'])]
  16. #[ORM\OrderBy(['score' => 'DESC'])]
  17. public Collection|array $scores;
  18. public function __construct(
  19. #[ORM\Column(type:"date")]
  20. public ?\DateTime $date = null,
  21. #[ORM\ManyToOne(targetEntity: Jeu::class, inversedBy: "parties")]
  22. #[ORM\JoinColumn(nullable: false)]
  23. public ?Jeu $jeu = null,
  24. ) {
  25. $this->scores = new ArrayCollection();
  26. if ($date == null) {
  27. $this->date = new \DateTime();
  28. }
  29. }
  30. public function getResultat()
  31. {
  32. return implode("\n",
  33. array_map(
  34. fn (Score $s) => $s->joueur .':'. $s->score,
  35. $this->scores->toArray()
  36. )
  37. );
  38. }
  39. public function getScoreMax(): Score
  40. {
  41. ///** @var ?Score $max */
  42. //$max = null;
  43. //return array_reduce(
  44. // $this->scores->toArray(),
  45. // fn($m, Score $s) => max($m, $s->score??0),
  46. // 0
  47. //);
  48. if (!$this->scoreMax) {
  49. /** @var ?Score $max */
  50. $max = null;
  51. /** @var Score $score */
  52. foreach ($this->scores as $score) {
  53. if ($max == null || $score->score > $max->score) {
  54. $max = $score;
  55. }
  56. }
  57. $this->scoreMax = $max;
  58. }
  59. return $this->scoreMax;
  60. }
  61. public function joueurs(): int
  62. {
  63. return $this->scores->count();
  64. }
  65. public function label(): ?string
  66. {
  67. if (!$this->getScoreMax()) return null;
  68. setlocale(LC_ALL, "fr_FR.UTF-8");
  69. $date = strftime("%e %b %Y", $this->date->getTimestamp());
  70. return
  71. $this->scoreMax->score
  72. . " ("
  73. .$this->scoreMax->joueur
  74. . " - "
  75. .$date
  76. . ")"
  77. ;
  78. }
  79. }