src/Factory/Products/ProductFactory.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Factory\Products;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use App\Entity\Products\Product;
  5. class ProductFactory
  6. {
  7.     public function __construct(EntityManagerInterface $entityManager)
  8.     {
  9.         $this->em $entityManager;
  10.     }
  11.     public function create(array $defaults = array()): Product
  12.     {
  13.         // Create instance
  14.         $product = new Product();
  15.         // Loop defaults
  16.         foreach($defaults AS $k => $v)
  17.         {
  18.             // Format setter method
  19.             $setterName "set" ucfirst($k);
  20.             // If the setter method exists
  21.             if(method_exists($product$setterName))
  22.                 $product->$setterName($v);
  23.         }
  24.         return $product;
  25.     }
  26.     public function canBeDeleted(Product $product): bool
  27.     {
  28.         return !$product->getIsSpecial();
  29.     }
  30. }