5
5
The PropertyInfo Component
6
6
==========================
7
7
8
- The PropertyInfo component provides the functionality to get information
9
- about class properties using metadata of popular sources .
8
+ The PropertyInfo component allows you to get information
9
+ about class properties by using different sources of metadata .
10
10
11
11
While the :doc: `PropertyAccess component </components/property_access >`
12
12
allows you to read and write values to/from objects and arrays, the PropertyInfo
13
- component works solely with class definitions to provide information such
14
- as data type and visibility about properties within that class.
15
-
16
- Similar to PropertyAccess, the PropertyInfo component combines both class
17
- properties (such as ``$property ``) and properties defined via accessor and
18
- mutator methods such as ``getProperty() ``, ``isProperty() ``, ``setProperty() ``,
19
- ``addProperty() ``, ``removeProperty() ``, etc.
13
+ component works solely with class definitions to provide information about the
14
+ data type and visibility - including via getter or setter methods - of the properties
15
+ within that class.
20
16
21
17
.. versionadded :: 2.8
22
18
The PropertyInfo component was introduced in Symfony 2.8.
@@ -42,22 +38,48 @@ Additional dependencies may be required for some of the
42
38
Usage
43
39
-----
44
40
45
- The entry point of this component is a new instance of the
46
- :class: `Symfony\\ Component\\ PropertyInfo\\ PropertyInfoExtractor `
47
- class, providing sets of information extractors.
41
+ To use this component, create a new
42
+ :class: `Symfony\\ Component\\ PropertyInfo\\ PropertyInfoExtractor ` instance and
43
+ provide it with a set of information extractors.
48
44
49
45
.. code-block :: php
50
46
51
47
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
48
+ use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
49
+ use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
50
+ use Example\Namespace\YourAwesomeCoolClass;
51
+
52
+ // a full list of extractors is shown further below
53
+ $phpDocExtractor = new PhpDocExtractor();
54
+ $reflectionExtractor = new ReflectionExtractor();
55
+
56
+ // array of PropertyListExtractorInterface
57
+ $listExtractors = array($reflectionExtractor);
58
+
59
+ // array of PropertyTypeExtractorInterface
60
+ $typeExtractors = array($phpDocExtractor, $reflectionExtractor);
61
+
62
+ // array of PropertyDescriptionExtractorInterface
63
+ $descriptionExtractors = array($phpDocExtractor);
64
+
65
+ // array of PropertyAccessExtractorInterface
66
+ $accessExtractors = array($reflectionExtractor);
52
67
53
68
$propertyInfo = new PropertyInfoExtractor(
54
- $arrayOfListExtractors ,
55
- $arrayOfTypeExtractors ,
56
- $arrayOfDescriptionExtractors ,
57
- $arrayOfAccessExtractors
69
+ $listExtractors ,
70
+ $typeExtractors ,
71
+ $descriptionExtractors ,
72
+ $accessExtractors
58
73
);
59
74
60
- The order of extractor instances within an array matters, as the first non-null
75
+ // see below for more examples
76
+ $class = YourAwesomeCoolClass::class;
77
+ $properties = $propertyInfo->getProperties($class);
78
+
79
+ Extractor Ordering
80
+ ~~~~~~~~~~~~~~~~~~
81
+
82
+ The order of extractor instances within an array matters: the first non-null
61
83
result will be returned. That is why you must provide each category of extractors
62
84
as a separate array, even if an extractor provides information for more than
63
85
one category.
@@ -101,21 +123,26 @@ Extractable Information
101
123
-----------------------
102
124
103
125
The :class: `Symfony\\ Component\\ PropertyInfo\\ PropertyInfoExtractor `
104
- class exposes public methods to extract four types of information: list,
105
- type, description and access information. The first type of information is
106
- about the class, while the remaining three are about the individual properties.
126
+ class exposes public methods to extract four types of information:
127
+
128
+ * :ref: `*List* of properties <property-info-list >`: `getProperties() `
129
+ * :ref: `Property *type* <property-info-type >`: `getTypes() `
130
+ * :ref: `Property *description* <property-info-description >`: `getShortDescription() ` and `getLongDescription() `
131
+ * :ref: `Property *access* details <property-info-access >`: `isReadable() ` and `isWritable() `
107
132
108
133
.. note ::
109
134
110
- When specifiying a class that the PropertyInfo component should work
111
- with, use the fully-qualified class name. Do not directly pass an object
112
- as some extractors (the
113
- :class: `Symfony\\ Component\\ PropertyInfo\\ Extractor\\ SerializerExtractor `
114
- is an example) may not automatically resolve objects to their class
115
- names - use the ``get_class() `` function.
135
+ Be sure to pass a *class * name, not an object to the extractor methods::
136
+
137
+ // bad! It may work, but not with all extractors
138
+ $propertyInfo->getProperties($awesomeObject);
116
139
117
- Since the PropertyInfo component requires PHP 5.5 or greater, you can
118
- also make use of the `class constant `_.
140
+ // Good!
141
+ $propertyInfo->getProperties(get_class($awesomeObject));
142
+ $propertyInfo->getProperties('Example\Namespace\YourAwesomeClass');
143
+ $propertyInfo->getProperties(YourAwesomeClass::class);
144
+
145
+ .. _property-info-list :
119
146
120
147
List Information
121
148
~~~~~~~~~~~~~~~~
@@ -137,6 +164,8 @@ containing each property name as a string.
137
164
}
138
165
*/
139
166
167
+ .. _property-info-type :
168
+
140
169
Type Information
141
170
~~~~~~~~~~~~~~~~
142
171
@@ -164,6 +193,10 @@ for a property.
164
193
}
165
194
*/
166
195
196
+ See :ref: `components-property-info-type ` for info about the ``Type `` class.
197
+
198
+ .. _property-info-description :
199
+
167
200
Description Information
168
201
~~~~~~~~~~~~~~~~~~~~~~~
169
202
@@ -189,6 +222,8 @@ strings.
189
222
It can span multiple lines.
190
223
*/
191
224
225
+ .. _property-info-access :
226
+
192
227
Access Information
193
228
~~~~~~~~~~~~~~~~~~
194
229
@@ -203,6 +238,11 @@ provide whether properties are readable or writable as booleans.
203
238
$propertyInfo->isWritable($class, $property);
204
239
// Example Result: bool(false)
205
240
241
+ The :class: `Symfony\\ Component\\ PropertyInfo\\ Extractor\\ ReflectionExtractor ` looks
242
+ for getter/isser/setter method in addition to whether or not a property is public
243
+ to determine if it's accessible. This based on how the :doc: `PropertyAccess </components/property_access >`
244
+ works.
245
+
206
246
.. tip ::
207
247
208
248
The main :class: `Symfony\\ Component\\ PropertyInfo\\ PropertyInfoExtractor `
@@ -219,10 +259,9 @@ Type Objects
219
259
------------
220
260
221
261
Compared to the other extractors, type information extractors provide much
222
- more information than can be represented as simple scalar values - because
223
- of this, type extractors return an array of objects. The array will contain
224
- an instance of the :class: `Symfony\\ Component\\ PropertyInfo\\ Type `
225
- class for each type that the property supports.
262
+ more information than can be represented as simple scalar values. Because
263
+ of this, type extractors return an array of :class: `Symfony\\ Component\\ PropertyInfo\\ Type `
264
+ objects for each type that the property supports.
226
265
227
266
For example, if a property supports both ``integer `` and ``string `` (via
228
267
the ``@return int|string `` annotation),
@@ -236,15 +275,12 @@ class.
236
275
instance. The :class: `Symfony\\ Component\\ PropertyInfo\\ Extractor\\ PhpDocExtractor `
237
276
is currently the only extractor that returns multiple instances in the array.
238
277
239
- Each object will provide 6 attributes; the first four (built-in type, nullable,
240
- class and collection) are scalar values and the last two (collection key
241
- type and collection value type) are more instances of the :class: `Symfony\\ Component\\ PropertyInfo\\ Type `
242
- class again if collection is ``true ``.
278
+ Each object will provide 6 attributes, available in the 6 methods:
243
279
244
280
.. _`components-property-info-type-builtin` :
245
281
246
- Built-in Type
247
- ~~~~~~~~~~~~~
282
+ Type::getBuiltInType()
283
+ ~~~~~~~~~~~~~~~~~~~~~~
248
284
249
285
The :method: `Type::getBuiltinType() <Symfony\\ Component\\ PropertyInfo\\ Type::getBuiltinType> `
250
286
method will return the built-in PHP data type, which can be one of 9 possible
@@ -254,22 +290,22 @@ string values: ``array``, ``bool``, ``callable``, ``float``, ``int``, ``null``,
254
290
Constants inside the :class: `Symfony\\ Component\\ PropertyInfo\\ Type `
255
291
class, in the form ``Type::BUILTIN_TYPE_* ``, are provided for convenience.
256
292
257
- Nullable
258
- ~~~~~~~~
293
+ Type::isNullable()
294
+ ~~~~~~~~~~~~~~~~~~
259
295
260
296
The :method: `Type::isNullable() <Symfony\\ Component\\ PropertyInfo\\ Type::isNullable> `
261
297
method will return a boolean value indicating whether the property parameter
262
298
can be set to ``null ``.
263
299
264
- Class
265
- ~~~~~
300
+ Type::getClassName()
301
+ ~~~~~~~~~~~~~~~~~~~~
266
302
267
303
If the :ref: `built-in PHP data type <components-property-info-type-builtin >`
268
304
is ``object ``, the :method: `Type::getClassName() <Symfony\\ Component\\ PropertyInfo\\ Type::getClassName> `
269
305
method will return the fully-qualified class or interface name accepted.
270
306
271
- Collection
272
- ~~~~~~~~~~
307
+ Type::isCollection()
308
+ ~~~~~~~~~~~~~~~~~~~~
273
309
274
310
The :method: `Type::isCollection() <Symfony\\ Component\\ PropertyInfo\\ Type::isCollection> `
275
311
method will return a boolean value indicating if the property parameter is
@@ -281,8 +317,8 @@ this returns ``true`` if:
281
317
* The mutator method the property is derived from has a prefix of ``add ``
282
318
or ``remove `` (which are defined as the list of array mutator prefixes).
283
319
284
- Collection Key & Value Types
285
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
320
+ Type::getCollectionKeyType() & Type::getCollectionValueType()
321
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
286
322
287
323
If the property is a collection, additional type objects may be returned
288
324
for both the key and value types of the collection (if the information is
@@ -315,13 +351,14 @@ Using PHP reflection, the :class:`Symfony\\Component\\PropertyInfo\\Extractor\\R
315
351
provides list, type and access information from setter and accessor methods.
316
352
It can also provide return and scalar types for PHP 7+.
317
353
318
- This service is automatically registered with the ``property_info `` service.
354
+ This service is automatically registered with the ``property_info `` service in
355
+ the Symfony Framework.
319
356
320
357
.. code-block :: php
321
358
322
359
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
323
360
324
- $reflectionExtractor = new ReflectionExtractor;
361
+ $reflectionExtractor = new ReflectionExtractor() ;
325
362
326
363
// List information.
327
364
$reflectionExtractor->getProperties($class);
@@ -341,13 +378,14 @@ PhpDocExtractor
341
378
Using `phpDocumentor Reflection `_ to parse property and method annotations,
342
379
the :class: `Symfony\\ Component\\ PropertyInfo\\ Extractor\\ PhpDocExtractor `
343
380
provides type and description information. This extractor is automatically
344
- registered with the ``property_info `` providing its dependencies are detected.
381
+ registered with the ``property_info `` in the Symfony Framework *if * the dependent
382
+ library is present.
345
383
346
384
.. code-block :: php
347
385
348
386
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
349
387
350
- $phpDocExtractor = new PhpDocExtractor;
388
+ $phpDocExtractor = new PhpDocExtractor() ;
351
389
352
390
// Type information.
353
391
$phpDocExtractor->getTypes($class, $property);
@@ -362,10 +400,11 @@ SerializerExtractor
362
400
363
401
This extractor depends on the `symfony/serializer `_ library.
364
402
365
- Using groups metadata from the :doc: `Serializer component </components/serializer >`,
403
+ Using :ref: `groups metadata <serializer-using-serialization-groups-annotations >`
404
+ from the :doc: `Serializer component </components/serializer >`,
366
405
the :class: `Symfony\\ Component\\ PropertyInfo\\ Extractor\\ SerializerExtractor `
367
- provides list information. This extractor is not registered automatically
368
- with the ``property_info `` service.
406
+ provides list information. This extractor is * not * registered automatically
407
+ with the ``property_info `` service in the Symfony Framework .
369
408
370
409
.. code-block :: php
371
410
@@ -392,9 +431,8 @@ DoctrineExtractor
392
431
393
432
Using entity mapping data from `Doctrine ORM `_, the
394
433
:class: `Symfony\\ Bridge\\ Doctrine\\ PropertyInfo\\ DoctrineExtractor `
395
- - located in the Doctrine bridge - provides list and type information.
396
- This extractor is not registered automatically with the ``property_info ``
397
- service.
434
+ provides list and type information. This extractor is not registered automatically
435
+ with the ``property_info `` service in the Symfony Framework.
398
436
399
437
.. code-block :: php
400
438
@@ -443,4 +481,3 @@ service by defining it as a service with one or more of the following
443
481
.. _`symfony/serializer` : https://packagist.org/packages/symfo
4E5D
ny/serializer
444
482
.. _`symfony/doctrine-bridge` : https://packagist.org/packages/symfony/doctrine-bridge
445
483
.. _`doctrine/orm` : https://packagist.org/packages/doctrine/orm
446
- .. _`class constant` : http://php.net/manual/en/language.oop5.basic.php#language.oop5.basic.class.class
0 commit comments