Untitled
unknown
plain_text
5 months ago
6.0 kB
2
Indexable
<?php // /src/Entity/License.php namespace App\Entity; use ApiPlatform\Metadata\ApiResource; use App\Validator\Constraints\UniqueProductNames; use Symfony\Component\Serializer\Annotation\Context; use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; use Symfony\Component\Validator\Constraints as Assert; use App\Repository\LicenseRepository; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: LicenseRepository::class)] #[ORM\HasLifecycleCallbacks] #[ApiResource] class License { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column] private ?int $id = null; #[ORM\Column(length: 255)] #[Assert\NotBlank] #[Assert\Length(max: 255, maxMessage: "The product name cannot exceed 255 characters.")] #[UniqueProductNames] private ?string $productName = null; #[ORM\Column(length: 255, nullable: true)] #[Assert\Length(max: 255, maxMessage: "The description cannot exceed 255 characters.")] private ?string $description = null; #[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)] #[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])] #[Assert\LessThanOrEqual(propertyPath: "activationDate", message: "The issued date must be before or equal to the activation date.")] private ?\DateTimeInterface $issuedDate = null; #[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)] #[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])] private ?\DateTimeInterface $expirationDate = null; #[ORM\Column(type: Types::DATE_MUTABLE, nullable: true)] #[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])] private ?\DateTimeInterface $activationDate = null; #[ORM\Column(length: 255, nullable: true)] private ?string $status = null; #[ORM\Column(type: Types::DATETIME_IMMUTABLE)] private ?\DateTimeInterface $createdAt = null; #[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)] private ?\DateTimeInterface $updatedAt = null; #[ORM\ManyToOne(inversedBy: 'licenses')] #[ORM\JoinColumn(nullable: false)] private ?User $owner = null; public function __construct() { } #[ORM\PrePersist] public function onPrePersist(): void { $this->createdAt = new \DateTimeImmutable(); $currentDate = new \DateTimeImmutable(); // Validate expiration date if ($this->issuedDate && $this->expirationDate && $this->expirationDate < $this->issuedDate) { throw new \InvalidArgumentException("Expiration date must be after the issued date."); } // Set status based on expiration and activation date if ($this->expirationDate && $this->expirationDate < $currentDate) { $this->status = 'Expired'; } elseif ($this->activationDate && $this->activationDate >= $this->issuedDate) { $this->status = 'Active'; } else { $this->status = 'Inactive'; } } #[ORM\PreUpdate] public function onPreUpdate(): void { $this->updatedAt = new \DateTimeImmutable(); // Update expiration-related status checks if ($this->expirationDate !== null) { $currentDate = new \DateTimeImmutable(); if ($this->expirationDate < $currentDate) { $this->status = 'Expired'; throw new \InvalidArgumentException("Expiration date must be after the issued date."); } } // Check activation date logic on update if ($this->activationDate !== null && $this->activationDate < $this->issuedDate) { throw new \InvalidArgumentException("Activation date must be after the issued date."); } } // Getter and Setter methods... public function getId(): ?int { return $this->id; } public function getProductName(): ?string { return $this->productName; } public function setProductName(string $productName): static { $this->productName = $productName; return $this; } public function getDescription(): ?string { return $this->description; } public function setDescription(?string $description): static { $this->description = $description; return $this; } public function getIssuedDate(): ?\DateTimeInterface { return $this->issuedDate; } public function setIssuedDate(?\DateTimeInterface $issuedDate): static { $this->issuedDate = $issuedDate; return $this; } public function getExpirationDate(): ?\DateTimeInterface { return $this->expirationDate; } public function setExpirationDate(?\DateTimeInterface $expirationDate): static { $this->expirationDate = $expirationDate; return $this; } public function getActivationDate(): ?\DateTimeInterface { return $this->activationDate; } public function setActivationDate(?\DateTimeInterface $activationDate): static { $this->activationDate = $activationDate; return $this; } public function getStatus(): ?string { return $this->status; } public function setStatus(string $status): static { $this->status = $status; return $this; } public function getCreatedAt(): ?\DateTimeInterface { return $this->createdAt; } public function setCreatedAt(\DateTimeInterface $createdAt): static { $this->createdAt = $createdAt; return $this; } public function getUpdatedAt(): ?\DateTimeInterface { return $this->updatedAt; } public function setUpdatedAt(?\DateTimeInterface $updatedAt): static { $this->updatedAt = $updatedAt; return $this; } public function getOwner(): ?User { return $this->owner; } public function setOwner(?User $owner): static { $this->owner = $owner; return $this; } }
Editor is loading...
Leave a Comment