Skip to content

Commit 34bc5a6

Browse files
committed
Document isReadable/getValue with
Fix spelling typos Formatting/Spelling fixes Document features enabling from createPropertyAccessor() wording
1 parent 0db3a92 commit 34bc5a6

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

components/property_access/introduction.rst

Lines changed: 35 additions & 28 deletions
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

@@ -46,7 +45,12 @@ method. This is done using the index notation that is used in PHP::
4645
var_dump($accessor->getValue($person, '[first_name]')); // 'Wouter'
4746
var_dump($accessor->getValue($person, '[age]')); // null
4847

49-
As you can see, the method will return ``null`` if the index does not exists.
48+
.. caution::
49+
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
52+
:method:`PropertyAccess::createPropertyAccessor<Symfony\\Component\\PropertyAccess\\PropertyAccess::createPropertyAccessor()>`
53+
to ``true``.
5054

5155
You can also use multi dimensional arrays::
5256

@@ -179,7 +183,7 @@ Magic ``__call()`` Method
179183
~~~~~~~~~~~~~~~~~~~~~~~~~
180184

181185
At last, ``getValue`` can use the magic ``__call`` method, but you need to
182-
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()>`::
183187

184188
// ...
185189
class Person
@@ -205,17 +209,10 @@ enable this feature by using :class:`Symfony\\Component\\PropertyAccess\\Propert
205209
$person = new Person();
206210

207211
// Enable magic __call
208-
$accessor = PropertyAccess::createPropertyAccessorBuilder()
209-
->enableMagicCall()
210-
->getPropertyAccessor();
212+
$accessor = PropertyAccess::createPropertyAccessor(true);
211213

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

214-
.. caution::
215-
216-
The ``__call`` feature is disabled by default, you can enable it by calling
217-
:method:`PropertyAccessorBuilder::enableMagicCall<Symfony\\Component\\PropertyAccess\\PropertyAccessorBuilder::enableMagicCall>`
218-
see `Enable other Features`_.
219216

220217
Writing to Arrays
221218
-----------------
@@ -270,10 +267,7 @@ can use setters, the magic ``__set`` method or properties to set values::
270267
var_dump($person->getLastName()); // 'de Jong'
271268
var_dump($person->children); // array(Person());
272269

273-
You can also use ``__call`` to set values but you need to enable the feature,
274-
see `Enable other Features`_.
275-
276-
.. 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()>`::
277271

278272
// ...
279273
class Person
@@ -298,9 +292,7 @@ see `Enable other Features`_.
298292
$person = new Person();
299293

300294
// Enable magic __call
301-
$accessor = PropertyAccess::createPropertyAccessorBuilder()
302-
->enableMagicCall()
303-
->getPropertyAccessor();
295+
$accessor = PropertyAccess::createPropertyAccessor(true)
304296

305297
$accessor->setValue($person, 'wouter', array(...));
306298

@@ -321,6 +313,12 @@ instead::
321313
// ...
322314
}
323315

316+
.. caution::
317+
318+
Calling :method:`PropertyAccess::createPropertyAccessor<Symfony\\Component\\PropertyAccess\\PropertyAccess::createPropertyAccessor()>`
319+
with an array for an invalid index would always return ``true``.
320+
To make it return ``false``, set the second argument (``$throwExceptionOnInvalidIndex``) argument to ``true``.
321+
324322
The same is possible for :method:`PropertyAccessor::setValue<Symfony\\Component\\PropertyAccess\\PropertyAccessor::setValue>`:
325323
Call the
326324
:method:`PropertyAccessor::isWritable<Symfony\\Component\\PropertyAccess\\PropertyAccessor::isWritable>`
@@ -365,11 +363,13 @@ You can also mix objects and arrays::
365363
var_dump('Hello '.$accessor->getValue($person, 'children[0].firstName')); // 'Wouter'
366364
// equal to $person->getChildren()[0]->firstName
367365

368-
Enable other Features
369-
~~~~~~~~~~~~~~~~~~~~~
366+
Using multiple 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.
370371

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

375375
// ...
@@ -378,20 +378,27 @@ configured to enable extra features. To do that you could use the
378378
// Enable magic __call
379379
$accessorBuilder->enableMagicCall();
380380

381-
// Disable magic __call
382-
$accessorBuilder->disableMagicCall();
383-
384381
// Check if magic __call handling is enabled
385-
$accessorBuilder->isMagicCallEnabled(); // true or false
382+
$accessorBuilder->isMagicCallEnabled(); // true
386383

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

390387
// Or all in one
391388
$accessor = PropertyAccess::createPropertyAccessorBuilder()
392389
->enableMagicCall()
393390
->getPropertyAccessor();
394391

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+
395402
Or you can pass parameters directly to the constructor (not the recommended way)::
396403

397404
// ...

0 commit comments

Comments
 (0)