src/Entity/Package.php line 15

  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\PackageRepository;
  4. use App\Traits\TimeStampTrait;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\DBAL\Types\Types;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. #[ORM\Entity(repositoryClassPackageRepository::class)]
  11. class Package
  12. {
  13.     use TimeStampTrait;
  14.     public const STARTER 'Starter';
  15.     public const STANDARD 'Standard';
  16.     public const PRO 'Pro';
  17.     public const BUSINESS 'Business';
  18.     #[ORM\Id]
  19.     #[ORM\GeneratedValue]
  20.     #[ORM\Column(type'integer')]
  21.     private ?int $id null;
  22.     /**
  23.      * Billing type (e.g. "monthly", "yearly") or any internal categorization you use.
  24.      * Avoid “interval” (reserved in SQL).
  25.      */
  26.     #[ORM\Column(length100)]
  27.     #[Assert\NotBlank]
  28.     #[Assert\Length(max100)]
  29.     private ?string $type null;
  30.     #[ORM\Column(length255)]
  31.     #[Assert\NotBlank]
  32.     #[Assert\Length(max255)]
  33.     private ?string $description null;
  34.     #[ORM\Column(options: ['default' => true])]
  35.     private ?bool $publish true;
  36.     #[ORM\OneToMany(mappedBy'package'targetEntitySubscription::class)]
  37.     private Collection $entreprisePackages;
  38.     #[ORM\Column(length25)]
  39.     #[Assert\NotBlank]
  40.     #[Assert\Length(max25)]
  41.     private ?string $name null;
  42.     // Decimal columns map to string in PHP with Doctrine.
  43.     #[ORM\Column(typeTypes::DECIMALprecision10scale2)]
  44.     #[Assert\NotBlank]
  45.     #[Assert\Regex('/^\d+(\.\d{1,2})?$/')]
  46.     private ?string $price null// monthly price
  47.     #[ORM\OneToMany(mappedBy'package'targetEntityEntreprise::class)]
  48.     private Collection $entreprises;
  49.     #[ORM\Column(typeTypes::DECIMALprecision10scale2nullabletrue)]
  50.     #[Assert\Regex('/^\d+(\.\d{1,2})?$/')]
  51.     private ?string $priceannuel null;
  52.     /**
  53.      * Maximum allowed “lots/units” for this package. NULL = unlimited.
  54.      */
  55.     #[ORM\Column(type'integer'nullabletrueoptions: ['unsigned' => true])]
  56.     #[Assert\Positive]
  57.     private ?int $maxUnits null;
  58.     /**
  59.      * Number of free trial months (default: 2).
  60.      */
  61.     #[ORM\Column(type'smallint'options: ['unsigned' => true'default' => 2])]
  62.     #[Assert\PositiveOrZero]
  63.     private int $trialMonths 2;
  64.     public function __construct()
  65.     {
  66.         $this->entreprisePackages = new ArrayCollection();
  67.         $this->entreprises = new ArrayCollection();
  68.     }
  69.     // -----------------------
  70.     // Core getters / setters
  71.     // -----------------------
  72.     public function getId(): ?int
  73.     {
  74.         return $this->id;
  75.     }
  76.     public function setId(?int $id): void
  77.     {
  78.         $this->id $id;
  79.     }
  80.     public function getType(): ?string
  81.     {
  82.         return $this->type;
  83.     }
  84.     public function setType(?string $type): void
  85.     {
  86.         $this->type $type;
  87.     }
  88.     public function getDescription(): ?string
  89.     {
  90.         return $this->description;
  91.     }
  92.     public function setDescription(?string $description): void
  93.     {
  94.         $this->description $description;
  95.     }
  96.     public function getPublish(): ?bool
  97.     {
  98.         return $this->publish;
  99.     }
  100.     public function setPublish(?bool $publish): void
  101.     {
  102.         $this->publish $publish;
  103.     }
  104.     /**
  105.      * @return Collection<int, Subscription>
  106.      */
  107.     public function getEntreprisePackages(): Collection
  108.     {
  109.         return $this->entreprisePackages;
  110.     }
  111.     public function addEntreprisePackage(Subscription $entreprisePackage): static
  112.     {
  113.         if (!$this->entreprisePackages->contains($entreprisePackage)) {
  114.             $this->entreprisePackages->add($entreprisePackage);
  115.             $entreprisePackage->setPackage($this);
  116.         }
  117.         return $this;
  118.     }
  119.     public function removeEntreprisePackage(Subscription $entreprisePackage): static
  120.     {
  121.         if ($this->entreprisePackages->removeElement($entreprisePackage)) {
  122.             if ($entreprisePackage->getPackage() === $this) {
  123.                 $entreprisePackage->setPackage(null);
  124.             }
  125.         }
  126.         return $this;
  127.     }
  128.     public function getName(): ?string
  129.     {
  130.         return $this->name;
  131.     }
  132.     public function setName(string $name): static
  133.     {
  134.         $this->name $name;
  135.         return $this;
  136.     }
  137.     public function getPrice(): ?string
  138.     {
  139.         return $this->price;
  140.     }
  141.     public function setPrice(string $price): static
  142.     {
  143.         $this->price $price;
  144.         return $this;
  145.     }
  146.     /**
  147.      * @return Collection<int, Entreprise>
  148.      */
  149.     public function getEntreprises(): Collection
  150.     {
  151.         return $this->entreprises;
  152.     }
  153.     public function addEntreprise(Entreprise $entreprise): static
  154.     {
  155.         if (!$this->entreprises->contains($entreprise)) {
  156.             $this->entreprises->add($entreprise);
  157.             $entreprise->setPackage($this);
  158.         }
  159.         return $this;
  160.     }
  161.     public function removeEntreprise(Entreprise $entreprise): static
  162.     {
  163.         if ($this->entreprises->removeElement($entreprise)) {
  164.             if ($entreprise->getPackage() === $this) {
  165.                 $entreprise->setPackage(null);
  166.             }
  167.         }
  168.         return $this;
  169.     }
  170.     public function getPriceannuel(): ?string
  171.     {
  172.         return $this->priceannuel;
  173.     }
  174.     public function setPriceannuel(?string $priceannuel): static
  175.     {
  176.         $this->priceannuel $priceannuel;
  177.         return $this;
  178.     }
  179.     public function getMaxUnits(): ?int
  180.     {
  181.         return $this->maxUnits;
  182.     }
  183.     public function setMaxUnits(?int $maxUnits): static
  184.     {
  185.         $this->maxUnits $maxUnits;
  186.         return $this;
  187.     }
  188.     public function getTrialMonths(): int
  189.     {
  190.         return $this->trialMonths;
  191.     }
  192.     public function setTrialMonths(int $trialMonths): static
  193.     {
  194.         $this->trialMonths $trialMonths;
  195.         return $this;
  196.     }
  197.     // -------------
  198.     // Helper API
  199.     // -------------
  200.     public function hasUnlimitedUnits(): bool
  201.     {
  202.         return $this->maxUnits === null;
  203.     }
  204.     public function getLotsLimitLabel(): string
  205.     {
  206.         return $this->hasUnlimitedUnits() ? 'Illimité' : (string)$this->maxUnits;
  207.     }
  208.     /** Float accessors for easier math (Doctrine stores DECIMAL as string) */
  209.     public function getMonthlyPriceFloat(): ?float
  210.     {
  211.         return $this->price !== null ? (float)$this->price null;
  212.     }
  213.     public function getAnnualPriceFloat(): ?float
  214.     {
  215.         return $this->priceannuel !== null ? (float)$this->priceannuel null;
  216.     }
  217.     /** Trial end from a given start date (e.g., signup date) */
  218.     public function getTrialEndFrom(\DateTimeInterface $start): \DateTimeImmutable
  219.     {
  220.         $dt \DateTimeImmutable::createFromInterface($start);
  221.         return $dt->modify('+' max(0$this->trialMonths) . ' months');
  222.     }
  223.     public function __toString(): string
  224.     {
  225.         return sprintf('%s (%s)'$this->name ?? 'Package'$this->type ?? 'type');
  226.     }
  227. }
  228. //
  229. //namespace App\Entity;
  230. //
  231. //use App\Repository\PackageRepository;
  232. //use App\Traits\TimeStampTrait;
  233. //use Doctrine\Common\Collections\ArrayCollection;
  234. //use Doctrine\Common\Collections\Collection;
  235. //use Doctrine\DBAL\Types\Types;
  236. //use Doctrine\ORM\Mapping as ORM;
  237. //
  238. //#[ORM\Entity(repositoryClass: PackageRepository::class)]
  239. //class Package
  240. //{
  241. //    use TimeStampTrait;
  242. //
  243. //    #[ORM\Id]
  244. //    #[ORM\GeneratedValue]
  245. //    #[ORM\Column(type: 'integer')]
  246. //    private ?int $id = null;
  247. //
  248. //    #[ORM\Column(length: 100)]
  249. //    private ?string $type = null;
  250. //
  251. //    #[ORM\Column(length: 255)]
  252. //    private ?string $description = null;
  253. //
  254. //    #[ORM\Column]
  255. //    private ?bool $publish = null;
  256. //
  257. //    #[ORM\OneToMany(mappedBy: 'package', targetEntity: Subscription::class)]
  258. //    private Collection $entreprisePackages;
  259. //
  260. //    #[ORM\Column(length: 25)]
  261. //    private ?string $name = null;
  262. //
  263. //    #[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2)]
  264. //    private ?string $price = null;
  265. //
  266. //    #[ORM\OneToMany(mappedBy: 'package', targetEntity: Entreprise::class)]
  267. //    private Collection $entreprises;
  268. //
  269. //    #[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
  270. //    private ?string $priceannuel = null;
  271. //
  272. //    public function __construct()
  273. //    {
  274. //        $this->entreprisePackages = new ArrayCollection();
  275. //        $this->entreprises = new ArrayCollection();
  276. //    }
  277. //
  278. //    /**
  279. //     * @return int|null
  280. //     */
  281. //    public function getId(): ?int
  282. //    {
  283. //        return $this->id;
  284. //    }
  285. //
  286. //    /**
  287. //     * @param int|null $id
  288. //     */
  289. //    public function setId(?int $id): void
  290. //    {
  291. //        $this->id = $id;
  292. //    }
  293. //
  294. //    /**
  295. //     * @return string|null
  296. //     */
  297. //    public function getType(): ?string
  298. //    {
  299. //        return $this->type;
  300. //    }
  301. //
  302. //    /**
  303. //     * @param string|null $type
  304. //     */
  305. //    public function setType(?string $type): void
  306. //    {
  307. //        $this->type = $type;
  308. //    }
  309. //
  310. //    /**
  311. //     * @return string|null
  312. //     */
  313. //    public function getDescription(): ?string
  314. //    {
  315. //        return $this->description;
  316. //    }
  317. //
  318. //    /**
  319. //     * @param string|null $description
  320. //     */
  321. //    public function setDescription(?string $description): void
  322. //    {
  323. //        $this->description = $description;
  324. //    }
  325. //
  326. //    /**
  327. //     * @return bool|null
  328. //     */
  329. //    public function getPublish(): ?bool
  330. //    {
  331. //        return $this->publish;
  332. //    }
  333. //
  334. //    /**
  335. //     * @param bool|null $publish
  336. //     */
  337. //    public function setPublish(?bool $publish): void
  338. //    {
  339. //        $this->publish = $publish;
  340. //    }
  341. //
  342. //    /**
  343. //     * @return Collection<int, Subscription>
  344. //     */
  345. //    public function getEntreprisePackages(): Collection
  346. //    {
  347. //        return $this->entreprisePackages;
  348. //    }
  349. //
  350. //    public function addEntreprisePackage(Subscription $entreprisePackage): static
  351. //    {
  352. //        if (!$this->entreprisePackages->contains($entreprisePackage)) {
  353. //            $this->entreprisePackages->add($entreprisePackage);
  354. //            $entreprisePackage->setPackage($this);
  355. //        }
  356. //
  357. //        return $this;
  358. //    }
  359. //
  360. //    public function removeEntreprisePackage(Subscription $entreprisePackage): static
  361. //    {
  362. //        if ($this->entreprisePackages->removeElement($entreprisePackage)) {
  363. //            // set the owning side to null (unless already changed)
  364. //            if ($entreprisePackage->getPackage() === $this) {
  365. //                $entreprisePackage->setPackage(null);
  366. //            }
  367. //        }
  368. //
  369. //        return $this;
  370. //    }
  371. //
  372. //    public function getName(): ?string
  373. //    {
  374. //        return $this->name;
  375. //    }
  376. //
  377. //    public function setName(string $name): static
  378. //    {
  379. //        $this->name = $name;
  380. //
  381. //        return $this;
  382. //    }
  383. //
  384. //    public function getPrice(): ?string
  385. //    {
  386. //        return $this->price;
  387. //    }
  388. //
  389. //    public function setPrice(string $price): static
  390. //    {
  391. //        $this->price = $price;
  392. //
  393. //        return $this;
  394. //    }
  395. //
  396. //    /**
  397. //     * @return Collection<int, Entreprise>
  398. //     */
  399. //    public function getEntreprises(): Collection
  400. //    {
  401. //        return $this->entreprises;
  402. //    }
  403. //
  404. //    public function addEntreprise(Entreprise $entreprise): static
  405. //    {
  406. //        if (!$this->entreprises->contains($entreprise)) {
  407. //            $this->entreprises->add($entreprise);
  408. //            $entreprise->setPackage($this);
  409. //        }
  410. //
  411. //        return $this;
  412. //    }
  413. //
  414. //    public function removeEntreprise(Entreprise $entreprise): static
  415. //    {
  416. //        if ($this->entreprises->removeElement($entreprise)) {
  417. //            // set the owning side to null (unless already changed)
  418. //            if ($entreprise->getPackage() === $this) {
  419. //                $entreprise->setPackage(null);
  420. //            }
  421. //        }
  422. //
  423. //        return $this;
  424. //    }
  425. //
  426. //    public function getPriceannuel(): ?string
  427. //    {
  428. //        return $this->priceannuel;
  429. //    }
  430. //
  431. //    public function setPriceannuel(?string $priceannuel): static
  432. //    {
  433. //        $this->priceannuel = $priceannuel;
  434. //
  435. //        return $this;
  436. //    }
  437. //}