<?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\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=UserRepository::class)
* @ORM\Table(name="`user`")
* @UniqueEntity(fields={"email"}, message="There is already an account with this email")
*/
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=180, unique=true)
*/
#[Assert\Email]
private $email;
/**
* @ORM\Column(type="json")
*/
private $roles = [];
/**
* @var string The hashed password
* @ORM\Column(type="string")
*/
private $password;
/**
* @ORM\ManyToOne(targetEntity=Shop::class, inversedBy="users")
* @ORM\JoinColumn(nullable=true)
*/
private $shop;
/**
* @ORM\ManyToMany(targetEntity=Shop::class, inversedBy="users")
* #[JoinTable(name: 'user_shop')]
*/
private Collection $shops;
/**
* @ORM\Column(type="boolean")
*/
private $isVerified = false;
/**
* @ORM\OneToMany(targetEntity=Order::class, mappedBy="user")
*/
private $orders;
/**
* @ORM\OneToMany(targetEntity=Address::class, mappedBy="user")
*/
private $addresses;
/**
* @ORM\ManyToOne(targetEntity=Company::class, inversedBy="users")
*/
private $company;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $firstname;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $lastname;
/**
* @ORM\Column(type="string", length=128, nullable=true)
* @Assert\Length(min = 10, max = 20, minMessage = "min_lenght", maxMessage = "max_lenght")
*/
private $phone;
/**
* @ORM\ManyToOne(targetEntity=User::class, inversedBy="childrenUsers")
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity=User::class, mappedBy="parent")
*/
private $childrenUsers;
/**
* @ORM\OneToMany(targetEntity=UserSetting::class, mappedBy="user", cascade={"persist"})
*/
private $settings;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $active;
/**
* @ORM\OneToMany(targetEntity=Sav::class, mappedBy="user")
*/
private $savs;
public function __construct()
{
$this->orders = new ArrayCollection();
$this->addresses = new ArrayCollection();
$this->childrenUsers = new ArrayCollection();
$this->settings = new ArrayCollection();
$this->shops = new ArrayCollection();
$this->savs = new ArrayCollection();
}
public function __toString()
{
return $this->getEmail();
}
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;
}
/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
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 setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
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 getShop(): ?Shop
{
return $this->shop;
}
public function setShop(?Shop $shop): self
{
$this->shop = $shop;
return $this;
}
public function isVerified(): bool
{
return $this->isVerified;
}
public function setIsVerified(bool $isVerified): self
{
$this->isVerified = $isVerified;
return $this;
}
/**
* @return Collection<int, Order>
*/
public function getOrders(): Collection
{
return $this->orders;
}
public function addOrder(Order $order): self
{
if (!$this->orders->contains($order)) {
$this->orders[] = $order;
$order->setUser($this);
}
return $this;
}
public function removeOrder(Order $order): self
{
if ($this->orders->removeElement($order)) {
// set the owning side to null (unless already changed)
if ($order->getUser() === $this) {
$order->setUser(null);
}
}
return $this;
}
/**
* @return Collection<int, Address>
*/
public function getAddresses(): Collection
{
//
//
//echo "e";
//die();
if($this->getCompany()) {
// echo "1";
if($this->getCompany()->getAddress()) {
if (!$this->addresses->contains($this->getCompany()->getAddress())) {
$this->addresses[] = $this->getCompany()->getAddress();
}
}
}
return $this->addresses;
}
public function addAddress(Address $address): self
{
if (!$this->addresses->contains($address)) {
$this->addresses[] = $address;
$address->setUser($this);
}
return $this;
}
public function removeAddress(Address $address): self
{
if ($this->addresses->removeElement($address)) {
// set the owning side to null (unless already changed)
if ($address->getUser() === $this) {
$address->setUser(null);
}
}
return $this;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
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 getPhone(): ?string
{
return $this->phone;
}
public function setPhone(string $phone): self
{
$this->phone = $phone;
return $this;
}
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
/**
* @return Collection<int, self>
*/
public function getChildrenUsers(): Collection
{
return $this->childrenUsers;
}
public function addChildrenUser(self $childrenUser): self
{
if (!$this->childrenUsers->contains($childrenUser)) {
$this->childrenUsers[] = $childrenUser;
$childrenUser->setParent($this);
}
return $this;
}
public function removeChildrenUser(self $childrenUser): self
{
if ($this->childrenUsers->removeElement($childrenUser)) {
// set the owning side to null (unless already changed)
if ($childrenUser->getParent() === $this) {
$childrenUser->setParent(null);
}
}
return $this;
}
/**
* @return Collection<int, UserSetting>
*/
public function getSettings(): Collection
{
return $this->settings;
}
public function getSetting($name)
{
foreach ($this->getSettings() as $setting) {
if ($setting->getSetting()->getName() == $name) {
if ($setting->getValue() == 'false') {
return false;
}
return $setting->getValue();
}
}
return false;
}
public function addSetting(UserSetting $setting): void
{
$setting->setUser($this);
$this->settings->add($setting);
/*if (!$this->settings->contains($setting)) {
$this->settings[] = $setting;
$setting->setUser($this);
}
return $this;
*/
}
public function removeSetting(UserSetting $setting): void
{
/*if ($this->settings->removeElement($setting)) {
// set the owning side to null (unless already changed)
if ($setting->getUser() === $this) {
$setting->setUser(null);
}
}
return $this;*/
}
public function isGranted($role)
{
return in_array($role, $this->getRoles());
}
public function isIsVerified(): ?bool
{
return $this->isVerified;
}
/**
* @return Collection<int, Shop>
*/
public function getShops(): Collection
{
return $this->shops;
}
public function addShop(Shop $shop): static
{
if (!$this->shops->contains($shop)) {
$this->shops->add($shop);
}
return $this;
}
public function removeShop(Shop $shop): static
{
$this->shops->removeElement($shop);
return $this;
}
public function getActive():bool
{
return $this->active;
}
public function setActive(bool $active):self
{
$this->active = $active;
return $this;
}
public function isActive(): ?bool
{
return $this->active;
}
/**
* @return Collection<int, Sav>
*/
public function getSavs(): Collection
{
return $this->savs;
}
public function addSav(Sav $sav): static
{
if (!$this->savs->contains($sav)) {
$this->savs->add($sav);
$sav->setUser($this);
}
return $this;
}
public function removeSav(Sav $sav): static
{
if ($this->savs->removeElement($sav)) {
// set the owning side to null (unless already changed)
if ($sav->getUser() === $this) {
$sav->setUser(null);
}
}
return $this;
}
}