src/Entity/Customer.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CustomerRepository;
  4. use App\Entity\Address\{BillingAddressShippingAddress};
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  9. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. use App\Entity\Order\Order;
  12. use Symfony\Component\Serializer\Annotation\Groups;
  13. #[ORM\Entity(repositoryClassCustomerRepository::class)]
  14. #[UniqueEntity(fields: ['email'], message'Cet email est déjà pris.')]
  15. class Customer implements UserInterfacePasswordAuthenticatedUserInterface
  16. {
  17.     #[ORM\Id]
  18.     #[ORM\GeneratedValue]
  19.     #[ORM\Column(type'integer')]
  20.     private $id;
  21.     #[ORM\Column(type'string'length180uniquetrue)]
  22.     private $email;
  23.     #[ORM\Column(type'json')]
  24.     private $roles = [];
  25.     #[ORM\Column(type'string')]
  26.     private $password;
  27.     #[ORM\Column(type'string'length255nullabletrue)]
  28.     private $name;
  29.     #[ORM\Column(type'string'length255nullabletrue)]
  30.     private $lastname1;
  31.     #[ORM\Column(type'string'length255nullabletrue)]
  32.     private $lastname2;
  33.     #[ORM\Column(type'string'length255nullabletrue)]
  34.     private $phone;
  35.     #[ORM\OneToMany(mappedBy'customer'targetEntityOrder::class)]
  36.     private $orders;
  37.     #[ORM\Column(type'boolean')]
  38.     private $isVerified false;
  39.     #[Groups(["address:read"])]
  40.     #[ORM\OneToOne(targetEntityBillingAddress::class, mappedBy'customer'cascade: ['persist''remove'])]
  41.     #[ORM\JoinColumn(nullabletrue)]
  42.     private $billingAddress
  43.     #[Groups(["address:read"])]
  44.     #[ORM\OneToOne(targetEntityShippingAddress::class, mappedBy'customer'cascade: ['persist''remove'])]
  45.     #[ORM\JoinColumn(nullabletrue)]
  46.     private ?ShippingAddress $shippingAddress;
  47.     public function __construct()
  48.     {
  49.         $this->orders = new ArrayCollection();
  50.     }
  51.     public function getId(): ?int
  52.     {
  53.         return $this->id;
  54.     }
  55.     public function getEmail(): ?string
  56.     {
  57.         return $this->email;
  58.     }
  59.     public function setEmail(string $email): self
  60.     {
  61.         $this->email $email;
  62.         return $this;
  63.     }
  64.     /**
  65.      * A visual identifier that represents this user.
  66.      *
  67.      * @see UserInterface
  68.      */
  69.     public function getUserIdentifier(): string
  70.     {
  71.         return (string) $this->email;
  72.     }
  73.     /**
  74.      * @see UserInterface
  75.      */
  76.     public function getRoles(): array
  77.     {
  78.         $roles $this->roles;
  79.         // guarantee every user at least has ROLE_USER
  80.         $roles[] = 'ROLE_USER';
  81.         return array_unique($roles);
  82.     }
  83.     public function setRoles(array $roles): self
  84.     {
  85.         $this->roles $roles;
  86.         return $this;
  87.     }
  88.     
  89.      public function hasRole($role){
  90.         if(!empty($this->getRoles())){
  91.             if(in_array($role$this->getRoles())){
  92.                 return true;
  93.             }
  94.         }
  95.         return false;
  96.     }
  97.     /**
  98.      * @see PasswordAuthenticatedUserInterface
  99.      */
  100.     public function getPassword(): string
  101.     {
  102.         return $this->password;
  103.     }
  104.     public function setPassword(string $password): self
  105.     {
  106.         $this->password $password;
  107.         return $this;
  108.     }
  109.     /**
  110.      * @see UserInterface
  111.      */
  112.     public function eraseCredentials()
  113.     {
  114.         // If you store any temporary, sensitive data on the user, clear it here
  115.         // $this->plainPassword = null;
  116.     }
  117.     public function getName(): ?string
  118.     {
  119.         return $this->name;
  120.     }
  121.     public function setName(?string $name): self
  122.     {
  123.         $this->name $name;
  124.         return $this;
  125.     }
  126.     public function getLastname1(): ?string
  127.     {
  128.         return $this->lastname1;
  129.     }
  130.     public function setLastname1(?string $lastname1): self
  131.     {
  132.         $this->lastname1 $lastname1;
  133.         return $this;
  134.     }
  135.     public function getLastname2(): ?string
  136.     {
  137.         return $this->lastname2;
  138.     }
  139.     public function setLastname2(?string $lastname2): self
  140.     {
  141.         $this->lastname2 $lastname2;
  142.         return $this;
  143.     }
  144.     public function getPhone(): ?string
  145.     {
  146.         return $this->phone;
  147.     }
  148.     public function setPhone(?string $phone): self
  149.     {
  150.         $this->phone $phone;
  151.         return $this;
  152.     }
  153.     /**
  154.      * @return Collection<int, Order>
  155.      */
  156.     public function getOrders(): Collection
  157.     {
  158.         return $this->orders;
  159.     }
  160.     public function addOrder(Order $order): self
  161.     {
  162.         if (!$this->orders->contains($order)) {
  163.             $this->orders[] = $order;
  164.             $order->setCustomer($this);
  165.         }
  166.         return $this;
  167.     }
  168.     public function removeOrder(Order $order): self
  169.     {
  170.         if ($this->orders->removeElement($order)) {
  171.             // set the owning side to null (unless already changed)
  172.             if ($order->getCustomer() === $this) {
  173.                 $order->setCustomer(null);
  174.             }
  175.         }
  176.         return $this;
  177.     }
  178.     public function isVerified(): bool
  179.     {
  180.         return $this->isVerified;
  181.     }
  182.     public function setIsVerified(bool $isVerified): self
  183.     {
  184.         $this->isVerified $isVerified;
  185.         return $this;
  186.     }
  187.     /**
  188.      * @deprecated since Symfony 5.3, use getUserIdentifier instead
  189.      */
  190.     public function getUsername(): string
  191.     {
  192.         return (string) $this->email;
  193.     }
  194.      /**
  195.      * Returning a salt is only needed, if you are not using a modern
  196.      * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
  197.      *
  198.      * @see UserInterface
  199.      */
  200.     public function getSalt(): ?string
  201.     {
  202.         return null;
  203.     }
  204.     public function getShippingAddress(): ?ShippingAddress
  205.     {
  206.         return $this->shippingAddress;
  207.     }
  208.     public function setShippingAddress(?ShippingAddress $shippingAddress): static
  209.     {
  210.         $this->shippingAddress $shippingAddress;
  211.         return $this;
  212.     }
  213.     public function getBillingAddress(): ?BillingAddress
  214.     {
  215.         return $this->billingAddress;
  216.     }
  217.     public function setBillingAddress(?BillingAddress $billingAddress): static
  218.     {
  219.         $this->billingAddress $billingAddress;
  220.         return $this;
  221.     }
  222.     public function __toString(){
  223.         return $this->getName()." ".$this->getLastname1()." ".$this->getLastname2();
  224.     }
  225. }