<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserFollowCompanyRepository")
* @ORM\HasLifecycleCallbacks()
* @UniqueEntity(fields={"user", "company"})
*/
#[ApiResource(
attributes: ['security' => "is_granted('ROLE_USER')"],
normalizationContext: ['groups' => ['user_follow_company:read']],
denormalizationContext: ['groups' => ['user_follow_company:write']],
collectionOperations: [
'get',
'post' => [
'security' => "is_granted('ROLE_ADMIN')",
],
],
itemOperations: [
'get',
'put' => ['security' => "is_granted('ROLE_ADMIN') or object.owner == user"],
],
)]
class UserFollowCompany
{
use TimestampableEntity;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="userFollowCompanies")
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
* @Groups({"user_follow_company:read", "user_follow_company:write"})
*/
private $user;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="userFollowedCompanies")
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
* @Groups({"user_follow_company:read", "user_follow_company:write"})
*/
private $company;
/**
* @ORM\Column(type="boolean")
* @Assert\Type("bool")
* @Groups({"user_follow_company:read", "user_follow_company:write"})
*/
private $followed;
/**
* UserFollowCompany constructor.
*
* @throws \Exception
*/
public function __construct()
{
$this->followed = false;
}
/**
* @return mixed
*/
public function __toString()
{
return $this->getCompany()->getName();
}
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 getFollowed(): bool
{
return $this->followed;
}
public function setFollowed(bool $followed): self
{
$this->followed = $followed;
return $this;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
}