24
24
The entry point of this component is the
25
25
:method: `PropertyAccess::createPropertyAccessor<Symfony\\ Component\\ PropertyAccess\\ PropertyAccess::createPropertyAccessor> `
26
26
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::
29
28
30
29
use Symfony\Component\PropertyAccess\PropertyAccess;
31
30
@@ -46,7 +45,12 @@ method. This is done using the index notation that is used in PHP::
46
45
var_dump($accessor->getValue($person, '[first_name]')); // 'Wouter'
47
46
var_dump($accessor->getValue($person, '[age]')); // null
48
47
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 ``.
50
54
51
55
You can also use multi dimensional arrays::
52
56
@@ -179,7 +183,7 @@ Magic ``__call()`` Method
179
183
~~~~~~~~~~~~~~~~~~~~~~~~~
180
184
181
185
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()> `::
183
187
184
188
// ...
185
189
class Person
@@ -205,17 +209,10 @@ enable this feature by using :class:`Symfony\\Component\\PropertyAccess\\Propert
205
209
$person = new Person();
206
210
207
211
// Enable magic __call
208
- $accessor = PropertyAccess::createPropertyAccessorBuilder()
209
- ->enableMagicCall()
210
- ->getPropertyAccessor();
212
+ $accessor = PropertyAccess::createPropertyAccessor(true);
211
213
212
214
var_dump($accessor->getValue($person, 'wouter')); // array(...)
213
215
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 `_.
219
216
220
217
Writing to Arrays
221
218
-----------------
@@ -270,10 +267,7 @@ can use setters, the magic ``__set`` method or properties to set values::
270
267
var_dump($person->getLastName()); // 'de Jong'
271
268
var_dump($person->children); // array(Person());
272
269
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()> `::
277
271
278
272
// ...
279
273
class Person
@@ -298,9 +292,7 @@ see `Enable other Features`_.
298
292
$person = new Person();
299
293
300
294
// Enable magic __call
301
- $accessor = PropertyAccess::createPropertyAccessorBuilder()
302
- ->enableMagicCall()
303
- ->getPropertyAccessor();
295
+ $accessor = PropertyAccess::createPropertyAccessor(true)
304
296
305
297
$accessor->setValue($person, 'wouter', array(...));
306
298
@@ -321,6 +313,12 @@ instead::
321
313
// ...
322
314
}
323
315
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
+
324
322
The same is possible for :method: `PropertyAccessor::setValue<Symfony\\ Component\\ PropertyAccess\\ PropertyAccessor::setValue> `:
325
323
Call the
326
324
:method: `PropertyAccessor::isWritable<Symfony\\ Component\\ PropertyAccess\\ PropertyAccessor::isWritable> `
@@ -365,11 +363,13 @@ You can also mix objects and arrays::
365
363
var_dump('Hello '.$accessor->getValue($person, 'children[0].firstName')); // 'Wouter'
366
364
// equal to $person->getChildren()[0]->firstName
367
365
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.
370
371
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
373
373
:class: `Symfony\\ Component\\ PropertyAccess\\ PropertyAccessorBuilder `::
374
374
375
375
// ...
@@ -378,20 +378,27 @@ configured to enable extra features. To do that you could use the
378
378
// Enable magic __call
379
379
$accessorBuilder->enableMagicCall();
380
380
381
- // Disable magic __call
382
- $accessorBuilder->disableMagicCall();
383
-
384
381
// Check if magic __call handling is enabled
385
- $accessorBuilder->isMagicCallEnabled(); // true or false
382
+ $accessorBuilder->isMagicCallEnabled(); // true
386
383
387
- // At the end get the configured property accessor
384
+ // Get the property accessor with magic __call enabled
388
385
$accessor = $accessorBuilder->getPropertyAccessor();
389
386
390
387
// Or all in one
391
388
$accessor = PropertyAccess::createPropertyAccessorBuilder()
392
389
->enableMagicCall()
393
390
->getPropertyAccessor();
394
391
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
+
395
402
Or you can pass parameters directly to the constructor (not the recommended way)::
396
403
397
404
// ...
0 commit comments