<?php
namespace App\Entity;
use App\Enum\User\Role;
use App\Enum\User\Status;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/
class User
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $username;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $name;
/**
* @ORM\Column(type="string", length=255)
*/
private $email;
/**
* @Assert\Regex("/^\+7[0-9]{10}$/",
* message = "Введите телефон {{ value }} в федеральном формате (+79123456789)"
* )
* @ORM\Column(type="string", length=20, nullable=true)
*/
private $phone;
/**
* @ORM\Column(type="string", length=32)
*/
private $password;
private $repassword;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="users")
*/
private $company;
/**
* @ORM\Column(type="enum", options={"values": "user_enum", "default": "new"})
*/
private $status = Status::STATUS_NEW;
/**
* @ORM\Column(type="enum", options={"values": "user_enum", "default": "ROLE_USER"})
*/
private $role = Role::ROLE_USER;
/**
* @ORM\ManyToOne(targetEntity=ClientLegal::class, inversedBy="users")
*/
private $clientLegal;
/**
* @ORM\ManyToMany(targetEntity=News::class, mappedBy="users")
*/
private $news;
/**
* @ORM\ManyToMany(targetEntity=News::class, mappedBy="usersReaded")
*/
private $newsReaded;
public function __construct()
{
$this->news = new ArrayCollection();
$this->newsReaded = new ArrayCollection();
}
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$callback = function (User $item, ExecutionContextInterface $context, $payload) {
$result = self::validate($item);
if (is_array($result)) {
foreach ($result as $error) {
$context->buildViolation($error)
->addViolation();
}
}
};
$metadata->addConstraint(new Callback($callback));
}
public static function validate(User $item)
{
$errors = [];
if (!preg_match("/^[a-zA-Z0-9]+$/", $item->getUsername())) {
$errors[] = "Логин может содержать только латинские буквы и цифры";
}
return $errors ?: true;
}
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;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(?string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password, $md5 = false): self
{
$this->password = !$md5 ? md5($password) : $password;
return $this;
}
public function getStatus()
{
return $this->status;
}
public function setStatus($status): self
{
$this->status = $status;
return $this;
}
public function getRole()
{
return $this->role;
}
public function isAdminRole(): bool
{
return $this->getRole() == Role::ROLE_ADMIN;
}
public function setRole($role): self
{
$this->role = $role;
return $this;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
public function getRepassword(): ?string
{
return $this->repassword;
}
public function setRepassword(string $password): self
{
$this->repassword = md5($password);
return $this;
}
public function getClientLegal(): ?ClientLegal
{
return $this->clientLegal;
}
public function setClientLegal(?ClientLegal $clientLegal): self
{
$this->clientLegal = $clientLegal;
return $this;
}
/**
* @return Collection<int, News>
*/
public function getNews(): Collection
{
return $this->news;
}
public function addNews(News $news): self
{
if (!$this->news->contains($news)) {
$this->news[] = $news;
$news->addForUser($this);
}
return $this;
}
public function removeNews(News $news): self
{
if ($this->news->removeElement($news)) {
$news->removeForUser($this);
}
return $this;
}
/**
* @return Collection<int, News>
*/
public function getNewsReaded(): Collection
{
return $this->newsReaded;
}
public function addNewsReaded(News $newsReaded): self
{
if (!$this->newsReaded->contains($newsReaded)) {
$this->newsReaded[] = $newsReaded;
$newsReaded->addUsersNotified($this);
}
return $this;
}
public function removeNewsReaded(News $newsReaded): self
{
if ($this->newsReaded->removeElement($newsReaded)) {
$newsReaded->removeUsersReaded($this);
}
return $this;
}
}