src/Entity/TimeNormalization/WorkDay.php line 54

Open in your IDE?
  1. <?php
  2. namespace App\Entity\TimeNormalization;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiProperty;
  5. use ApiPlatform\Core\Annotation\ApiResource;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
  7. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  8. use App\Interfaces\UuidKeyInterface;
  9. use App\Repository\TimeNormalization\WorkDayRepository;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  12. use Symfony\Component\{
  13.     Serializer\Annotation\Context,
  14.     Serializer\Annotation\Groups,
  15.     Serializer\Normalizer\DateTimeNormalizer,
  16.     Validator\Constraints as Assert
  17. };
  18. /**
  19.  * День рабочего календаря.
  20.  */
  21. #[ApiResource(
  22.     collectionOperations: [
  23.         'get' => [
  24.             'openapi_context' => ['summary' => 'Получить список дней рабочего календаря''description' => 'Получить список дней рабочего календаря'],
  25.         ],
  26.     ],
  27.     itemOperations: [
  28.         'get' => [
  29.             'openapi_context' => [
  30.                 'summary' => 'Получить данные дня рабочего календаря',
  31.                 'description' => 'публичный метод для получения данных по дню рабочего календаря',
  32.             ],
  33.         ],
  34.         'patch' => [
  35.             'security' => "is_granted('ROLE_ADMIN')",
  36.             'denormalization_context' => ['groups' => [self::PATCH_GROUP]],
  37.             'openapi_context' => [
  38.                 'summary' => 'Изменить день рабочего календаря',
  39.                 'description' => 'требуется роль Администратора (ROLE_ADMIN)',
  40.             ],
  41.         ],
  42.     ],
  43.     order: ['day' => 'ASC'],
  44.     paginationItemsPerPage31,
  45. )]
  46. #[ApiFilter(DateFilter::class, properties: ['day'])]
  47. #[ApiFilter(OrderFilter::class, properties: ['day' => 'ASC'])]
  48. #[ORM\Entity(repositoryClassWorkDayRepository::class)]
  49. #[ORM\Table(name'work_days')]
  50. #[UniqueEntity(fields: ['day'])]
  51. class WorkDay implements UuidKeyInterface
  52. {
  53.     public const READ_GROUP 'WorkDay:read';
  54.     public const WRITE_GROUP 'WorkDay:write';
  55.     public const PATCH_GROUP 'WorkDay:patch';
  56.     public const TYPE_WORKDAY 'workday';
  57.     public const TYPE_WEEKEND 'weekend';
  58.     public const TYPE_HOLIDAY 'holiday';
  59.     public const TYPE_SHORTDAY 'shortday';
  60.     public const TYPES = [
  61.         self::TYPE_WORKDAY,
  62.         self::TYPE_WEEKEND,
  63.         self::TYPE_HOLIDAY,
  64.         self::TYPE_SHORTDAY,
  65.     ];
  66.     /** список типов рабочих дней */
  67.     public const TYPES_WORKDAYS = [
  68.         self::TYPE_SHORTDAY,
  69.         self::TYPE_WORKDAY,
  70.     ];
  71.     #[Groups([self::READ_GROUP])]
  72.     #[ApiProperty(writablefalseidentifierfalse)]
  73.     #[ORM\Id]
  74.     #[ORM\Column(type'uuid'uniquetrue)]
  75.     #[ORM\GeneratedValue(strategy'CUSTOM')]
  76.     #[ORM\CustomIdGenerator(class: 'App\Doctrine\Generator\UuidGenerator')]
  77.     protected ?string $id null;
  78.     #[Assert\NotBlank(groups: [self::WRITE_GROUP])]
  79.     #[Groups([self::READ_GROUPself::WRITE_GROUP])]
  80.     #[ApiProperty(description'дата'writablefalseidentifiertrueopenapiContext: ['format' => 'date'])]
  81.     #[ORM\Column(type'date'nullablefalseuniquetrue)]
  82.     #[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
  83.     private \DateTimeInterface $day;
  84.     #[Assert\Choice(choicesself::TYPES)]
  85.     #[ApiProperty(
  86.         description'тип',
  87.         requiredfalse,
  88.         exampleself::TYPE_WORKDAY,
  89.         openapiContext: [
  90.             'type' => 'string',
  91.             'enum' => self::TYPES,
  92.         ]
  93.     )]
  94.     #[Groups([self::READ_GROUPself::WRITE_GROUPself::PATCH_GROUP])]
  95.     #[ORM\Column(name'day_type'type'string'length10options: ['default' => 'workday'])]
  96.     private string $dayType self::TYPE_WORKDAY;
  97.     #[Groups([self::READ_GROUPself::WRITE_GROUPself::PATCH_GROUP])]
  98.     #[ApiProperty(description'выходной?'requiredfalse)]
  99.     #[ORM\Column(name'is_weekend'type'boolean'nullablefalseoptions: ['default' => false])]
  100.     private bool $isWeekend false;
  101.     public function __construct(\DateTimeInterface $daystring $dayTypebool $isWeekend null)
  102.     {
  103.         $this->setDay($day);
  104.         $this->dayType $dayType;
  105.         $this->setIsWeekend($isWeekend);
  106.     }
  107.     public function getId(): ?string
  108.     {
  109.         return $this->id;
  110.     }
  111.     #[ApiProperty(identifiertruedescription'дата'openapiContext: ['format' => 'date'])]
  112.     public function getDay(): string
  113.     {
  114.         return $this->day->format('Y-m-d');
  115.     }
  116.     #[ApiProperty(readablefalse)]
  117.     public function getDate(): \DateTimeInterface
  118.     {
  119.         return $this->day;
  120.     }
  121.     public function setDay(\DateTimeInterface $day): self
  122.     {
  123.         $this->day \DateTime::createFromInterface($day);
  124.         return $this;
  125.     }
  126.     public function getDayType(): ?string
  127.     {
  128.         return $this->dayType;
  129.     }
  130.     public function setDayType(string $dayType): self
  131.     {
  132.         $this->dayType $dayType;
  133.         return $this;
  134.     }
  135.     public function getIsWeekend(): ?bool
  136.     {
  137.         return $this->isWeekend;
  138.     }
  139.     public function setIsWeekend(bool $isWeekend null): self
  140.     {
  141.         $this->isWeekend $isWeekend ?? self::isWeekend($this->day);
  142.         return $this;
  143.     }
  144.     /** является ли день субботой или воскресеньем */
  145.     public static function isWeekend(\DateTimeInterface $day): bool
  146.     {
  147.         return $day->format('N') > 5;
  148.     }
  149.     /** является ли день рабочим */
  150.     public function isWorkDay(): bool
  151.     {
  152.         return \in_array($this->dayTypeself::TYPES_WORKDAYS);
  153.     }
  154. }