memory

Play Memory
Log | Files | Refs

Player.php (3183B)


      1 <?php
      2 
      3 namespace App\Entity;
      4 
      5 use ApiPlatform\Core\Annotation\ApiResource;
      6 use Doctrine\Common\Collections\ArrayCollection;
      7 use Doctrine\Common\Collections\Collection;
      8 use Doctrine\ORM\Mapping\Column;
      9 use Doctrine\ORM\Mapping\Entity;
     10 use Doctrine\ORM\Mapping\GeneratedValue;
     11 use Doctrine\ORM\Mapping\Id;
     12 use Doctrine\ORM\Mapping\OneToMany;
     13 use Doctrine\ORM\Mapping\ManyToMany;
     14 use Doctrine\ORM\Mapping\JoinColumn;
     15 use Doctrine\ORM\Mapping\InverseJoinColumn;
     16 use Doctrine\ORM\Mapping\JoinTable;
     17 use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
     18 use Symfony\Component\Security\Core\User\UserInterface;
     19 
     20 #[Entity]
     21 #[ApiResource]
     22 class Player implements \JsonSerializable, UserInterface, PasswordAuthenticatedUserInterface {
     23     #[Column(unique:true)] #[Id] #[GeneratedValue] public int $id;
     24     #[Column(name:'name')] public string $username;
     25     #[Column] public string $email;
     26     #[Column] public string $password_hash;
     27     #[Column(nullable:true)] public string $preferred_api = '';
     28     #[Column(nullable:true)] public string $preferred_color_closed = '';
     29     #[Column(nullable:true)] public string $preferred_color_found = '';
     30 
     31     #[ManyToMany(targetEntity:Game::class, cascade: ["persist"])]
     32     #[JoinTable(name:"player_games")]
     33     #[JoinColumn(name: "player_id", referencedColumnName: "id")]
     34     #[InverseJoinColumn(name: "game_id", referencedColumnName: "id", unique:true)]
     35     private $games;
     36 
     37     public function __construct(string $username, string $email, string $password_hash)
     38     {
     39         $this->username = $username;
     40         $this->email = $email;
     41         $this->password_hash = $password_hash;
     42         $this->games = new ArrayCollection();
     43     }
     44 
     45     public function addGame(Game $game) {
     46         $this->games[] = $game;
     47     }
     48 
     49     public function getPreferences():array {
     50         return [
     51             'preferred_api' => $this->preferred_api,
     52             'color_closed' => $this->preferred_color_closed,
     53             'color_found' => $this->preferred_color_found
     54         ];
     55     }
     56 
     57     public function setPreferences(array $params) {
     58         $this->preferred_api = $params['api'];
     59         $this->preferred_color_found = $params['color_found'];
     60         $this->preferred_color_closed = $params['color_closed'];
     61     }
     62 
     63     public function getGames():Collection {
     64         $t = new ArrayCollection();
     65         foreach($this->games as $g) {
     66             $t->add($g->jsonSerialize());
     67         }
     68         return $t;
     69     }
     70 
     71     public function jsonSerialize():mixed {
     72         $games = $this->getGames()->toArray();
     73         return array(
     74             'id' => $this->id,
     75             'name' => $this->username,
     76             'email' => $this->email,
     77             'games' => $games
     78         );
     79     }
     80 
     81     public function getPassword(): ?string
     82     {
     83         return $this->password_hash;
     84     }
     85 
     86     public function getRoles(): array
     87     {
     88         $roles = ['ROLE_USER'];
     89         if ($this->username=='Henk') $roles[] = 'ROLE_ADMIN';
     90         return $roles;
     91     }
     92 
     93     public function eraseCredentials()
     94     {
     95         // TODO: Implement eraseCredentials() method.
     96     }
     97 
     98     public function getUserIdentifier(): string
     99     {
    100         return $this->username;
    101     }
    102 }