<?php
namespace App\Entity;
use App\Repository\MembershipRepository;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Entity(repositoryClass: MembershipRepository::class)]
#[ORM\UniqueConstraint(name: "association_table", columns: ['company_id', 'club_id'])]
#[UniqueEntity(fields: ['company', 'club'], message: 'A membership already exists for this company in this club.', errorPath: 'user')]
class Membership
{
const STATUS_ACTIVE = 'active';
const STATUS_INACTIVE = 'inactive';
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
use TimestampableEntity;
#[ORM\ManyToOne(cascade: ["persist"], inversedBy: 'memberships')]
#[ORM\JoinColumn(nullable: false, onDelete: "CASCADE")]
#[Groups([
'read:club:collection:getCompanies',
'read:club:collection',
'read:user:item:get',
'read:user:item:me',
'read:company:collection:get',
])]
private ?Club $club = null;
#[ORM\ManyToOne(cascade: ["persist"], inversedBy: 'memberships')]
#[ORM\JoinColumn(nullable: false, onDelete: "CASCADE")]
private ?Company $company = null;
#[ORM\Column(length: 255)]
#[Groups([
'read:user:item:me',
])]
private ?string $status = null;
public function __construct()
{
$this->status = self::STATUS_ACTIVE;
}
public function __toString(): string
{
return $this->club.' --> '.$this->company;
}
public static function getStatusList(): array
{
return [
self::STATUS_ACTIVE => 'Active',
self::STATUS_INACTIVE => 'Inactive',
];
}
public function getStatusLabel(): ?string
{
$list = self::getStatusList();
return ($this->status != null && array_key_exists($this->status, $list) ? $list[$this->status] : null);
}
public function getStatusClass(): ?string
{
return match ($this->status) {
self::STATUS_ACTIVE => 'success',
self::STATUS_INACTIVE => 'danger',
default => null,
};
}
public function getId(): ?int
{
return $this->id;
}
public function getClub(): ?Club
{
return $this->club;
}
public function setClub(?Club $club): self
{
$this->club = $club;
return $this;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(string $status): self
{
$this->status = $status;
return $this;
}
}