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
Feature: Replace the old denormalizer-dependencies feature with a backwards compatible method-dependencies feature, that can be used on constructors, factories, normalizers and denormalizers. (#20)
If you need any dependencies in addition to the source data to be mapped, it is possible to inject any object (like a symfony service) into the denormalizer methods.
560
-
To do so, just typehint the dependency in the denormalizer method along with the VOM arguments. Additionally, you need to register the dependency.
561
-
562
-
```php
563
-
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
If you are using the `VersatileObjectMapperFactory` you can pass the dependencies to the `create` method.
603
-
604
-
```php
605
-
$vom = VersatileObjectMapperFactory::create(null, [new DependencyA(), new DependencyB()]);
606
-
```
607
-
608
-
609
556
##### Non-Scalar Denormalizer Arguments
610
557
611
558
To prevent unnecessary usage of denormalizers when dealing with arrays, the denormalizer methods only accept scalar arguments by default.
@@ -635,7 +582,7 @@ class BadHabits
635
582
}
636
583
```
637
584
638
-
#### Normalizer Methods
585
+
### Normalizer Methods
639
586
640
587
Similar to the denormalizer methods, also normalizer methods can be configured to be called during normalization. These methods must not have any required arguments.
641
588
Normalizer methods must be prefixed with `get`, `has`, `is` or `normalize`. Groups can be added on normalizer methods with the first three prefixes (virtual property) or on the related property when using the `normalize` prefix.
@@ -677,7 +624,7 @@ class Calls
677
624
}
678
625
```
679
626
680
-
##### Custom serialization
627
+
### Custom serialization
681
628
682
629
The normalizer attribute can be added on the `__toString()` method. Note that in this case it must be the only normalizer on the model and return a string representation of the object.
683
630
@@ -740,6 +687,125 @@ class Calls
740
687
}
741
688
```
742
689
690
+
### Method Dependencies
691
+
692
+
If you need any dependencies in addition to the source data to be mapped, it is possible to inject any object (like a symfony service) into methods.
693
+
To do so, just typehint the dependency in the method along with the VOM arguments. All dependencies must be explicitly registered.
694
+
In symfony framework, you can simply add any symfony service by adding it to the package config:
695
+
696
+
_config/packages/zolex_vom.yaml_
697
+
```yaml
698
+
zolex_vom:
699
+
method_dependencies:
700
+
- '@parameter_bag'
701
+
```
702
+
703
+
Without symfony, you simply call the respective method on the `ModelMetadataFactory`
704
+
705
+
```php
706
+
$factory = new \Zolex\VOM\Metadata\Factory\ModelMetadataFactory(/*...*/);
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
791
+
use Zolex\VOM\Mapping as VOM;
792
+
793
+
#[VOM\Model]
794
+
class NormalizerDependency
795
+
{
796
+
#[VOM\Factory]
797
+
public static function create(
798
+
#[VOM\Argument]
799
+
int $something,
800
+
ParameterBagInterface $parameterBag,
801
+
#[VOM\Argument]
802
+
string $else
803
+
): self {
804
+
return new self($parameterBag->get('foo'));
805
+
}
806
+
}
807
+
```
808
+
743
809
### Disable Nesting
744
810
745
811
The data structures can be as deeply nested as you want. By default, nesting is enabled. That is when no accessor is given or the accessor is symfony property access syntax.
0 commit comments