<?php
namespace App\Entity\Chat;
use App\Enum\ConversationType;
use App\Repository\Chat\CanalRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Drosalys\Bundle\ApiBundle\Serializer\Attributes\IdGroups;
use Symfony\Component\Serializer\Annotation as Serial;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: CanalRepository::class)]
#[ORM\Table(name: 'chat_canals')]
class Canal extends Conversation
{
#[ORM\Column(type: 'string', length: 255)]
#[
Assert\NotBlank(message: 'entity.tchat.canal.label.NotBlank'),
Assert\Length(
min: 2,
max: 255,
minMessage: 'entity.tchat.canal.label.min',
maxMessage: 'entity.tchat.canal.label.max',
)
]
#[Serial\Groups(groups: ['Conversation', 'PostCanal', 'Identifier', 'PutCanal'])]
private ?string $label = null;
#[ORM\Column(type: 'text', nullable: true)]
#[
Assert\Length(
min: 2,
max: 5000,
minMessage: 'entity.tchat.canal.description.min',
maxMessage: 'entity.tchat.canal.description.max',
)
]
#[Serial\Groups(groups: ['Conversation', 'PostCanal', 'PutCanal'])]
private ?string $description = null;
#[ORM\ManyToMany(targetEntity: Resource::class, inversedBy: 'canals')]
#[ORM\JoinTable(name: 'chat_canal_resources')]
#[IdGroups(groups: ['PostCanal'])]
#[Serial\Context(
context: ['entity_id_groups' => true]
)]
private Collection $resources;
#[ORM\Column(type: 'integer', nullable: false, options: ['default' => 2])]
#[
Assert\NotBlank()
]
#[Serial\Groups(groups: ['Conversation', 'PostCanal', 'PutCanal'])]
private ?int $openTo = null;
#[ORM\Column(type: 'boolean', nullable: false)]
private ?bool $autoCreation = false;
public function __construct()
{
$this->resources = new ArrayCollection();
parent::__construct();
}
public function getLabel(): ?string
{
return $this->label;
}
public function setLabel(string $label): self
{
$this->label = $label;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function isPublic(): ?bool
{
return $this->getType() === ConversationType::CANAL_PUBLIC;
}
public function hasResources(): bool
{
return !$this->getResources()->isEmpty();
}
public function getResources(): Collection
{
return $this->resources ?? $this->resources = new ArrayCollection();
}
public function setResources(Collection $resources): Canal
{
$this->resources = $resources;
return $this;
}
public function addResource(Resource $resource): self
{
if (!$this->getResources()->contains($resource)) {
$this->getResources()->add($resource);
}
return $this;
}
public function removeResource(Resource $resource): self
{
$this->getResources()->removeElement($resource);
return $this;
}
public function getOpenTo(): ?int
{
return $this->openTo;
}
public function setOpenTo(?int $openTo): Canal
{
$this->openTo = $openTo;
return $this;
}
public function getAutoCreation(): ?bool
{
return $this->autoCreation;
}
public function setAutoCreation(?bool $autoCreation): Canal
{
$this->autoCreation = $autoCreation;
return $this;
}
}