|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file contains a form creator for Models |
|
4
|
|
|
* |
|
5
|
|
|
* @license https://github.com/allejo/bzion/blob/master/LICENSE.md GNU General Public License Version 3 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace BZIon\Form\Creator; |
|
9
|
|
|
|
|
10
|
|
|
use Symfony\Component\Form\Form; |
|
11
|
|
|
use Symfony\Component\Form\FormBuilder; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* A form creator for Models which cooperates with the CRUDController |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class ModelFormCreator implements FormCreatorInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* The model that the form is editing |
|
20
|
|
|
* @var \Model|null |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $editing; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The user who is submitting the form |
|
26
|
|
|
* @var \Player |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $me; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The controller showing the form |
|
32
|
|
|
* @var \Controller|null |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $controller; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Create a new ModelFormCreator |
|
38
|
|
|
* |
|
39
|
|
|
* @param \Model|null $editing The model that's being edited |
|
40
|
|
|
* @param \Player|null $me The user who is submitting the form |
|
41
|
|
|
* @param \Controller|null $controller The controller showing the form |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function __construct($editing = null, $me = null, $controller = null) |
|
44
|
|
|
{ |
|
45
|
1 |
|
$this->editing = $editing; |
|
46
|
1 |
|
$this->me = ($me) ?: \Player::invalid(); |
|
47
|
1 |
|
$this->controller = $controller; |
|
48
|
1 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Build a form |
|
52
|
|
|
* |
|
53
|
|
|
* @param FormBuilder $builder Symfony's form builder |
|
54
|
|
|
* @return FormBuilder |
|
55
|
|
|
*/ |
|
56
|
|
|
abstract protected function build($builder); |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function create() |
|
62
|
|
|
{ |
|
63
|
1 |
|
$builder = \Service::getFormFactory()->createNamedBuilder( |
|
64
|
1 |
|
$this->getName(), |
|
65
|
1 |
|
'form', |
|
66
|
1 |
|
null, |
|
67
|
1 |
|
$this->getFormOptions() |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
1 |
|
$form = $this->build($builder)->getForm(); |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
1 |
|
if ($this->editing) { |
|
73
|
1 |
|
$this->fill($form, $this->editing); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
return $form; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Fill the form with a model's data |
|
81
|
|
|
* |
|
82
|
|
|
* Override this in your form |
|
83
|
|
|
* |
|
84
|
|
|
* @param Form $form The form to fill |
|
85
|
|
|
* @param \Model $model The model to provide the data to use |
|
86
|
|
|
* @return void |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public function fill($form, $model) |
|
89
|
|
|
{ |
|
90
|
1 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Update a model based on a form's data |
|
94
|
|
|
* |
|
95
|
|
|
* Override this in your form |
|
96
|
|
|
* |
|
97
|
|
|
* @param Form $form The form to use |
|
98
|
|
|
* @param \Model $model The model to update |
|
99
|
|
|
* @return void |
|
100
|
|
|
*/ |
|
101
|
|
|
public function update($form, $model) |
|
102
|
|
|
{ |
|
103
|
|
|
throw new BadMethodCallException("Please override the update() method in the FormCreator class for the model"); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Enter the data of a valid form into the database |
|
108
|
|
|
* @param Form $form The submitted form |
|
109
|
|
|
* @return \Model |
|
110
|
|
|
*/ |
|
111
|
|
|
public function enter($form) |
|
112
|
|
|
{ |
|
113
|
|
|
throw new BadMethodCallException("Please override the enter() method in the FormCreator class for the model"); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Are we editing or creating a model? |
|
118
|
|
|
* @return bool True if editing, false when creating a model |
|
119
|
|
|
*/ |
|
120
|
1 |
|
protected function isEdit() |
|
121
|
|
|
{ |
|
122
|
1 |
|
return (bool) $this->editing; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Get the options for the form |
|
127
|
|
|
* @return array |
|
128
|
|
|
*/ |
|
129
|
1 |
|
protected function getFormOptions() |
|
130
|
|
|
{ |
|
131
|
1 |
|
return array(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Get the name of the form |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
1 |
|
protected function getName() |
|
139
|
|
|
{ |
|
140
|
1 |
|
return 'form'; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.