src/Entity/User.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Enum\User\Role;
  4. use App\Enum\User\Status;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. use Symfony\Component\Validator\Constraints\Callback;
  10. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  11. use Symfony\Component\Validator\Mapping\ClassMetadata;
  12. /**
  13.  * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
  14.  */
  15. class User
  16. {
  17.     /**
  18.      * @ORM\Id()
  19.      * @ORM\GeneratedValue()
  20.      * @ORM\Column(type="integer")
  21.      */
  22.     private $id;
  23.     /**
  24.      * @ORM\Column(type="string", length=255)
  25.      */
  26.     private $username;
  27.     /**
  28.      * @ORM\Column(type="string", length=255, nullable=true)
  29.      */
  30.     private $name;
  31.     /**
  32.      * @ORM\Column(type="string", length=255)
  33.      */
  34.     private $email;
  35.     /**
  36.      * @Assert\Regex("/^\+7[0-9]{10}$/",
  37.      *     message = "Введите телефон {{ value }} в федеральном формате (+79123456789)"
  38.      * )
  39.      * @ORM\Column(type="string", length=20, nullable=true)
  40.      */
  41.     private $phone;
  42.     /**
  43.      * @ORM\Column(type="string", length=32)
  44.      */
  45.     private $password;
  46.     private $repassword;
  47.     /**
  48.      * @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="users")
  49.      */
  50.     private $company;
  51.     /**
  52.      * @ORM\Column(type="enum", options={"values": "user_enum", "default": "new"})
  53.      */
  54.     private $status Status::STATUS_NEW;
  55.     /**
  56.      * @ORM\Column(type="enum", options={"values": "user_enum", "default": "ROLE_USER"})
  57.      */
  58.     private $role Role::ROLE_USER;
  59.     /**
  60.      * @ORM\ManyToOne(targetEntity=ClientLegal::class, inversedBy="users")
  61.      */
  62.     private $clientLegal;
  63.     /**
  64.      * @ORM\ManyToMany(targetEntity=News::class, mappedBy="users")
  65.      */
  66.     private $news;
  67.     /**
  68.      * @ORM\ManyToMany(targetEntity=News::class, mappedBy="usersReaded")
  69.      */
  70.     private $newsReaded;
  71.     public function __construct()
  72.     {
  73.         $this->news = new ArrayCollection();
  74.         $this->newsReaded = new ArrayCollection();
  75.     }
  76.     public static function loadValidatorMetadata(ClassMetadata $metadata)
  77.     {
  78.         $callback = function (User $itemExecutionContextInterface $context$payload) {
  79.             $result self::validate($item);
  80.             if (is_array($result)) {
  81.                 foreach ($result as $error) {
  82.                     $context->buildViolation($error)
  83.                         ->addViolation();
  84.                 }
  85.             }
  86.         };
  87.         $metadata->addConstraint(new Callback($callback));
  88.     }
  89.     public static function validate(User $item)
  90.     {
  91.         $errors = [];
  92.         if (!preg_match("/^[a-zA-Z0-9]+$/"$item->getUsername())) {
  93.             $errors[] = "Логин может содержать только латинские буквы и цифры";
  94.         }
  95.         return $errors ?: true;
  96.     }
  97.     public function getId(): ?int
  98.     {
  99.         return $this->id;
  100.     }
  101.     public function getUsername(): ?string
  102.     {
  103.         return $this->username;
  104.     }
  105.     public function setUsername(string $username): self
  106.     {
  107.         $this->username $username;
  108.         return $this;
  109.     }
  110.     public function getName(): ?string
  111.     {
  112.         return $this->name;
  113.     }
  114.     public function setName(?string $name): self
  115.     {
  116.         $this->name $name;
  117.         return $this;
  118.     }
  119.     public function getEmail(): ?string
  120.     {
  121.         return $this->email;
  122.     }
  123.     public function setEmail(string $email): self
  124.     {
  125.         $this->email $email;
  126.         return $this;
  127.     }
  128.     public function getPhone(): ?string
  129.     {
  130.         return $this->phone;
  131.     }
  132.     public function setPhone(?string $phone): self
  133.     {
  134.         $this->phone $phone;
  135.         return $this;
  136.     }
  137.     public function getPassword(): ?string
  138.     {
  139.         return $this->password;
  140.     }
  141.     public function setPassword(string $password$md5 false): self
  142.     {
  143.         $this->password = !$md5 md5($password) : $password;
  144.         return $this;
  145.     }
  146.     public function getStatus()
  147.     {
  148.         return $this->status;
  149.     }
  150.     public function setStatus($status): self
  151.     {
  152.         $this->status $status;
  153.         return $this;
  154.     }
  155.     public function getRole()
  156.     {
  157.         return $this->role;
  158.     }
  159.     public function isAdminRole(): bool
  160.     {
  161.         return $this->getRole() == Role::ROLE_ADMIN;
  162.     }
  163.     public function setRole($role): self
  164.     {
  165.         $this->role $role;
  166.         return $this;
  167.     }
  168.     public function getCompany(): ?Company
  169.     {
  170.         return $this->company;
  171.     }
  172.     public function setCompany(?Company $company): self
  173.     {
  174.         $this->company $company;
  175.         return $this;
  176.     }
  177.     public function getRepassword(): ?string
  178.     {
  179.         return $this->repassword;
  180.     }
  181.     public function setRepassword(string $password): self
  182.     {
  183.         $this->repassword md5($password);
  184.         return $this;
  185.     }
  186.     public function getClientLegal(): ?ClientLegal
  187.     {
  188.         return $this->clientLegal;
  189.     }
  190.     public function setClientLegal(?ClientLegal $clientLegal): self
  191.     {
  192.         $this->clientLegal $clientLegal;
  193.         return $this;
  194.     }
  195.     /**
  196.      * @return Collection<int, News>
  197.      */
  198.     public function getNews(): Collection
  199.     {
  200.         return $this->news;
  201.     }
  202.     public function addNews(News $news): self
  203.     {
  204.         if (!$this->news->contains($news)) {
  205.             $this->news[] = $news;
  206.             $news->addForUser($this);
  207.         }
  208.         return $this;
  209.     }
  210.     public function removeNews(News $news): self
  211.     {
  212.         if ($this->news->removeElement($news)) {
  213.             $news->removeForUser($this);
  214.         }
  215.         return $this;
  216.     }
  217.     /**
  218.      * @return Collection<int, News>
  219.      */
  220.     public function getNewsReaded(): Collection
  221.     {
  222.         return $this->newsReaded;
  223.     }
  224.     public function addNewsReaded(News $newsReaded): self
  225.     {
  226.         if (!$this->newsReaded->contains($newsReaded)) {
  227.             $this->newsReaded[] = $newsReaded;
  228.             $newsReaded->addUsersNotified($this);
  229.         }
  230.         return $this;
  231.     }
  232.     public function removeNewsReaded(News $newsReaded): self
  233.     {
  234.         if ($this->newsReaded->removeElement($newsReaded)) {
  235.             $newsReaded->removeUsersReaded($this);
  236.         }
  237.         return $this;
  238.     }
  239. }