memory

Play Memory
Log | Files | Refs

Game.php (2194B)


      1 <?php
      2 
      3 namespace App\Entity;
      4 
      5 use Doctrine\Common\Collections\ArrayCollection;
      6 use Doctrine\Common\Collections\Collection;
      7 use Doctrine\ORM\Mapping\Column;
      8 use Doctrine\ORM\Mapping\GeneratedValue;
      9 use Doctrine\ORM\Mapping\Id;
     10 use Doctrine\ORM\Mapping\Entity;
     11 use Doctrine\ORM\Mapping\ManyToMany;
     12 use ApiPlatform\Core\Annotation\ApiResource;
     13 use phpDocumentor\Reflection\Types\True_;
     14 
     15 
     16 #[Entity]
     17 #[ApiResource]
     18 class Game implements \JsonSerializable {
     19 
     20     #[Column(unique:true)] #[Id] #[GeneratedValue] private int $id;
     21     #[Column(name:'date')] public \DateTime $dateTime;
     22     #[Column] public float $score;
     23 
     24     // optional string for api used in this game
     25     #[Column(nullable:true)] public string $api = '';
     26 
     27     //optional column for color of closed cards in this game
     28     #[Column(nullable:true)] public string $color_closed = '';
     29 
     30     //optional column for color of found cards in this game
     31     #[Column(nullable:true)] public string $color_found = '';
     32 
     33     // Zo doe je dus unidirectionele OneToMany in doctrine...
     34     // zie
     35     // https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-unidirectional-with-join-table
     36 
     37     #[ManyToMany(targetEntity: Player::class, mappedBy: "games")]
     38     private Collection $player;
     39 
     40     public function __construct(Player $player, mixed $params)
     41     {
     42         $this->player = new ArrayCollection();
     43         $this->player[] = $player;
     44         $this->dateTime = new \DateTime('now');
     45         $this->score = $params['score'];
     46         $this->api = $params['api'] ?? '';
     47         $this->color_found = $params['color_found'] ?? '';
     48         $this->color_closed = $params['color_closed'] ?? '';
     49     }
     50 
     51     public function getId(): int { return $this->id; }
     52 
     53     public function getDayFromDate():string {
     54         return $this->dateTime->format('Y-m-d');
     55     }
     56 
     57     public function jsonSerialize():mixed {
     58         return array(
     59             'date' => $this->dateTime,
     60             'day' => $this->getDayFromDate(),
     61             'score' => $this->score,
     62             'api' => $this->api,
     63             'color_closed' => $this->color_closed,
     64             'color_found' => $this->color_found
     65         );
     66     }
     67 }