<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Project\Project;
use App\Interfaces\SluggableInterface;
use App\Interfaces\UuidKeyInterface;
use App\Repository\UnitRepository;
use App\Traits\SlugAsIdTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
#[ApiResource(
collectionOperations: [
'get',
'post' => ['security' => "is_granted('ROLE_MASTER_MANAGER')"],
],
itemOperations: [
'get',
'put' => ['security' => "is_granted('ROLE_MASTER_MANAGER')",
'openapi_context' => [
'summary' => 'Updates the Unit resource',
'description' => 'Updates the Unit resource',
'requestBody' => [
'content' => [
'application/ld+json' => [
'schema' => [
'type' => 'object',
'properties' => [
'name' => ['type' => 'string', 'maxLength' => 255],
'slug' => ['type' => 'string', 'maxLength' => 255],
],
],
'example' => [
'name' => 'string',
'slug' => 'string',
],
],
],
],
],
],
'patch' => ['security' => "is_granted('ROLE_MASTER_MANAGER')",
'openapi_context' => [
'summary' => 'Updates the Unit resource',
'description' => 'Updates the Unit resource',
'requestBody' => [
'content' => [
'application/merge-patch+json' => [
'schema' => [
'type' => 'object',
'properties' => [
'name' => ['type' => 'string', 'maxLength' => 255],
'slug' => ['type' => 'string', 'maxLength' => 255],
],
],
'example' => [
'name' => 'string',
'slug' => 'string',
],
],
],
],
],
],
'delete' => ['security' => "is_granted('ROLE_MASTER_MANAGER')"],
],
)]
#[ORM\Entity(repositoryClass: UnitRepository::class)]
#[ORM\Table(name: 'unit')]
#[ORM\Index(name: 'idx_unit_slug', columns: ['slug'])]
#[UniqueEntity(fields: ['name'])]
#[UniqueEntity(fields: ['slug'])]
class Unit implements UuidKeyInterface, SluggableInterface
{
use SlugAsIdTrait;
public const READ_GROUP = 'Unit:read';
public const WRITE_GROUP = 'Unit:write';
#[Assert\NotBlank]
#[Groups([self::READ_GROUP, self::WRITE_GROUP, Project::READ_GROUP, User::READ_GROUP])]
#[ApiProperty(attributes: ['openapi_context' => ['type' => 'string', 'required' => false]])]
#[ORM\Column(type: 'string', length: 255, nullable: false, options: ['comment' => 'Name of Unit'])]
protected ?string $name = null;
#[Gedmo\Slug(fields: ['name'], updatable: false, separator: '-')]
#[Assert\NotBlank]
#[Assert\Regex(pattern: '/^[a-z0-9]+(?:-[a-z0-9]+)*$/i')]
#[ApiProperty(iri: 'https://schema.org/identifier', identifier: true)]
#[Groups([self::READ_GROUP, self::WRITE_GROUP, User::READ_GROUP])]
#[ORM\Column(type: 'string', length: 255, unique: true)]
protected ?string $slug = null;
#[ORM\OneToMany(targetEntity: Project::class, mappedBy: 'unit')]
private Collection $projects;
public function __construct(string $name = null, string $slug = null)
{
$this->name = $name;
$this->slug = $slug;
$this->projects = new ArrayCollection();
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
public function setSlug(?string $slug): self
{
$this->slug = $slug;
return $this;
}
#[Groups([self::READ_GROUP])]
public function getProjectCount(): int
{
return $this->projects->count();
}
public function __toString(): string
{
return $this->getName() ?? $this->slug ?? '';
}
}