@@ 12-77 (lines=66) @@ | ||
9 | use Symfony\Component\OptionsResolver\Options; |
|
10 | use Symfony\Component\OptionsResolver\OptionsResolver; |
|
11 | ||
12 | final class CustomBlockExampleFactory implements ExampleFactoryInterface |
|
13 | { |
|
14 | /** |
|
15 | * @var FactoryInterface |
|
16 | */ |
|
17 | private $factory; |
|
18 | ||
19 | /** |
|
20 | * @var \Faker\Generator |
|
21 | */ |
|
22 | private $faker; |
|
23 | ||
24 | /** |
|
25 | * @var OptionsResolver |
|
26 | */ |
|
27 | private $optionsResolver; |
|
28 | ||
29 | /** |
|
30 | * @param FactoryInterface $factory |
|
31 | */ |
|
32 | public function __construct(FactoryInterface $factory) |
|
33 | { |
|
34 | $this->factory = $factory; |
|
35 | ||
36 | $this->faker = \Faker\Factory::create(); |
|
37 | $this->optionsResolver = |
|
38 | (new OptionsResolver()) |
|
39 | ->setDefault('name', function (Options $options) { |
|
40 | return StringInflector::nameToCode($options['title']); |
|
41 | }) |
|
42 | ->setAllowedTypes('name', 'string') |
|
43 | ||
44 | ->setDefault('title', function (Options $options) { |
|
45 | return $this->faker->words(3, true); |
|
46 | }) |
|
47 | ->setAllowedTypes('title', 'string') |
|
48 | ||
49 | ->setDefault('body', function (Options $options) { |
|
50 | return $this->faker->sentence(); |
|
51 | }) |
|
52 | ->setAllowedTypes('body', 'string') |
|
53 | ||
54 | ->setDefault('link', function (Options $options) { |
|
55 | return $this->faker->url; |
|
56 | }) |
|
57 | ->setAllowedTypes('link', 'string') |
|
58 | ; |
|
59 | } |
|
60 | ||
61 | /** |
|
62 | * {@inheritdoc} |
|
63 | */ |
|
64 | public function create(array $options = []) |
|
65 | { |
|
66 | $options = $this->optionsResolver->resolve($options); |
|
67 | ||
68 | /** @var CustomBlock $stringBlock */ |
|
69 | $stringBlock = $this->factory->createNew(); |
|
70 | $stringBlock->setName($options['name']); |
|
71 | $stringBlock->setTitle($options['name']); |
|
72 | $stringBlock->setBody($options['body']); |
|
73 | $stringBlock->setLinkUrl($options['link']); |
|
74 | ||
75 | return $stringBlock; |
|
76 | } |
|
77 | } |
|
78 |
@@ 12-77 (lines=66) @@ | ||
9 | use Symfony\Component\OptionsResolver\Options; |
|
10 | use Symfony\Component\OptionsResolver\OptionsResolver; |
|
11 | ||
12 | final class StaticContentExampleFactory implements ExampleFactoryInterface |
|
13 | { |
|
14 | /** |
|
15 | * @var FactoryInterface |
|
16 | */ |
|
17 | private $staticContentFactory; |
|
18 | ||
19 | /** |
|
20 | * @var \Faker\Generator |
|
21 | */ |
|
22 | private $faker; |
|
23 | ||
24 | /** |
|
25 | * @var OptionsResolver |
|
26 | */ |
|
27 | private $optionsResolver; |
|
28 | ||
29 | /** |
|
30 | * @param FactoryInterface $staticContentFactory |
|
31 | */ |
|
32 | public function __construct(FactoryInterface $staticContentFactory) |
|
33 | { |
|
34 | $this->staticContentFactory = $staticContentFactory; |
|
35 | ||
36 | $this->faker = \Faker\Factory::create(); |
|
37 | $this->optionsResolver = |
|
38 | (new OptionsResolver()) |
|
39 | ->setDefault('title', function (Options $options) { |
|
40 | return $this->faker->words(3, true); |
|
41 | }) |
|
42 | ->setAllowedTypes('title', 'string') |
|
43 | ||
44 | ->setDefault('name', function (Options $options) { |
|
45 | return StringInflector::nameToCode($options['title']); |
|
46 | }) |
|
47 | ->setAllowedTypes('name', 'string') |
|
48 | ||
49 | ->setDefault('body', function (Options $options) { |
|
50 | return $this->faker->paragraphs(4, true); |
|
51 | }) |
|
52 | ->setAllowedTypes('body', 'string') |
|
53 | ||
54 | ->setDefault('publishable', function (Options $options) { |
|
55 | return $this->faker->boolean(90); |
|
56 | }) |
|
57 | ->setAllowedTypes('publishable', 'bool') |
|
58 | ; |
|
59 | } |
|
60 | ||
61 | /** |
|
62 | * {@inheritdoc} |
|
63 | */ |
|
64 | public function create(array $options = []) |
|
65 | { |
|
66 | $options = $this->optionsResolver->resolve($options); |
|
67 | ||
68 | /** @var StaticContent $staticContent */ |
|
69 | $staticContent = $this->staticContentFactory->createNew(); |
|
70 | $staticContent->setTitle($options['title']); |
|
71 | $staticContent->setName($options['name']); |
|
72 | $staticContent->setBody($options['body']); |
|
73 | $staticContent->setPublishable($options['publishable']); |
|
74 | ||
75 | return $staticContent; |
|
76 | } |
|
77 | } |
|
78 |