<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use App\Validator\CompanyDomainEmail;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserCompanyRepository")
* @ORM\HasLifecycleCallbacks()
* @CompanyDomainEmail()
*/
#[ApiResource(
attributes: ['security' => "is_granted('ROLE_USER')"],
normalizationContext: ['groups' => ['userCompany:read']],
denormalizationContext: ['groups' => ['userCompany:write']],
collectionOperations: ['get', 'post'],
itemOperations: ['get', 'put']
)]
#[ApiFilter(PropertyFilter::class)]
#[ApiFilter(SearchFilter::class, properties: ['id', 'email' => 'partial', 'user' => 'exact', 'company' => 'exact'])]
#[ApiFilter(OrderFilter::class, properties: ['id', 'email', 'user' => 'exact', 'company' => 'exact'], arguments: ['orderParameterName' => 'order'])]
class UserCompany
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="userCompanies")
* @ORM\JoinColumn(nullable=false)
*/
private $user;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="userCompanies")
* @ORM\JoinColumn(nullable=false)
* @Groups({"user:item", "user:write","userCompany:read", "userCompany:write"})
*/
private $company;
/**
* @ORM\Column(type="string", length=20, nullable=true)
* @Groups({"user:item", "user:write","userCompany:read", "userCompany:write"})
*/
private $token;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Assert\Email()
* @Assert\NotBlank()
* @Groups({"user:item", "user:write","userCompany:read", "userCompany:write"})
*/
private $email;
/**
* @ORM\Column(type="boolean")
* @Groups({"user:item", "user:write","userCompany:read", "userCompany:write"})
*/
private $verified;
/**
* @var DateTimeImmutable
*
* @ORM\Column(type="datetimetz_immutable", nullable=true)
*/
private $verifiedAt;
public function __construct()
{
$this->verified = false;
}
public function __toString()
{
return $this->getCompany()->getName() . (($this->verified) ? '(Verified)' : 'Unverified');
}
public function getId(): ?int
{
return $this->id;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
public function getToken(): ?string
{
return $this->token;
}
public function setToken(?string $token): self
{
$this->token = $token;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(?string $email): self
{
$this->email = $email;
return $this;
}
public function getVerified(): ?bool
{
return $this->verified;
}
public function setVerified(bool $verified): self
{
$this->verified = $verified;
return $this;
}
public function getVerifiedAt(): ?DateTimeImmutable
{
return $this->verifiedAt;
}
public function setVerifiedAt(?DateTimeImmutable $verifiedAt): self
{
$this->verifiedAt = $verifiedAt;
return $this;
}
}