<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @UniqueEntity("username", message="Nom d'utilisateur déjà utilisé")
* @UniqueEntity("mail", message="Adresse email déjà utilisé")
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
const NEWSLETTER = [
'ENABLED' => 0,
'DISABLED' => -1
];
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=100, unique=true, nullable=true)
*/
private $username;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string", nullable=true)
*/
private $password;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $lastname;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $firstname;
/**
* @ORM\Column(type="string", length=255, unique=true)
*/
private $mail;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $address;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $phone;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $signupDate;
private $plainPassword;
private $role;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $image;
/**
* @ORM\Column(type="integer", nullable=true, options={"default" : 0})
*/
private $newsletter;
public function __construct()
{
$this->newsletter = self::NEWSLETTER['ENABLED'];
}
public function getId(): ?int
{
return $this->id;
}
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->username;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* Returning a salt is only needed, if you are not using a modern
* hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
*
* @see UserInterface
*/
public function getSalt(): ?string
{
return null;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function getLastname(): ?string
{
return $this->lastname;
}
public function setLastname(string $lastname): self
{
$this->lastname = $lastname;
return $this;
}
public function getFirstname(): ?string
{
return $this->firstname;
}
public function setFirstname(?string $firstname): self
{
$this->firstname = $firstname;
return $this;
}
public function getMail(): ?string
{
return $this->mail;
}
public function setMail(string $mail): self
{
$this->mail = $mail;
return $this;
}
public function getAddress(): ?string
{
return $this->address;
}
public function setAddress(?string $address): self
{
$this->address = $address;
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(?string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getSignupDate(): ?\DateTimeInterface
{
return $this->signupDate;
}
public function setSignupDate(\DateTimeInterface $signupDate): self
{
$this->signupDate = $signupDate;
return $this;
}
/**
* Get the value of plainPassword
*/
public function getPlainPassword()
{
return $this->plainPassword;
}
/**
* Set the value of plainPassword
*
* @return self
*/
public function setPlainPassword($plainPassword)
{
$this->plainPassword = $plainPassword;
return $this;
}
/**
* Get the value of role
*/
public function getRole()
{
return $this->role;
}
/**
* Set the value of role
*
* @return self
*/
public function setRole($role)
{
$this->role = $role;
return $this;
}
public function getRoleName(): ?string
{
if(in_array('ROLE_ADMIN', $this->getRoles())){
return 'Admin';
} else if(in_array('ROLE_CLIENT', $this->getRoles())){
return 'Client';
}
return null;
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage(?string $image): self
{
$this->image = $image;
return $this;
}
public function getFullName(){
return ($this->getFirstname() ? $this->getFirstname(): '') . ' ' . ($this->getLastname() ? $this->getLastname() : '');
}
public function getNewsletter(): ?int
{
return $this->newsletter;
}
public function setNewsletter(?int $newsletter): self
{
$this->newsletter = $newsletter;
return $this;
}
}