Skip to content

Commit 16738a5

Browse files
committed
Document features enabling from createPropertyAccessor()
1 parent f65ad3a commit 16738a5

File tree

1 file changed

+27
-31
lines changed

1 file changed

+27
-31
lines changed

components/property_access/introduction.rst

+27-31
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ Usage
2424
The entry point of this component is the
2525
:method:`PropertyAccess::createPropertyAccessor<Symfony\\Component\\PropertyAccess\\PropertyAccess::createPropertyAccessor>`
2626
factory. This factory will create a new instance of the
27-
:class:`Symfony\\Component\\PropertyAccess\\PropertyAccessor` class with the
28-
default configuration::
27+
:class:`Symfony\\Component\\PropertyAccess\\PropertyAccessor` class::
2928

3029
use Symfony\Component\PropertyAccess\PropertyAccess;
3130

@@ -48,10 +47,10 @@ method. This is done using the index notation that is used in PHP::
4847

4948
.. caution::
5049

51-
As you can see, the method will return ``null`` if the index does not exists.
52-
To make it throw an exception in case of invalid index, set the first argument of
50+
As you can see, the method return ``null`` if the index does not exist.
51+
To make it throw an exception, you need to enable the feature by setting the second argument of
5352
:method:`PropertyAccess::createPropertyAccessor<Symfony\\Component\\PropertyAccess\\PropertyAccess::createPropertyAccessor()>`
54-
to ``true``, e.g. ``PropertyAccess::createPropertyAccessor(true)``.
53+
to ``true``.
5554

5655
You can also use multi dimensional arrays::
5756

@@ -184,7 +183,7 @@ Magic ``__call()`` Method
184183
~~~~~~~~~~~~~~~~~~~~~~~~~
185184

186185
At last, ``getValue`` can use the magic ``__call`` method, but you need to
187-
enable this feature by using :class:`Symfony\\Component\\PropertyAccess\\PropertyAccessorBuilder`::
186+
enable this feature by setting the first argument of :method:`PropertyAccess::createPropertyAccessor<Symfony\\Component\\PropertyAccess\\PropertyAccess::createPropertyAccessor()>`::
188187

189188
// ...
190189
class Person
@@ -210,17 +209,10 @@ enable this feature by using :class:`Symfony\\Component\\PropertyAccess\\Propert
210209
$person = new Person();
211210

212211
// Enable magic __call
213-
$accessor = PropertyAccess::createPropertyAccessorBuilder()
214-
->enableMagicCall()
215-
->getPropertyAccessor();
212+
$accessor = PropertyAccess::createPropertyAccessor(true);
216213

217214
var_dump($accessor->getValue($person, 'wouter')); // array(...)
218215

219-
.. caution::
220-
221-
The ``__call`` feature is disabled by default, you can enable it by calling
222-
:method:`PropertyAccessorBuilder::enableMagicCall<Symfony\\Component\\PropertyAccess\\PropertyAccessorBuilder::enableMagicCall>`
223-
see `Enable other Features`_.
224216

225217
Writing to Arrays
226218
-----------------
@@ -275,10 +267,7 @@ can use setters, the magic ``__set`` method or properties to set values::
275267
var_dump($person->getLastName()); // 'de Jong'
276268
var_dump($person->children); // array(Person());
277269

278-
You can also use ``__call`` to set values but you need to enable the feature,
279-
see `Enable other Features`_.
280-
281-
.. code-block:: php
270+
You can also use ``__call`` to set values but you need to enable the feature by setting the first argument of :method:`PropertyAccess::createPropertyAccessor<Symfony\\Component\\PropertyAccess\\PropertyAccess::createPropertyAccessor()>`::
282271

283272
// ...
284273
class Person
@@ -303,9 +292,7 @@ see `Enable other Features`_.
303292
$person = new Person();
304293

305294
// Enable magic __call
306-
$accessor = PropertyAccess::createPropertyAccessorBuilder()
307-
->enableMagicCall()
308-
->getPropertyAccessor();
295+
$accessor = PropertyAccess::createPropertyAccessor(true)
309296

310297
$accessor->setValue($person, 'wouter', array(...));
311298

@@ -330,7 +317,7 @@ instead::
330317

331318
Calling :method:`PropertyAccess::createPropertyAccessor<Symfony\\Component\\PropertyAccess\\PropertyAccess::createPropertyAccessor()>`
332319
with an array for an invalid index would always return ``true``.
333-
To make it return ``false``, set its first argument to ``true`` e.g. ``PropertyAccess::createPropertyAccessor(true)``.
320+
To make it return ``false``, set the second argument (``$throwExceptionOnInvalidIndex``) argument to ``true``.
334321

335322
The same is possible for :method:`PropertyAccessor::setValue<Symfony\\Component\\PropertyAccess\\PropertyAccessor::setValue>`:
336323
Call the
@@ -376,11 +363,13 @@ You can also mix objects and arrays::
376363
var_dump('Hello '.$accessor->getValue($person, 'children[0].firstName')); // 'Wouter'
377364
// equal to $person->getChildren()[0]->firstName
378365

379-
Enable other Features
380-
~~~~~~~~~~~~~~~~~~~~~
366+
Using different configurations
367+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
368+
369+
The :method:`PropertyAccess::createPropertyAccessor<Symfony\\Component\\PropertyAccess\\PropertyAccess::createPropertyAccessor()>`
370+
method allows you to get a property accessor configured from the passed arguments.
381371

382-
The :class:`Symfony\\Component\\PropertyAccess\\PropertyAccessor` can be
383-
configured to enable extra features. To do that you could use the
372+
For using several property accessor configured independently, use the
384373
:class:`Symfony\\Component\\PropertyAccess\\PropertyAccessorBuilder`::
385374

386375
// ...
@@ -389,20 +378,27 @@ configured to enable extra features. To do that you could use the
389378
// Enable magic __call
390379
$accessorBuilder->enableMagicCall();
391380

392-
// Disable magic __call
393-
$accessorBuilder->disableMagicCall();
394-
395381
// Check if magic __call handling is enabled
396-
$accessorBuilder->isMagicCallEnabled(); // true or false
382+
$accessorBuilder->isMagicCallEnabled(); // true
397383

398-
// At the end get the configured property accessor
384+
// Get the property accessor with magic __call enabled
399385
$accessor = $accessorBuilder->getPropertyAccessor();
400386

401387
// Or all in one
402388
$accessor = PropertyAccess::createPropertyAccessorBuilder()
403389
->enableMagicCall()
404390
->getPropertyAccessor();
405391

392+
// Disable magic __call
393+
$accessorBuilder->disableMagicCall();
394+
395+
// Enable exception on invalid indexes
396+
$accessorBuilder->enableExceptionOnInvalidIndex();
397+
398+
// Get the newly configured property accessor
399+
$accessor = $accessorBuilder->getPropertyAccessor();
400+
401+
406402
Or you can pass parameters directly to the constructor (not the recommended way)::
407403

408404
// ...

0 commit comments

Comments
 (0)