@@ -1282,6 +1282,195 @@ class RegexpExtractorProperty
12821282$model = $objectMapper->denormalize('image1.jpg,tag:foobar,visibility:hidden', RegexpExtractorProperty::class);
12831283` ` `
12841284
1285+ # # Scenarios
1286+
1287+ If there is more than one representation of your normalized data, the `scenario` argument allows to switch between different mappings.
1288+
1289+ ` ` ` php
1290+ use Zolex\V OM\M apping as VOM;
1291+
1292+ #[VOM\M odel]
1293+ class ModelWithScenarios
1294+ {
1295+ #[VOM\P roperty]
1296+ #[VOM\P roperty('[FIRST_NAME]', scenario: 'second')]
1297+ #[VOM\P roperty('[name][first]'), scenario: 'third'])]
1298+ public string $firstName;
1299+
1300+ #[VOM\P roperty]
1301+ #[VOM\P roperty('[LAST_NAME]', scenario: 'second')]
1302+ #[VOM\P roperty('[name][last]'), scenario: 'third'])]
1303+ public string $lastName;
1304+ }
1305+ ` ` `
1306+
1307+ The scenario can be passed in the context of the `serialize`, `deserialize`, `normalize` and `denormalize` methods of the ObjectMapper.
1308+
1309+ ` ` ` php
1310+ $data = [
1311+ 'firstName' => 'Peter',
1312+ 'lastName' => 'Parker'
1313+ ];
1314+ $objectMapper->denormalize($data, ModelWithScenarios::class);
1315+
1316+ $data = [
1317+ 'FIRST_NAME' => 'Peter',
1318+ 'LAST_NAME' => 'Parker'
1319+ ];
1320+ $objectMapper->denormalize($data, ModelWithScenarios::class, null, ['scenario' => 'second']);
1321+
1322+ $data = [
1323+ 'name' => [
1324+ 'first' => 'Peter',
1325+ 'last' => 'Parker'
1326+ ]
1327+ ];
1328+ $objectMapper->denormalize($data, ModelWithScenarios::class, null, ['scenario' => 'third']);
1329+ ` ` `
1330+
1331+ The `scenario` argument can also be used on the arguments of constructors, factories and denormalizers as well as on normalizer methods.
1332+
1333+ **Constructor Scenarios**
1334+
1335+ ` ` ` php
1336+ use Zolex\V OM\M apping as VOM;
1337+
1338+ #[VOM\M odel]
1339+ class ModelWithScenarios
1340+ {
1341+ public function __construct(
1342+ #[VOM\A rgument]
1343+ #[VOM\A rgument('[FIRST_NAME]', scenario: 'second')]
1344+ string $firstName,
1345+
1346+ #[VOM\A rgument]
1347+ #[VOM\A rgument('[LAST_NAME]', scenario: 'second')]
1348+ string $lastName,
1349+ ) {
1350+ // ...
1351+ }
1352+ }
1353+ ` ` `
1354+
1355+ **Factory Scenarios**
1356+
1357+ ` ` ` php
1358+ use Zolex\V OM\M apping as VOM;
1359+
1360+ #[VOM\M odel]
1361+ class ModelWithScenarios
1362+ {
1363+ #[VOM\F actory]
1364+ public function create(
1365+ #[VOM\A rgument]
1366+ #[VOM\A rgument('[FIRST_NAME]', scenario: 'second')]
1367+ string $firstName,
1368+
1369+ #[VOM\A rgument]
1370+ #[VOM\A rgument('[LAST_NAME]', scenario: 'second')]
1371+ string $lastName,
1372+ ): self {
1373+ // ...
1374+ }
1375+ }
1376+ ` ` `
1377+
1378+ **Denormalizer Scenarios**
1379+
1380+ ` ` ` php
1381+ use Zolex\V OM\M apping as VOM;
1382+
1383+ #[VOM\M odel]
1384+ class ModelWithScenarios
1385+ {
1386+ #[VOM\D enormalizer]
1387+ public function denormalizeValues(
1388+ #[VOM\A rgument]
1389+ #[VOM\A rgument('[FIRST_NAME]', scenario: 'second')]
1390+ string $firstName,
1391+
1392+ #[VOM\A rgument]
1393+ #[VOM\A rgument('[LAST_NAME]', scenario: 'second')]
1394+ string $lastName,
1395+ ) {
1396+ // ...
1397+ }
1398+ }
1399+ ` ` `
1400+
1401+ **Normalizer Scenarios**
1402+
1403+ ` ` ` php
1404+ use Zolex\V OM\M apping as VOM;
1405+
1406+ #[VOM\M odel]
1407+ class ModelWithScenarios
1408+ {
1409+ private string $firstName = 'Peter';
1410+ private string $lastName = 'Parker';
1411+
1412+ #[VOM\N ormalizer(accessor: '[firstName]')]
1413+ public function normalizeFirstNameForDefaultScenario(): string {
1414+ return $this->firstName;
1415+ }
1416+
1417+ #[VOM\N ormalizer(accessor: '[FIRST_NAME]', scenario: 'second')]
1418+ public function normalizeFirstNameForSecondScenario(): string {
1419+ return strtoupper($this->firstName);
1420+ }
1421+
1422+ #[VOM\N ormalizer(accessor: '[lastName]')]
1423+ public function normalizeLastNameForDefaultScenario(): string {
1424+ return return $this->lastName;
1425+ }
1426+
1427+ #[VOM\N ormalizer(accessor: '[LAST_NAME]', scenario: 'second')]
1428+ public function normalizeFirstNameForSecondScenario(): string {
1429+ return strtoupper($this->lastName);
1430+ }
1431+ }
1432+ ` ` `
1433+
1434+ > [!NOTE]
1435+ > For fine-grained control, scenarios can also be combined with the `Groups` feature.
1436+
1437+ ` ` ` php
1438+ use \S ymfony\C omponent\S erializer\A ttribute\G roups;
1439+ use Zolex\V OM\M apping as VOM;
1440+
1441+ #[VOM\M odel]
1442+ class ModelWithScenarios
1443+ {
1444+ private string $firstName = 'Peter';
1445+ private string $lastName = 'Parker';
1446+
1447+ #[Groups('firstname')]
1448+ #[VOM\N ormalizer(accessor: '[firstName]')]
1449+ public function getFirstNameForDefaultScenario(): string {
1450+ return $this->firstName;
1451+ }
1452+
1453+ #[Groups('firstname')]
1454+ #[VOM\N ormalizer(accessor: '[FIRST_NAME]', scenario: 'second')]
1455+ public function getFirstNameForSecondScenario(): string {
1456+ return strtoupper($this->firstName);
1457+ }
1458+
1459+ #[Groups('lastname')]
1460+ #[VOM\N ormalizer(accessor: '[lastName]')]
1461+ public function getLastNameForDefaultScenario(): string {
1462+ return return $this->lastName;
1463+ }
1464+
1465+ #[Groups('lastname')]
1466+ #[VOM\N ormalizer(accessor: '[LAST_NAME]', scenario: 'second')]
1467+ public function getFirstNameForSecondScenario(): string {
1468+ return strtoupper($this->lastName);
1469+ }
1470+ }
1471+ ` ` `
1472+
1473+
12851474# # Interfaces and Abstract Classes
12861475
12871476When dealing with objects that are fairly similar or share properties, you can use interfaces or abstract classes.
0 commit comments