src/Entity/Price.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Exception\InvalidArgumentException;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Component\Validator\Constraints as Assert;
  6. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  7. /**
  8.  * @ORM\Entity(
  9.  *     repositoryClass="App\Repository\PriceRepository"
  10.  * )
  11.  * @ORM\Table(
  12.  *     name="price",
  13.  *     options={"collate"="utf8_swedish_ci"}
  14.  * )
  15.  * @ORM\HasLifecycleCallbacks
  16.  */
  17. class Price implements EntityInterface
  18. {
  19.     /**
  20.      * @ORM\Id
  21.      * @ORM\Column(
  22.      *     type="integer"
  23.      * )
  24.      * @ORM\GeneratedValue(
  25.      *     strategy="AUTO"
  26.      * )
  27.      *
  28.      * @var int|null
  29.      */
  30.     protected ?int $id null;
  31.     /**
  32.      * @ORM\Column(
  33.      *     type="integer"
  34.      * )
  35.      * @Assert\NotNull
  36.      * @Assert\Range(
  37.      *     min=0
  38.      * )
  39.      *
  40.      * @var int
  41.      */
  42.     protected int $price;
  43.     /**
  44.      * @ORM\ManyToOne(
  45.      *     targetEntity="PriceList",
  46.      *     inversedBy="prices"
  47.      * )
  48.      * @ORM\JoinColumn(
  49.      *     name="price_list_id",
  50.      *     referencedColumnName="id",
  51.      *     onDelete="cascade"
  52.      * )
  53.      *
  54.      * @Assert\NotNull
  55.      *
  56.      * @var PriceList
  57.      */
  58.     protected PriceList $priceList;
  59.     /**
  60.      * @ORM\ManyToOne(
  61.      *     targetEntity="Product",
  62.      *     inversedBy="prices",
  63.      *     cascade={"persist"}
  64.      * )
  65.      * @ORM\JoinColumn(
  66.      *     name="product_id",
  67.      *     referencedColumnName="id",
  68.      *     onDelete="cascade"
  69.      * )
  70.      *
  71.      * @var Product|null
  72.      */
  73.     protected ?Product $product null;
  74.     /**
  75.      * @ORM\ManyToOne(
  76.      *     targetEntity="ProductVersion",
  77.      *     inversedBy="prices",
  78.      *     cascade={"persist"}
  79.      * )
  80.      * @ORM\JoinColumn(
  81.      *     name="product_version_id",
  82.      *     referencedColumnName="id",
  83.      *     onDelete="cascade"
  84.      * )
  85.      *
  86.      * @var ProductVersion|null
  87.      */
  88.     protected ?ProductVersion $productVersion null;
  89.     /**
  90.      * @param PriceList $priceList
  91.      * @param PricedInterface $product
  92.      * @param int $price
  93.      */
  94.     public function __construct(PriceList $priceListPricedInterface $productint $price)
  95.     {
  96.         $this->priceList $priceList;
  97.         if ($product instanceof Product) {
  98.             $this->setProduct($product);
  99.         } elseif ($product instanceof ProductVersion) {
  100.             $this->setProductVersion($product);
  101.         }
  102.         $this->setPrice($price);
  103.     }
  104.     /**
  105.      * @return string
  106.      */
  107.     public function __toString(): string
  108.     {
  109.         return $this->getFormatted();
  110.     }
  111.     /**
  112.      * @return string
  113.      */
  114.     public function getFormatted(): string
  115.     {
  116.         return number_format($this->price 1002','' ') . ' ' $this->priceList->getPriceGroup()->getCurrency();
  117.     }
  118.     /**
  119.      * @return int|null
  120.      */
  121.     public function getId(): ?int
  122.     {
  123.         return $this->id;
  124.     }
  125.     /**
  126.      * @return int
  127.      */
  128.     public function getPrice(): int
  129.     {
  130.         return $this->price;
  131.     }
  132.     /**
  133.      * @return PriceList
  134.      */
  135.     public function getPriceList(): PriceList
  136.     {
  137.         return $this->priceList;
  138.     }
  139.     /**
  140.      * @return Product|null
  141.      */
  142.     public function getProduct(): ?Product
  143.     {
  144.         return $this->product;
  145.     }
  146.     /**
  147.      * @return ProductVersion|null
  148.      */
  149.     public function getProductVersion(): ?ProductVersion
  150.     {
  151.         return $this->productVersion;
  152.     }
  153.     /**
  154.      * @param int $price
  155.      */
  156.     public function setPrice(int $price): void
  157.     {
  158.         $this->price $price;
  159.     }
  160.     /**
  161.      * @param PriceList $priceList
  162.      */
  163.     public function setPriceList(PriceList $priceList): void
  164.     {
  165.         $this->priceList $priceList;
  166.     }
  167.     /**
  168.      * @param Product $product
  169.      */
  170.     public function setProduct(Product $product): void
  171.     {
  172.         $this->product $product;
  173.         $this->productVersion null;
  174.     }
  175.     /**
  176.      * @param ProductVersion $productVersion
  177.      */
  178.     public function setProductVersion(ProductVersion $productVersion): void
  179.     {
  180.         $this->productVersion $productVersion;
  181.         $this->product null;
  182.     }
  183.     /**
  184.      * @Assert\Callback
  185.      *
  186.      * @param ExecutionContextInterface $context
  187.      */
  188.     public function validate(ExecutionContextInterface $context): void
  189.     {
  190.         if ($this->product === null && $this->productVersion === null) {
  191.             $context->buildViolation('error.product.none')
  192.                 ->atPath('product')
  193.                 ->addViolation();
  194.         } elseif ($this->product !== null && $this->productVersion !== null) {
  195.             $context->buildViolation('error.product.both')
  196.                 ->atPath('product')
  197.                 ->addViolation();
  198.         }
  199.     }
  200. }