<?php
namespace App\Entity;
use App\Entity\Traits\ToArray;
use App\Enum\User\Role;
use App\Enum\User\Status;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
use ToArray;
const USER_FIELD_NAME = "user";
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=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)
*/
private $firstName;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $lastName;
/**
* @var string|null
*/
private $oldPassword;
/**
* @var string|null
*/
private $newPassword;
/**
* @ORM\OneToOne(targetEntity=UserSettings::class, cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=false)
*/
private $settings;
/**
* @ORM\Column(type="string", length=30, nullable=true)
*/
private $telegramUserId;
/**
* @ORM\Column(type="string", length=30, nullable=true)
*/
private $telegramPhone;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $sortId;
/**
* @ORM\Column(type="enum", options={"values"="user_enum"})
*/
private $status = Status::STATUS_ACTIVE;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $createdAt;
/**
* @ORM\OneToMany(targetEntity=Device::class, mappedBy="user")
*/
private $devices;
/**
* @ORM\OneToMany(targetEntity=Member::class, mappedBy="user")
*/
private $members;
/**
* @ORM\OneToMany(targetEntity=Contact::class, mappedBy="owner", orphanRemoval=true)
*/
private $contacts;
/**
* @ORM\Column(type="string", length=50, nullable=true)
*/
private $phone;
public function __construct()
{
$this->createdAt = new \DateTime();
$this->devices = new ArrayCollection();
$this->members = new ArrayCollection();
$this->contacts = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string) $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 getRolesData(): array
{
$rolesData = [];
foreach ($this->getRoles() as $role) {
$rolesData[] = [
'value' => $role,
'text' => Role::getText($role),
];
}
return $rolesData;
}
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
public function isAdmin(): bool
{
return in_array(Role::ROLE_ADMIN, $this->getRoles(), true);
}
public function isModerator(): bool
{
return in_array(Role::ROLE_MODERATOR, $this->getRoles(), true);
}
public function isDeveloper(): bool
{
return in_array(Role::ROLE_DEVELOPER, $this->getRoles(), true);
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(?string $password): self
{
$this->password = $password;
return $this;
}
public function getOldPassword(): ?string
{
return $this->oldPassword;
}
public function setOldPassword(string $oldPassword): self
{
$this->oldPassword = $oldPassword;
return $this;
}
public function getNewPassword(): ?string
{
return $this->newPassword;
}
public function setNewPassword(string $newPassword): self
{
$this->newPassword = $newPassword;
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 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 getName(): string
{
return $this->firstName . ' ' . $this->lastName;
}
public function getSettings(): ?UserSettings
{
return $this->settings;
}
public function setSettings(UserSettings $settings): self
{
$this->settings = $settings;
return $this;
}
public function getTelegramUserId(): ?string
{
return $this->telegramUserId;
}
public function setTelegramUserId(?string $telegramUserId): self
{
$this->telegramUserId = $telegramUserId;
return $this;
}
public function getTelegramPhone(): ?string
{
return $this->telegramPhone;
}
public function setTelegramPhone(?string $telegramPhone): self
{
$this->telegramPhone = $telegramPhone;
return $this;
}
public function getSortId(): ?int
{
return $this->sortId;
}
public function setSortId(?int $sortId): self
{
$this->sortId = $sortId;
return $this;
}
public function getStatus()
{
return $this->status;
}
public function setStatus($status): self
{
$this->status = $status;
return $this;
}
public function getStatusText(): ?string
{
return Status::getText($this->getStatus());
}
public function getStatusCssClass($prefix = null): ?string
{
return Status::getCssClass($this->getStatus(), $prefix);
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
/**
* @return Collection<int, Device>
*/
public function getDevices(): Collection
{
return $this->devices;
}
public function addDevice(Device $device): self
{
if (!$this->devices->contains($device)) {
$this->devices[] = $device;
$device->setUser($this);
}
return $this;
}
public function removeDevice(Device $device): self
{
if ($this->devices->removeElement($device)) {
// set the owning side to null (unless already changed)
if ($device->getUser() === $this) {
$device->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, Member>
*/
public function getMembers(): Collection
{
return $this->members;
}
public function addMember(Member $member): self
{
if (!$this->members->contains($member)) {
$this->members[] = $member;
$member->setUser($this);
}
return $this;
}
public function removeMember(Member $member): self
{
if ($this->members->removeElement($member)) {
// set the owning side to null (unless already changed)
if ($member->getUser() === $this) {
$member->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, Contact>
*/
public function getContacts(): Collection
{
return $this->contacts;
}
public function addContact(Contact $contact): self
{
if (!$this->contacts->contains($contact)) {
$this->contacts[] = $contact;
$contact->setOwner($this);
}
return $this;
}
public function removeContact(Contact $contact): self
{
if ($this->contacts->removeElement($contact)) {
// set the owning side to null (unless already changed)
if ($contact->getOwner() === $this) {
$contact->setOwner(null);
}
}
return $this;
}
public function getPhone(): ?string
{
return $this->phone;
}
public function setPhone(?string $phone): self
{
$this->phone = $phone;
return $this;
}
}