This repository was archived by the owner on Apr 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathFormConfigurationFactory.php
More file actions
179 lines (169 loc) · 6.31 KB
/
FormConfigurationFactory.php
File metadata and controls
179 lines (169 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
namespace Codete\FormGeneratorBundle;
use Codete\FormGeneratorBundle\Annotations\Field;
use Codete\FormGeneratorBundle\Annotations\Form;
use Codete\FormGeneratorBundle\Form\Type\EmbedType;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\Reader;
use Doctrine\Instantiator\Instantiator;
/**
* FormConfigurationFactory creates initial form configuration that is adjusted later.
*
* @author Maciej Malarz <malarzm@gmail.com>
*/
class FormConfigurationFactory
{
/**
* @var AdjusterRegistry
*/
private $adjusterRegistry;
/**
* @var Reader
*/
private $annotationReader;
/**
* @var Instantiator
*/
private $instantiator;
/**
* @param AdjusterRegistry $adjusterRegistry
*/
public function __construct(AdjusterRegistry $adjusterRegistry)
{
$this->adjusterRegistry = $adjusterRegistry;
$this->annotationReader = new AnnotationReader();
$this->instantiator = new Instantiator();
}
/**
* Generates initial form configuration.
*
* @param string $form
* @param object $model
* @param array $context
* @return array
*/
public function getConfiguration($form, $model, $context)
{
$fields = null;
foreach ($this->adjusterRegistry->getFormViewProviders() as $provider) {
if ($provider->supports($form, $model, $context)) {
$fields = $provider->getFields($model, $context);
break;
}
}
if ($fields === null) {
$fields = $this->getFields($model, $form);
}
$fields = $this->normalizeFields($fields);
return $this->getFieldsConfiguration($model, $fields);
}
/**
* Creates form configuration for $model for given $fields.
*
* @param object $model
* @param array $fields
* @return array
*/
private function getFieldsConfiguration($model, $fields = [])
{
$configuration = $properties = [];
$ro = new \ReflectionObject($model);
if (empty($fields)) {
$properties = $ro->getProperties();
} else {
foreach (array_keys($fields) as $field) {
// setting the configuration to null guarantees the order of elements in there
// to match the order specified in $fields
$configuration[$field] = null;
if (! $ro->hasProperty($field)) {
continue; // most prob a class-level field, order will be maintained due to trick above
}
$properties[] = $ro->getProperty($field);
}
}
$fieldConfigurations = [];
// first are coming properties
foreach ($properties as $property) {
$propertyName = $property->getName();
$propertyIsListed = array_key_exists($propertyName, $fields);
if (!empty($fields) && !$propertyIsListed) {
continue; // list of fields was specified and current one is not there
}
$fieldConfiguration = $this->annotationReader->getPropertyAnnotation($property, Field::class);
if ($fieldConfiguration === null && !$propertyIsListed) {
continue;
}
$fieldConfigurations[$propertyName] = $fieldConfiguration;
}
// later are coming class-level fields. We need to iterate through all annotations as there's no method
// to get *all* occurrences of chosen annotation.
foreach ($this->annotationReader->getClassAnnotations($ro) as $annotation) {
if (! $annotation instanceof Field) {
continue;
}
$propertyName = $annotation->value;
if (!empty($fields) && !array_key_exists($propertyName, $fields)) {
continue; // list of fields was specified and current one is not there
}
$fieldConfigurations[$propertyName] = $annotation;
}
foreach ($fieldConfigurations as $propertyName => $fieldConfiguration) {
$configuration[$propertyName] = (array)$fieldConfiguration;
if (isset($fields[$propertyName])) {
$configuration[$propertyName] = array_replace_recursive($configuration[$propertyName], $fields[$propertyName]);
}
if ($configuration[$propertyName]['type'] === EmbedType::class) {
if (! $ro->hasProperty($propertyName) || ($value = $ro->getProperty($propertyName)->getValue($model)) === null) {
$value = $this->instantiator->instantiate($configuration[$propertyName]['class']);
}
$configuration[$propertyName]['data_class'] = $configuration[$propertyName]['class'];
$configuration[$propertyName]['model'] = $value;
}
// this variable comes from Doctrine\Common\Annotations\Annotation
unset($configuration[$propertyName]['value']);
}
return $configuration;
}
/**
* Gets field list from $model basing on its Form annotation.
*
* @param object $model
* @param string $form view
* @return array list of fields (or empty for all fields)
*/
private function getFields($model, $form)
{
$ro = new \ReflectionObject($model);
$formAnnotation = $this->annotationReader->getClassAnnotation($ro, Form::class);
if (($formAnnotation === null || !$formAnnotation->hasForm($form)) && $ro->getParentClass()) {
while ($ro = $ro->getParentClass()) {
$formAnnotation = $this->annotationReader->getClassAnnotation($ro, Form::class);
if ($formAnnotation !== null && $formAnnotation->hasForm($form)) {
break;
}
}
}
if ($formAnnotation === null) {
$formAnnotation = new Annotations\Form([]);
}
return $formAnnotation->getForm($form);
}
/**
* Normalizes $fields array.
*
* @param array $_fields
* @return array
*/
private function normalizeFields($_fields)
{
$fields = [];
foreach ($_fields as $key => $value) {
if (is_array($value)) {
$fields[$key] = $value;
} else {
$fields[$value] = [];
}
}
return $fields;
}
}