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