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
Copy file name to clipboardExpand all lines: docs/VOM.md
+52-1Lines changed: 52 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,7 @@ The Versatile Object Mapper - or in short VOM - is a PHP library to transform an
19
19
<!-- toc -->
20
20
21
21
-[The Object Mapper](#the-object-mapper)
22
-
*[Without Framework](#without-framework)
22
+
*[Without Framework](#plain-old-php)
23
23
*[Laravel Framework](#laravel-framework)
24
24
*[Symfony Framework](#symfony-framework)
25
25
*[Denormalization](#denormalization)
@@ -322,6 +322,33 @@ class ConstructorArguments
322
322
> [!NOTE]
323
323
> When using the [`object_to_populate` context](#object-to-populate), the constructor arguments and constructor property promotion will be skipped.
324
324
325
+
#### Serialized Objects
326
+
327
+
In case the source data is a serialized representation of the model, the constructor (or factory) can have a single string argument with the `serialized` option set to `true`.
328
+
329
+
See [Custom serialization](#custom-serialization) to implement the equivalent normalizer.
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.
675
+
676
+
See [Serialized Objects](#serialized-objects) to implement the equivalent denormalizer.
677
+
678
+
```php
679
+
use Symfony\Component\Serializer\Attribute\Groups;
680
+
use Zolex\VOM\Mapping as VOM;
681
+
682
+
#[VOM\Model]
683
+
class SerializedObject
684
+
{
685
+
public int $id;
686
+
public string $name;
687
+
688
+
#[VOM\Mormalizer]
689
+
public function __toString(): string
690
+
{
691
+
return $this->id . ':' . $this->name;
692
+
}
693
+
}
694
+
```
695
+
645
696
> [!CAUTION]
646
697
> It is possible to normalize data and denormalize a model while maintaining the exact same results, no matter how often you repeat this process.
647
698
> If this is one of your requirements, and you are using Normalizer and Denormalizer methods, you have to be careful how you store the injected data
@@ -143,6 +150,10 @@ public function getMetadataFor(string|\ReflectionClass $class, ?ModelMetadata $m
143
150
}
144
151
}
145
152
153
+
if ($hasSerializer && $normalizerCount > 1) {
154
+
thrownewMappingException(\sprintf('The "__toString()" method on model "%s" is configured as a normalizer. There must be no additional normalizer methods.', $class->getName()));
thrownewMappingException(\sprintf('Normalizer on "%s::%s()" cannot be added. Normalizer can only be added on methods beginning with "get", "has", "is" or "normalize".', $reflectionClass->getName(), $reflectionMethod->getName()));
199
+
thrownewMappingException(\sprintf('Normalizer on "%s::%s()" cannot be added. Normalizer can only be added on methods beginning with "get", "has", "is" or "normalize" or on the "__toString" method.', $reflectionClass->getName(), $reflectionMethod->getName()));
0 commit comments