|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare (strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace HMLB\DDDBundle\Form\Type; |
|
6
|
|
|
|
|
7
|
|
|
use HMLB\DDD\Message\Command\Command; |
|
8
|
|
|
use ReflectionClass; |
|
9
|
|
|
use Symfony\Component\Form\AbstractType; |
|
10
|
|
|
use Symfony\Component\Form\FormInterface; |
|
11
|
|
|
use Symfony\Component\OptionsResolver\Options; |
|
12
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* CommandType. |
|
16
|
|
|
* |
|
17
|
|
|
* @author Hugues Maignol <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class CommandType extends AbstractType |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* The class name of the Command. |
|
23
|
|
|
* |
|
24
|
|
|
* @return string |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract protected function getCommandClass(); |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Override this to pre-populate the default options of the OptionsResolver. |
|
30
|
|
|
* |
|
31
|
|
|
* @return array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function getDefaultOptions() |
|
34
|
|
|
{ |
|
35
|
|
|
return []; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param FormInterface $form |
|
40
|
|
|
* @param Options $options |
|
41
|
|
|
* |
|
42
|
|
|
* @return Command |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function createCommandFromData(FormInterface $form, Options $options) |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
|
|
$reflection = new ReflectionClass($this->getCommandClass()); |
|
47
|
|
|
$data = []; |
|
48
|
|
|
/** @var FormInterface $field */ |
|
49
|
|
|
foreach ($form as $field) { |
|
50
|
|
|
$data[$field->getName()] = $field->getData(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $reflection->newInstanceArgs($data); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param OptionsResolver $resolver |
|
58
|
|
|
*/ |
|
59
|
|
|
public function configureOptions(OptionsResolver $resolver) |
|
60
|
|
|
{ |
|
61
|
|
|
$setup = [ |
|
62
|
|
|
'data_class' => 'OH\Core\Command\AskQuestion', |
|
63
|
|
|
'empty_data' => function (Options $options) { |
|
64
|
|
|
return function (FormInterface $form) use ($options) { |
|
65
|
|
|
return $this->createCommandFromData($form, $options); |
|
66
|
|
|
}; |
|
67
|
|
|
}, |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
|
|
$merged = array_merge($setup, $this->getDefaultOptions()); |
|
71
|
|
|
|
|
72
|
|
|
$resolver->setDefaults($merged); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getBlockPrefix() |
|
79
|
|
|
{ |
|
80
|
|
|
return call_user_func($this->getCommandClass().'::messageName'); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.