You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -244,7 +250,7 @@ public function setPlace(mixed $place): void
244
250
```
245
251
246
252
> convert the value of the array whose offset is `1` into a `Place` Enum
247
-
> if the value is null resolve the string `Galway` to `Place::Galway`. Once created,
253
+
> if the value is null resolve the string `Abidjan` to `Place::Abidjan`. Once created,
248
254
> call the method `setPlace` with the created `Place` enum filling the `$place` argument.
249
255
250
256
<pclass="notice">Using this class with a <code>mixed</code> type without providing the <code>enum</code> parameter will trigger an exception.</p>
@@ -485,40 +491,52 @@ final class CastToNaira implements TypeCasting
485
491
<pclass="message-info">While the built-in <code>TypeCasting</code> classes do not support Intersection Type, your own
486
492
implementing class can support them via inspection of the <code>$reflectionProperty</code> argument.</p>
487
493
488
-
## Using the feature outside the TabularDataReader
494
+
## Using the feature without a TabularDataReader
489
495
490
496
The feature can be used outside the package usage via the `Denormalizer` class.
491
497
492
498
The class exposes four (4) methods to ease `array` to `object` conversion:
493
499
494
-
-`Denormalizer::denormalizeAll` and `Denormalizer::assignAll` which convert a collection of records into a collection of instances of a specified class.
495
-
-`Denormalizer::denormalize` and `Denormalizer::assign` which convert a single record into a new instance of the specified class.
500
+
-`Denormalizer::denormalizeAll` and `Denormalizer::assignAll` to convert a collection of records into a collection of instances of a specified class.
501
+
-`Denormalizer::denormalize` and `Denormalizer::assign` to convert a single record into a new instance of the specified class.
502
+
503
+
Since we are not leveraging the `TabularDataReader` we must explicitly tell the class how to link array keys and class properties.
504
+
Once instantiated you can reuse the instance to independently convert a single record or a collection of `array`.
496
505
497
506
```php
498
507
use League\Csv\Serializer\Denormalizer;
499
508
500
509
$record = [
501
510
'date' => '2023-10-30',
502
511
'temperature' => '-1.5',
503
-
'place' => 'Berkeley',
512
+
'place' => 'Yamoussoukro',
504
513
];
505
514
506
515
//a complete collection of records as shown below
507
516
$collection = [$record];
508
517
//we first instantiate the denormalizer
509
-
$denormalizer = new Denormalizer(Weather::class, ['date', 'temperature', 'place']);
518
+
//and we provide the information to map record key to the class properties
519
+
$denormalizer = new Denormalizer(ClimaticRecord::class, ['date', 'temperature', 'place']);
510
520
$weather = $denormalizer->denormalize($record); //we convert 1 record into 1 instance
511
521
512
522
foreach ($denormalizer->denormalizeAll($collection) as $weather) {
513
-
// each $weather entry will be an instance of the Weather class;
523
+
// each $weather entry will be an instance of the ClimaticRecord class;
514
524
}
525
+
```
526
+
527
+
To complete the feature 2 static methods are provided if you only need to denormalization once,
528
+
`Denormalizer::assign` will automatically use the `array` keys as property names. Whereas,
529
+
you still need to give the property list to `Denormalizer::assignAll` to allow the class
530
+
to work with any given iterable structure of `array`.
515
531
532
+
```php
533
+
<?php
516
534
// you can use the alternate syntactic sugar methods
0 commit comments