vendor/sylius/sylius/src/Sylius/Bundle/CoreBundle/Fixture/TshirtProductFixture.php line 19

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\CoreBundle\Fixture;
  12. use Faker\Factory;
  13. use Faker\Generator;
  14. @trigger_error('The "TshirtProductFixture" class is deprecated since Sylius 1.5 Use new product fixtures class located at "src/Sylius/Bundle/CoreBundle/Fixture/" instead.'\E_USER_DEPRECATED);
  15. use Sylius\Bundle\FixturesBundle\Fixture\AbstractFixture;
  16. use Sylius\Component\Attribute\AttributeType\TextAttributeType;
  17. use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
  18. use Symfony\Component\OptionsResolver\OptionsResolver;
  19. class TshirtProductFixture extends AbstractFixture
  20. {
  21.     private Generator $faker;
  22.     private OptionsResolver $optionsResolver;
  23.     public function __construct(
  24.         private AbstractResourceFixture $taxonFixture,
  25.         private AbstractResourceFixture $productAttributeFixture,
  26.         private AbstractResourceFixture $productOptionFixture,
  27.         private AbstractResourceFixture $productFixture,
  28.     ) {
  29.         $this->faker Factory::create();
  30.         $this->optionsResolver =
  31.             (new OptionsResolver())
  32.                 ->setRequired('amount')
  33.                 ->setAllowedTypes('amount''int')
  34.         ;
  35.     }
  36.     public function getName(): string
  37.     {
  38.         return 'tshirt_product';
  39.     }
  40.     public function load(array $options): void
  41.     {
  42.         $options $this->optionsResolver->resolve($options);
  43.         $this->taxonFixture->load(['custom' => [[
  44.             'code' => 'category',
  45.             'name' => 'Category',
  46.             'children' => [
  47.                 [
  48.                     'code' => 't_shirts',
  49.                     'name' => 'T-Shirts',
  50.                     'slug' => 't-shirts',
  51.                     'children' => [
  52.                         [
  53.                             'code' => 'mens_t_shirts',
  54.                             'translations' => [
  55.                                 'en_US' => [
  56.                                     'name' => 'Men',
  57.                                     'slug' => 't-shirts/men',
  58.                                 ],
  59.                                 'fr_FR' => [
  60.                                     'name' => 'Hommes',
  61.                                     'slug' => 't-shirts/hommes',
  62.                                 ],
  63.                             ],
  64.                         ],
  65.                         [
  66.                             'code' => 'womens_t_shirts',
  67.                             'translations' => [
  68.                                 'en_US' => [
  69.                                     'name' => 'Women',
  70.                                     'slug' => 't-shirts/women',
  71.                                 ],
  72.                                 'fr_FR' => [
  73.                                     'name' => 'Hommes',
  74.                                     'slug' => 't-shirts/femmes',
  75.                                 ],
  76.                             ],
  77.                         ],
  78.                     ],
  79.                 ],
  80.             ],
  81.         ]]]);
  82.         $this->productAttributeFixture->load(['custom' => [
  83.             ['name' => 'T-Shirt brand''code' => 't_shirt_brand''type' => TextAttributeType::TYPE],
  84.             ['name' => 'T-Shirt collection''code' => 't_shirt_collection''type' => TextAttributeType::TYPE],
  85.             ['name' => 'T-Shirt material''code' => 't_shirt_material''type' => TextAttributeType::TYPE],
  86.         ]]);
  87.         $this->productOptionFixture->load(['custom' => [
  88.             [
  89.                 'name' => 'T-Shirt color',
  90.                 'code' => 't_shirt_color',
  91.                 'values' => [
  92.                     't_shirt_color_red' => 'Red',
  93.                     't_shirt_color_black' => 'Black',
  94.                     't_shirt_color_white' => 'White',
  95.                 ],
  96.             ],
  97.             [
  98.                 'name' => 'T-Shirt size',
  99.                 'code' => 't_shirt_size',
  100.                 'values' => [
  101.                     't_shirt_size_s' => 'S',
  102.                     't_shirt_size_m' => 'M',
  103.                     't_shirt_size_l' => 'L',
  104.                     't_shirt_size_xl' => 'XL',
  105.                     't_shirt_size_xxl' => 'XXL',
  106.                 ],
  107.             ],
  108.         ]]);
  109.         $products = [];
  110.         $productsNames $this->getUniqueNames($options['amount']);
  111.         for ($i 0$i $options['amount']; ++$i) {
  112.             $categoryTaxonCode $this->faker->randomElement(['mens_t_shirts''womens_t_shirts']);
  113.             $products[] = [
  114.                 'name' => sprintf('T-Shirt "%s"'$productsNames[$i]),
  115.                 'code' => $this->faker->uuid,
  116.                 'main_taxon' => $categoryTaxonCode,
  117.                 'taxons' => ['t_shirts'$categoryTaxonCode],
  118.                 'product_attributes' => [
  119.                     't_shirt_brand' => $this->faker->randomElement(['Nike''Adidas''JKM-476 Streetwear''Potato''Centipede Wear']),
  120.                     't_shirt_collection' => sprintf('Sylius %s %s'$this->faker->randomElement(['Summer''Winter''Spring''Autumn']), random_int(19952012)),
  121.                     't_shirt_material' => $this->faker->randomElement(['Centipede''Wool''Centipede 10% / Wool 90%''Potato 100%']),
  122.                 ],
  123.                 'product_options' => ['t_shirt_color''t_shirt_size'],
  124.                 'images' => [
  125.                     [
  126.                         'path' => sprintf('%s/../Resources/fixtures/%s'__DIR__'t-shirts.jpg'),
  127.                         'type' => 'main',
  128.                     ],
  129.                     [
  130.                         'path' => sprintf('%s/../Resources/fixtures/%s'__DIR__'t-shirts.jpg'),
  131.                         'type' => 'thumbnail',
  132.                     ],
  133.                 ],
  134.             ];
  135.         }
  136.         $this->productFixture->load(['custom' => $products]);
  137.     }
  138.     protected function configureOptionsNode(ArrayNodeDefinition $optionsNode): void
  139.     {
  140.         $optionsNode
  141.             ->children()
  142.                 ->integerNode('amount')->isRequired()->min(0)->end()
  143.         ;
  144.     }
  145.     private function getUniqueNames(int $amount): array
  146.     {
  147.         $productsNames = [];
  148.         for ($i 0$i $amount; ++$i) {
  149.             $name $this->faker->word;
  150.             while (in_array($name$productsNames)) {
  151.                 $name $this->faker->word;
  152.             }
  153.             $productsNames[] = $name;
  154.         }
  155.         return $productsNames;
  156.     }
  157. }