<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: "App\Repository\UserRepository")]
#[UniqueEntity(fields: ['username'], message: 'Username deja utilisé')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private int $id;
#[ORM\Column(length: 180, unique: true)]
private string $username;
#[ORM\Column(type: "json")]
private array $roles = [];
#[ORM\Column]
private string $password;
#[Assert\EqualTo(propertyPath:'password', message: 'Les mots de passe ne sont pas identiques' )]
private string $passwordConfirm;
#[ORM\OneToMany(mappedBy: 'User', targetEntity: Product::class, orphanRemoval: true)]
private Collection $Products;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $image = null;
private ?File $imageFile = null;
public function __construct()
{
$this->Products = new ArrayCollection();
}
public function __toString(): string
{
return $this->username;
}
public function getId(): ?int
{
return $this->id;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUsername(): string
{
return (string) $this->username;
}
public function setUsername(string $username): self
{
$this->username = $username;
return $this;
}
/**
* @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 UserInterface
*/
public function getPassword(): string
{
return $this->password;
}
public function setPassword(string $password): self
{
$this->password = $password;
return $this;
}
/**
* @see UserInterface
*/
public function getSalt()
{
// not needed when using the "bcrypt" algorithm in security.yaml
}
/**
* @see UserInterface
*/
public function eraseCredentials()
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
}
/**
* @return mixed
*/
public function getFullName(): mixed
{
return $this->username;
}
public function getUserIdentifier(): string
{
return $this->getUsername();
}
/**
* @return Collection<int, Product>
*/
public function getProducts(): Collection
{
return $this->Products;
}
public function addProduct(Product $product): self
{
if (!$this->Products->contains($product)) {
$this->Products->add($product);
$product->setUser($this);
}
return $this;
}
public function removeProduct(Product $product): self
{
$this->Products->removeElement($product);
return $this;
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage(?string $image): self
{
$this->image = $image;
return $this;
}
public function getPasswordConfirm(): string
{
return $this->passwordConfirm;
}
public function setPasswordConfirm(string $passwordConfirm): self
{
$this->passwordConfirm = $passwordConfirm;
return $this;
}
public function getImageFile(): ?File
{
return $this->imageFile;
}
public function setImageFile(File $imageFile): self
{
$this->imageFile = $imageFile;
return $this;
}
}