<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Table(name="users")
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User implements UserInterface, \JsonSerializable
{
const ROLE_ADMIN = "ROLE_ADMIN";
const ROLE_COMPANY = "ROLE_COMPANY";
const ROLE_CANDIDATE = "ROLE_CANDIDATE";
const ROLES = [
self::ROLE_ADMIN => 'Admin',
self::ROLE_COMPANY => 'Company',
self::ROLE_CANDIDATE => 'Candidate'
];
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* One User has One Company.
* @ORM\OneToOne(targetEntity="Company", mappedBy="user")
*/
private $company;
/**
* @ORM\OneToOne(targetEntity="Candidate", mappedBy="user")
*/
private $candidate;
/**
*
* @ORM\Column(type="string", length=180)
* @Assert\Email()
* @Assert\NotBlank()
*/
private $email;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string", nullable=true)
*/
private $password;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $accessTokenEconomedia;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $refreshTokenEconomedia;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $idEconomedia;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $personaldataAgreements;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $economediaAgreements;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $partnersAgreements;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $createdAt;
/**
* @ORM\Column(type="boolean", options={"default": "0"})
*/
private $fromNewsletter = FALSE;
private $newPassword;
public function getId(): ?int
{
return $this->id;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
public function removeCompany(Company $company): self
{
if ($this->company == $company) {
$this->company = null;
}
return $this;
}
public function getCandidate(): ?Candidate
{
return $this->candidate;
}
public function setCandidate(?Candidate $candidate): self
{
$this->candidate = $candidate;
return $this;
}
public function removeCandidate(Candidate $candidate): self
{
if ($this->candidate == $candidate) {
$this->candidate = null;
}
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getAccessTokenEconomedia(): ?string
{
return $this->accessTokenEconomedia;
}
public function setAccessTokenEconomedia(string $accessTokenEconomedia): self
{
$this->accessTokenEconomedia = $accessTokenEconomedia;
return $this;
}
public function getRefreshTokenEconomedia(): ?string
{
return $this->refreshTokenEconomedia;
}
public function setRefreshTokenEconomedia(string $refreshTokenEconomedia): self
{
$this->refreshTokenEconomedia = $refreshTokenEconomedia;
return $this;
}
public function getIdEconomedia(): ?int
{
return $this->idEconomedia;
}
public function setIdEconomedia(int $idEconomedia): self
{
$this->idEconomedia = $idEconomedia;
return $this;
}
public function getPersonaldataAgreements(): ?int
{
return $this->personaldataAgreements;
}
public function setPersonaldataAgreements(int $personaldataAgreements): self
{
$this->personaldataAgreements = $personaldataAgreements;
return $this;
}
public function getEconomediaAgreements(): ?int
{
return $this->economediaAgreements;
}
public function setEconomediaAgreements(int $economediaAgreements): self
{
$this->economediaAgreements = $economediaAgreements;
return $this;
}
public function getPartnersAgreements(): ?int
{
return $this->partnersAgreements;
}
public function setPartnersAgreements(int $partnersAgreements): self
{
$this->partnersAgreements = $partnersAgreements;
return $this;
}
public function getCreatedAt(): ?\DateTime {
return $this->createdAt;
}
public function setCreatedAt(\DateTime $createdAt): self {
$this->createdAt = $createdAt;
return $this;
}
/**
* @return bool
*/
public function getFromNewsletter() {
return $this->fromNewsletter;
}
/**
* @param bool $fromNewsletter
*
* @return self
*/
public function setFromNewsletter(bool $fromNewsletter): self {
$this->fromNewsletter = $fromNewsletter;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string) $this->email;
}
/**
* @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 addRole($role): self
{
if(!in_array($role, $this->getRoles())){
$this->roles[] = $role;
}
return $this;
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
public function removeRole($role): self
{
if (($key = array_search($role, $this->getRoles())) !== false) {
array_splice($this->roles, $key, 1);
}
return $this;
}
/**
* @see UserInterface
*/
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(?string $password): self
{
if (!is_null($password) && $password != '') {
$this->password = $password;
}
return $this;
}
public function getNewPassword(): ?string
{
return $this->newPassword;
}
public function setNewPassword(?string $newPassword): self
{
if (!is_null($newPassword) && $newPassword != '') {
$this->newPassword = $newPassword;
}
return $this;
}
/**
* @see UserInterface
* @return ?string
*/
public function getSalt()
{
// not needed when using the "bcrypt" algorithm in security.yaml
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function __toString() {
return $this->getEmail();
}
/**
* Specify data which should be serialized to JSON
*
* @link https://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
public function jsonSerialize() {
$company = $this->getCompany();
return [
'id' => $this->getId(),
'email' => $this->getEmail(),
'roles' => $this->getRoles(),
'company' => $company,
'subscription' => $company ? $company->getOneActiveSubscription() : null,
'candidate' => $this->getCandidate(),
'economediaId' => $this->getIdEconomedia(),
'createdAt' => $this->getCreatedAt() ? $this->getCreatedAt()->format('Y-m-d H:i:s') : null,
];
}
}