<?phpnamespace App\Entity;use DateTime;use Doctrine\Common\Collections\ArrayCollection;use Doctrine\Common\Collections\Collection;use Doctrine\ORM\Mapping as ORM;use Symfony\Component\Validator\Constraints as Assert;/** * @ORM\Entity( * repositoryClass="App\Repository\PriceListRepository" * ) * @ORM\Table( * name="price_list", * options={"collate"="utf8_swedish_ci"} * ) * @ORM\HasLifecycleCallbacks */class PriceList implements EntityInterface{ /** * @ORM\Id * @ORM\Column( * type="integer" * ) * @ORM\GeneratedValue( * strategy="AUTO" * ) * * @var int|null */ protected ?int $id = null; /** * @ORM\ManyToOne( * targetEntity="PriceGroup", * inversedBy="priceLists" * ) * @ORM\JoinColumn( * name="price_group_id", * referencedColumnName="id", * onDelete="cascade" * ) * @Assert\NotNull * * @var PriceGroup */ protected PriceGroup $priceGroup; /** * @ORM\OneToMany( * targetEntity="Price", * mappedBy="priceList", * cascade={"persist","remove"} * ) * * @var Collection<Price> */ protected $prices; /** * @ORM\Column( * name="valid_from", * type="datetime" * ) * @Assert\NotNull * * @var DateTime */ protected DateTime $validFrom; /** * @param PriceGroup $priceGroup * @param DateTime $validFrom */ public function __construct(PriceGroup $priceGroup, DateTime $validFrom) { $this->priceGroup = $priceGroup; $this->validFrom = $validFrom; $this->prices = new ArrayCollection(); } /** * @param Price $price */ public function addPrice(Price $price): void { if ($this->prices === null) { $this->prices = new ArrayCollection(); } $price->setPriceList($this); $this->prices->add($price); } /** * @return int|null */ public function getId(): ?int { return $this->id; } /** * @param PricedInterface $product * * @return Price|null */ public function getPrice(PricedInterface $product): ?Price { foreach ($this->getPrices() as $price) { if ($product instanceof Product && ($priceProduct = $price->getProduct()) !== null) { if ($priceProduct->getCode() == $product->getCode()) { return $price; } } elseif ($product instanceof ProductVersion && ($priceProduct = $price->getProductVersion()) !== null) { if ($priceProduct->getCode() == $product->getCode()) { return $price; } } } return null; } /** * @return PriceGroup */ public function getPriceGroup(): PriceGroup { return $this->priceGroup; } /** * @return Price[] */ public function getPrices() { return $this->prices; } /** * @return DateTime */ public function getValidFrom(): DateTime { return $this->validFrom; }}