<?php
namespace App\Entity;
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;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User implements UserInterface, \Serializable, PasswordAuthenticatedUserInterface
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=50, nullable=true)
*/
private $firstname;
/**
* @ORM\Column(type="string", length=100, nullable=true)
*/
private $lastname;
/**
* @ORM\Column(type="string", length=190, unique=true)
* @Assert\Email(message="Dit is geen geldig emailadres")
*/
private $email;
/**
* @ORM\Column(type="string", length=100)
*/
private $password;
/**
* @ORM\Column(type="boolean", options={"default":"0"}, nullable=true)
*/
private $active;
/**
* @ORM\Column(type="text")
*/
private $roles;
public function getId()
{
return $this->id;
}
public function getName()
{
return trim($this->firstname . ' ' . $this->lastname);
}
public function getFirstname(): ?string
{
return $this->firstname;
}
public function setFirstname(?string $firstname): self
{
$this->firstname = $firstname;
return $this;
}
public function getLastname(): ?string
{
return $this->lastname;
}
public function setLastname(?string $lastname): self
{
$this->lastname = $lastname;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* String representation of object
* @link http://php.net/manual/en/serializable.serialize.php
* @return string the string representation of the object or null
* @since 5.1.0
*/
public function serialize()
{
return serialize(array(
$this->id,
$this->email,
$this->password,
$this->firstname,
$this->lastname,
$this->active,
$this->roles
));
}
/**
* Constructs the object
* @link http://php.net/manual/en/serializable.unserialize.php
* @param string $serialized <p>
* The string representation of the object.
* </p>
* @return void
* @since 5.1.0
*/
public function unserialize($serialized)
{
[
$this->id,
$this->email,
$this->password,
$this->firstname,
$this->lastname,
$this->active,
$this->roles] = unserialize($serialized);
}
/**
* Returns the roles granted to the user.
*
* <code>
* public function getRoles()
* {
* return array('ROLE_USER');
* }
* </code>
*
* Alternatively, the roles might be stored on a ``roles`` property,
* and populated in any number of different ways when the user object
* is created.
*
* @return array (Role|string)[] The user roles
*/
public function getRoles()
{
return (array)json_decode($this->roles, true);
}
public function setRoles(array $roles): self
{
$this->roles = json_encode($roles, JSON_PRETTY_PRINT);
return $this;
}
/**
* Set password
*
* @param string $password
*/
public function setPassword($password)
{
$this->password = $password;
}
/**
* Returns the password used to authenticate the user.
*
* This should be the encoded password. On authentication, a plain-text
* password will be salted, encoded, and then compared to this value.
*
* @return string The password
*/
public function getPassword(): ?string
{
return $this->password;
}
/**
* Returns the salt that was originally used to encode the password.
*
* This can return null if the password was not encoded using a salt.
*
* @return string|null The salt
*/
public function getSalt()
{
return null;
}
/**
* Returns the username used to authenticate the user.
*
* @return string The username
*/
public function getUsername()
{
return $this->getEmail();
}
public function getUserIdentifier(): string
{
return $this->getEmail();
}
/**
* Removes sensitive data from the user.
*
* This is important if, at any given point, sensitive information like
* the plain-text password is stored on this object.
*/
public function eraseCredentials()
{
}
/**
* Remove password from returnvalue
* @return array
*/
public function forVue()
{
$driver = $this->isDriver();
$picker = $this->isPicker();
return [
'id' => (int)$this->getId(),
'admin' => !$driver && !$picker && $this->isAdmin(),
'driver' => !$picker && $driver,
'picker' => $picker,
'role' => $this->getRoleName(),
'firstname' => $this->getFirstname(),
'lastname' => $this->getLastname(),
'email' => $this->getEmail(),
'active' => $this->getActive(),
];
}
public function getRoleName()
{
if ($this->isPicker()) {
return 'picker';
}
if ($this->isDriver()) {
return 'driver';
}
if ($this->isAdmin()) {
return 'admin';
}
return 'user';
}
public function isAdmin()
{
return \in_array('ROLE_ADMIN', $this->getRoles(), true);
}
public function isDriver()
{
return \in_array('ROLE_DRIVER', $this->getRoles(), true);
}
public function isPicker()
{
return \in_array('ROLE_PICKER', $this->getRoles(), true);
}
public function getActive(): ?bool
{
return $this->active;
}
public function setActive(?bool $active): self
{
$this->active = $active;
return $this;
}
}