src/Entity/User.php line 16
<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation\Ignore;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[UniqueEntity(fields: ['email'], message: 'Пользователь с таким E-mail адресом уже зарегистрирован')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
public const ROLE_NAMES = [
'ROLE_ROOT' => 'Руководитель',
'ROLE_ADMIN' => 'Администратор',
'ROLE_USER' => 'Пользователь',
'ROLE_GUEST' => 'Гость'
];
private ?string $role = '';
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 180, unique: true)]
private ?string $email = null;
#[ORM\Column]
private array $roles = [];
/**
* @var string The hashed password
*/
#[ORM\Column]
private ?string $password = null;
#[ORM\Column(nullable: true)]
private ?string $vkToken = null;
#[ORM\Column(nullable: true)]
private ?string $vkId = null;
#[ORM\Column(type: 'boolean')]
private $isVerified = false;
#[ORM\Column(options: ['default' => 0])]
private ?float $balance = null;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: BillingHistory::class)]
#[Ignore]
private Collection $billingHistories;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: BillingTransaction::class)]
#[Ignore]
private Collection $billingTransactions;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: UserProject::class)]
#[Ignore]
private Collection $userProjects;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: UserPlatform::class)]
#[Ignore]
private Collection $userPlatforms;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: UserOrd::class)]
#[Ignore]
private Collection $userOrd;
public function __construct()
{
$this->billingHistories = new ArrayCollection();
$this->billingTransactions = new ArrayCollection();
$this->userProjects = new ArrayCollection();
$this->userPlatforms = new ArrayCollection();
$this->userOrd = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): 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 setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
public function getRolesName(): string
{
$rolesName = [];
foreach ($this->getRoles() as $role) $rolesName[] = self::ROLE_NAMES[$role];
return implode(', ', $rolesName);
}
/**
* @param string $roleName
* @return bool
*/
public function hasRole(string $roleName): bool
{
if (in_array($roleName, $this->roles)) return true;
return false;
}
public function getRole(): bool
{
return $this->role;
}
/**
* @return bool
*/
public function isAdmin(): bool
{
if (in_array('ROLE_ADMIN', $this->roles)) return true;
if (in_array('ROLE_ROOT', $this->roles)) return true;
return false;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
public function isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
public function getBalance(): ?float
{
return $this->balance;
}
public function setBalance(float $balance): self
{
$this->balance = $balance;
return $this;
}
/**
* @return Collection<int, BillingHistory>
*/
public function getBillingHistories(): Collection
{
return $this->billingHistories;
}
public function addBillingHistory(BillingHistory $billingHistory): self
{
if (!$this->billingHistories->contains($billingHistory)) {
$this->billingHistories->add($billingHistory);
$billingHistory->setUser($this);
}
return $this;
}
public function removeBillingHistory(BillingHistory $billingHistory): self
{
if ($this->billingHistories->removeElement($billingHistory)) {
// set the owning side to null (unless already changed)
if ($billingHistory->getUser() === $this) {
$billingHistory->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, BillingTransaction>
*/
public function getBillingTransactions(): Collection
{
return $this->billingTransactions;
}
public function addBillingTransaction(BillingTransaction $billingTransaction): self
{
if (!$this->billingTransactions->contains($billingTransaction)) {
$this->billingTransactions->add($billingTransaction);
$billingTransaction->setUser($this);
}
return $this;
}
public function removeBillingTransaction(BillingTransaction $billingTransaction): self
{
if ($this->billingTransactions->removeElement($billingTransaction)) {
// set the owning side to null (unless already changed)
if ($billingTransaction->getUser() === $this) {
$billingTransaction->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, UserProject>
*/
public function getUserProjects(): Collection
{
return $this->userProjects;
}
public function addUserProject(UserProject $userProject): self
{
if (!$this->userProjects->contains($userProject)) {
$this->userProjects->add($userProject);
$userProject->setUser($this);
}
return $this;
}
public function removeUserProject(UserProject $userProject): self
{
if ($this->userProjects->removeElement($userProject)) {
// set the owning side to null (unless already changed)
if ($userProject->getUser() === $this) {
$userProject->setUser(null);
}
}
return $this;
}
#[Ignore]
public function getMainUserProject(): UserProject
{
foreach ($this->userProjects as $project) {
if ($project->isMain()) {
return $project;
}
}
}
/**
* @return string|null
*/
public function getVkToken(): ?string
{
return $this->vkToken;
}
/**
* @param string|null $vkToken
*/
public function setVkToken(?string $vkToken): void
{
$this->vkToken = $vkToken;
}
/**
* @return string|null
*/
public function getVkId(): ?string
{
return $this->vkId;
}
/**
* @param string|null $vkId
*/
public function setVkId(?string $vkId): void
{
$this->vkId = $vkId;
}
/**
* @return Collection
*/
public function getUserPlatforms(): Collection
{
return $this->userPlatforms;
}
/**
* @param Collection $userPlatforms
*/
public function setUserPlatforms(Collection $userPlatforms): void
{
$this->userPlatforms = $userPlatforms;
}
public function addUserPlatform(UserPlatform $userPlatform): self
{
if (!$this->userPlatforms->contains($userPlatform)) {
$this->userPlatforms->add($userPlatform);
$userPlatform->setUser($this);
}
return $this;
}
public function removeUserPlatform(UserPlatform $userPlatform): self
{
if ($this->userPlatforms->removeElement($userPlatform)) {
if ($userPlatform->getUser() === $this) {
$userPlatform->setUser(null);
}
}
return $this;
}
/**
* @return Collection
*/
public function getUserOrd(): Collection
{
return $this->userOrd;
}
/**
* @param Collection $userOrd
*/
public function setUserOrd(Collection $userOrd): void
{
$this->userOrd = $userOrd;
}
public function addUserOrd(UserOrd $userOrd): self
{
if (!$this->userOrd->contains($userOrd)) {
$this->userOrd->add($userOrd);
$userOrd->setUser($this);
}
return $this;
}
public function removeUserOrd(UserOrd $userOrd): self
{
if ($this->userOrd->removeElement($userOrd)) {
if ($userOrd->getUser() === $this) {
$userOrd->setUser(null);
}
}
return $this;
}
}