@@ -11,36 +11,42 @@ type guessers.
11
11
12
12
.. sidebar :: Form Type Guessers in the Bridges
13
13
14
- Symfony also provides some form type guessers in the bridges. These can be
15
- used if you use that library.
14
+ Symfony also provides some form type guessers in the bridges:
16
15
17
16
* :class: `Symfony\\ Bridge\\ Propel1\\ Form\\ PropelTypeGuesser ` provided by
18
17
the Propel1 bridge;
19
18
* :class: `Symfony\\ Bridge\\ Doctrine\\ Form\\ DoctrineOrmTypeGuesser `
20
19
provided by the Doctrine bridge.
21
20
22
- A PHPDoc Type Guesser
23
- ---------------------
21
+ Create a PHPDoc Type Guesser
22
+ ----------------------------
24
23
25
- In this section, you are going to build a PHPDoc type guesser. At first, you
26
- need to create a class which extends
27
- :class: `Symfony\\ Component\\ Form\\ FormTypeGuesserInterface `. This interface
28
- requires 4 methods:
24
+ In this section, you are going to build a guesser that reads information about
25
+ fields from the PHPDoc of the properties. At first, you need to create a class
26
+ which implements :class: `Symfony\\ Component\\ Form\\ FormTypeGuesserInterface `.
27
+ This interface requires 4 methods:
29
28
30
29
* :method: `Symfony\\ Component\\ Form\\ FormTypeGuesserInterface::guessType ` -
31
30
tries to guess the type of a field;
32
31
* :method: `Symfony\\ Component\\ Form\\ FormTypeGuesserInterface::guessRequired ` -
33
- tries to guess the value of the ``required `` option;
32
+ tries to guess the value of the :ref: `required <reference-form-option-required >`
33
+ option;
34
34
* :method: `Symfony\\ Component\\ Form\\ FormTypeGuesserInterface::guessMaxLength ` -
35
- tries to guess the value of the ``max_length `` option;
35
+ tries to guess the value of the :ref: `max_length <reference-form-option-max_length >`
36
+ option;
36
37
* :method: `Symfony\\ Component\\ Form\\ FormTypeGuesserInterface::guessPattern ` -
37
- tries to guess the value of the ``pattern `` option.
38
+ tries to guess the value of the :ref: `pattern <reference-form-option-pattern >`
39
+ option.
38
40
39
- The most basic class looks like::
41
+ Start by creating the class and these methods. Next, you'll learn how to fill each on.
42
+
43
+ .. code-block :: php
44
+
45
+ namespace Acme\Form;
40
46
41
47
use Symfony\Component\Form\FormTypeGuesserInterface;
42
48
43
- class PHPDocTypeGuesser implements FormTypeGuesserInterface
49
+ class PhpdocTypeGuesser implements FormTypeGuesserInterface
44
50
{
45
51
public function guessType($class, $property)
46
52
{
@@ -69,54 +75,70 @@ that the type guesser cannot guess the type.
69
75
The ``TypeGuess `` constructor requires 3 options:
70
76
71
77
* The type name (one of the :doc: `form types </reference/forms/types `);
72
- * Additionally options (for instance, when the type is ``entity ``, you also
78
+ * Additional options (for instance, when the type is ``entity ``, you also
73
79
want to set the ``class `` option). If no types are guessed, this should be
74
80
set to an empty array;
75
81
* The confidence that the guessed type is correct. This can be one of the
76
82
constants of the :class: `Symfony\\ Component\\ Form\\ Guess\G uess ` class:
77
83
``LOW_CONFIDENCE ``, ``MEDIUM_CONFIDENCE ``, ``HIGH_CONFIDENCE ``,
78
- ``VERY_HIGH_CONFIDENCE ``. After all type guessers are executed, the type
79
- with the highest confidence is used.
84
+ ``VERY_HIGH_CONFIDENCE ``. After all type guessers have been executed, the
85
+ type with the highest confidence is used.
80
86
81
87
With this knowledge, you can easily implement the ``guessType `` method of the
82
88
``PHPDocTypeGuesser ``::
83
89
90
+ namespace Acme\Form;
91
+
84
92
use Symfony\Component\Form\Guess\Guess;
85
93
use Symfony\Component\Form\Guess\TypeGuess;
86
94
87
- // ...
88
- public function guessType($class, $property)
95
+ class PhpdocTypeGuesser implements FormTypeGuesserInterface
89
96
{
90
- $annotations = $this->readPhpDocAnnotations($class, $property);
91
-
92
- if (!isset($annotations['var'])) {
93
- return; // guess nothing if the @var annotation is not available
97
+ public function guessType($class, $property)
98
+ {
99
+ $annotations = $this->readPhpDocAnnotations($class, $property);
100
+
101
+ if (!isset($annotations['var'])) {
102
+ return; // guess nothing if the @var annotation is not available
103
+ }
104
+
105
+ // otherwise, base the type on the @var annotation
106
+ switch ($annotations['var']) {
107
+ case 'string':
108
+ // there is a high confidence that the type is a string when
109
+ // @var string is used
110
+ return new TypeGuess('text', array(), Guess::HIGH_CONFIDENCE);
111
+
112
+ case 'int':
113
+ case 'integer':
114
+ // integers can also be the id of an entity or a checkbox (0 or 1)
115
+ return new TypeGuess('integer', array(), Guess::MEDIUM_CONFIDENCE);
116
+
117
+ case 'float':
118
+ case 'double':
119
+ case 'real':
120
+ return new TypeGuess('number', array(), Guess::MEDIUM_CONFIDENCE);
121
+
122
+ case 'boolean':
123
+ case 'bool':
124
+ return new TypeGuess('checkbox', array(), Guess::HIGH_CONFIDENCE);
125
+
126
+ default:
127
+ // there is a very low confidence that this one is correct
128
+ return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
129
+ }
94
130
}
95
131
96
- // otherwise, base the type on the @var annotation
97
- switch ($annotations['var']) {
98
- case 'string':
99
- // there is a high confidence that the type is a string when
100
- // @var string is used
101
- return new TypeGuess('text', array(), Guess::HIGH_CONFIDENCE);
102
-
103
- case 'int':
104
- case 'integer':
105
- // integers can also be the id of an entity or a checkbox (0 or 1)
106
- return new TypeGuess('integer', array(), Guess::MEDIUM_CONFIDENCE);
107
-
108
- case 'float':
109
- case 'double':
110
- case 'real':
111
- return new TypeGuess('number', array(), Guess::MEDIUM_CONFIDENCE);
112
-
113
- case 'boolean':
114
- case 'bool':
115
- return new TypeGuess('checkbox', array(), Guess::HIGH_CONFIDENCE);
116
-
117
- default:
118
- // there is a very low confidence that this one is correct
119
- return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
132
+ protected function readPhpDocAnnotations($class, $property)
133
+ {
134
+ $reflectionProperty = new \ReflectionProperty($class, $property);
135
+ $phpdoc = $reflectionProperty->getDocComment();
136
+
137
+ // parse the $phpdoc into an array like:
138
+ // array('type' => 'string', 'since' => '1.0')
139
+ $phpdocTags = ...;
140
+
141
+ return $phpdocTags;
120
142
}
121
143
}
122
144
@@ -139,10 +161,10 @@ set.
139
161
140
162
.. caution ::
141
163
142
- You should be very careful with the ``guessPattern `` method. When the
164
+ You should be very careful using the ``guessPattern `` method. When the
143
165
type is a float, you cannot use it to determine a min or max value of the
144
166
float (e.g. you want a float to be greater than ``5 ``, ``4.512313 `` is not valid
145
- but ``length(4.512314) > length(5) `` is, so the pattern will success ). In
167
+ but ``length(4.512314) > length(5) `` is, so the pattern will succeed ). In
146
168
this case, the value should be set to ``null `` with a ``MEDIUM_CONFIDENCE ``.
147
169
148
170
Registering a Type Guesser
@@ -164,6 +186,6 @@ The last thing you need to do is registering your custom type guesser by using
164
186
165
187
.. note ::
166
188
167
- When you use the full stack framework, you need to register your type
168
- guesser and tag it with ``form.type_guesser ``. For more information see
189
+ When you use the Symfony framework, you need to register your type guesser
190
+ and tag it with ``form.type_guesser ``. For more information see
169
191
:ref: `the tag reference <reference-dic-type_guesser >`.
0 commit comments