| @@ 14-90 (lines=77) @@ | ||
| 11 | use Symfony\Component\OptionsResolver\Options; |
|
| 12 | use Symfony\Component\OptionsResolver\OptionsResolver; |
|
| 13 | ||
| 14 | final class ProductBlockExampleFactory implements ExampleFactoryInterface |
|
| 15 | { |
|
| 16 | /** |
|
| 17 | * @var FactoryInterface |
|
| 18 | */ |
|
| 19 | private $factory; |
|
| 20 | ||
| 21 | /** |
|
| 22 | * @var \Faker\Generator |
|
| 23 | */ |
|
| 24 | private $faker; |
|
| 25 | ||
| 26 | /** |
|
| 27 | * @var OptionsResolver |
|
| 28 | */ |
|
| 29 | private $optionsResolver; |
|
| 30 | ||
| 31 | /** |
|
| 32 | * @var RepositoryInterface |
|
| 33 | */ |
|
| 34 | private $productRepository; |
|
| 35 | ||
| 36 | /** |
|
| 37 | * @param FactoryInterface $factory |
|
| 38 | * @param RepositoryInterface $productRepository |
|
| 39 | */ |
|
| 40 | public function __construct(FactoryInterface $factory, RepositoryInterface $productRepository) |
|
| 41 | { |
|
| 42 | $this->factory = $factory; |
|
| 43 | $this->productRepository = $productRepository; |
|
| 44 | ||
| 45 | $this->faker = \Faker\Factory::create(); |
|
| 46 | $this->optionsResolver = |
|
| 47 | (new OptionsResolver()) |
|
| 48 | ->setDefault('name', function (Options $options) { |
|
| 49 | return StringInflector::nameToCode($options['title']); |
|
| 50 | }) |
|
| 51 | ->setAllowedTypes('name', 'string') |
|
| 52 | ||
| 53 | ->setDefault('title', function (Options $options) { |
|
| 54 | return $this->faker->words(3, true); |
|
| 55 | }) |
|
| 56 | ->setAllowedTypes('title', 'string') |
|
| 57 | ||
| 58 | ->setDefault('body', function (Options $options) { |
|
| 59 | return $this->faker->sentence(); |
|
| 60 | }) |
|
| 61 | ->setAllowedTypes('body', 'string') |
|
| 62 | ||
| 63 | ->setDefault('link', function (Options $options) { |
|
| 64 | return $this->faker->url; |
|
| 65 | }) |
|
| 66 | ->setAllowedTypes('link', 'string') |
|
| 67 | ||
| 68 | ->setDefault('product', LazyOption::randomOne($this->productRepository)) |
|
| 69 | ->setNormalizer('product', LazyOption::findOneBy($this->productRepository, 'code')) |
|
| 70 | ; |
|
| 71 | } |
|
| 72 | ||
| 73 | /** |
|
| 74 | * {@inheritdoc} |
|
| 75 | */ |
|
| 76 | public function create(array $options = []) |
|
| 77 | { |
|
| 78 | $options = $this->optionsResolver->resolve($options); |
|
| 79 | ||
| 80 | /** @var ProductBlock $stringBlock */ |
|
| 81 | $stringBlock = $this->factory->createNew(); |
|
| 82 | $stringBlock->setName($options['name']); |
|
| 83 | $stringBlock->setTitle($options['name']); |
|
| 84 | $stringBlock->setBody($options['body']); |
|
| 85 | $stringBlock->setLinkUrl($options['link']); |
|
| 86 | $stringBlock->setProduct($options['product']); |
|
| 87 | ||
| 88 | return $stringBlock; |
|
| 89 | } |
|
| 90 | } |
|
| 91 | ||
| @@ 14-90 (lines=77) @@ | ||
| 11 | use Symfony\Component\OptionsResolver\Options; |
|
| 12 | use Symfony\Component\OptionsResolver\OptionsResolver; |
|
| 13 | ||
| 14 | final class TaxonBlockExampleFactory implements ExampleFactoryInterface |
|
| 15 | { |
|
| 16 | /** |
|
| 17 | * @var FactoryInterface |
|
| 18 | */ |
|
| 19 | private $factory; |
|
| 20 | ||
| 21 | /** |
|
| 22 | * @var \Faker\Generator |
|
| 23 | */ |
|
| 24 | private $faker; |
|
| 25 | ||
| 26 | /** |
|
| 27 | * @var OptionsResolver |
|
| 28 | */ |
|
| 29 | private $optionsResolver; |
|
| 30 | ||
| 31 | /** |
|
| 32 | * @var RepositoryInterface |
|
| 33 | */ |
|
| 34 | private $taxonRepository; |
|
| 35 | ||
| 36 | /** |
|
| 37 | * @param FactoryInterface $factory |
|
| 38 | * @param RepositoryInterface $taxonRepository |
|
| 39 | */ |
|
| 40 | public function __construct(FactoryInterface $factory, RepositoryInterface $taxonRepository) |
|
| 41 | { |
|
| 42 | $this->factory = $factory; |
|
| 43 | $this->taxonRepository = $taxonRepository; |
|
| 44 | ||
| 45 | $this->faker = \Faker\Factory::create(); |
|
| 46 | $this->optionsResolver = |
|
| 47 | (new OptionsResolver()) |
|
| 48 | ->setDefault('name', function (Options $options) { |
|
| 49 | return StringInflector::nameToCode($options['title']); |
|
| 50 | }) |
|
| 51 | ->setAllowedTypes('name', 'string') |
|
| 52 | ||
| 53 | ->setDefault('title', function (Options $options) { |
|
| 54 | return $this->faker->words(3, true); |
|
| 55 | }) |
|
| 56 | ->setAllowedTypes('title', 'string') |
|
| 57 | ||
| 58 | ->setDefault('body', function (Options $options) { |
|
| 59 | return $this->faker->sentence(); |
|
| 60 | }) |
|
| 61 | ->setAllowedTypes('body', 'string') |
|
| 62 | ||
| 63 | ->setDefault('link', function (Options $options) { |
|
| 64 | return $this->faker->url; |
|
| 65 | }) |
|
| 66 | ->setAllowedTypes('link', 'string') |
|
| 67 | ||
| 68 | ->setDefault('taxon', LazyOption::randomOne($this->taxonRepository)) |
|
| 69 | ->setNormalizer('taxon', LazyOption::findOneBy($this->taxonRepository, 'code')) |
|
| 70 | ; |
|
| 71 | } |
|
| 72 | ||
| 73 | /** |
|
| 74 | * {@inheritdoc} |
|
| 75 | */ |
|
| 76 | public function create(array $options = []) |
|
| 77 | { |
|
| 78 | $options = $this->optionsResolver->resolve($options); |
|
| 79 | ||
| 80 | /** @var TaxonBlock $stringBlock */ |
|
| 81 | $stringBlock = $this->factory->createNew(); |
|
| 82 | $stringBlock->setName($options['name']); |
|
| 83 | $stringBlock->setTitle($options['name']); |
|
| 84 | $stringBlock->setBody($options['body']); |
|
| 85 | $stringBlock->setLinkUrl($options['link']); |
|
| 86 | $stringBlock->setTaxon($options['taxon']); |
|
| 87 | ||
| 88 | return $stringBlock; |
|
| 89 | } |
|
| 90 | } |
|
| 91 | ||