Commit 6832421
committed
feature symfony#60355 [Serializer] Add
This PR was merged into the 7.4 branch.
Discussion
----------
[Serializer] Add `CDATA_WRAPPING_NAME_PATTERN` support to `XmlEncoder`
| Q | A
| ------------- | ---
| Branch? | 7.3
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Issues |
| License | MIT
### Add `CDATA_WRAPPING_NAME_PATTERN` support to XmlEncoder
This PR introduces a new option for the Symfony Serializer component's `XmlEncoder`: `CDATA_WRAPPING_NAME_PATTERN`.
While Symfony already provides `CDATA_WRAPPING_PATTERN` to wrap field *content* in CDATA based on a regex match, this new feature enables CDATA wrapping based on the **field name**—regardless of the field's content.
**Example use case:**
```php
$this->encoder->encode($data, 'xml', [
XmlEncoder::CDATA_WRAPPING_NAME_PATTERN => '(firstname|lastname)'
]);
```
This ensures that fields named `firstname` or `lastname` are always wrapped in CDATA tags, even if their content doesn't match the typical pattern used by `CDATA_WRAPPING_PATTERN`.
### Motivation:
* **Consistent Output**: In many integration scenarios (e.g., with legacy systems, XML consumers, or strict XSD schemas), consumers expect certain fields to **always** use CDATA sections—even when their content doesn't require escaping. This option allows for deterministic XML output by name.
* **Better Control**: It separates concerns: one can now configure CDATA-wrapping for *content-based* needs and *field-based* policies independently.
* **Backward Compatible**: The feature is fully optional and does not interfere with existing behavior unless the option is explicitly set.
Commits
-------
c4f363c [Serializer] Add CDATA_WRAPPING_NAME_PATTERN support to XmlEncoderCDATA_WRAPPING_NAME_PATTERN support to XmlEncoder (Maximilian Ruta)File tree
2 files changed
+27
-3
lines changed- src/Symfony/Component/Serializer
- Encoder
- Tests/Encoder
2 files changed
+27
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
| 62 | + | |
62 | 63 | | |
63 | 64 | | |
64 | 65 | | |
| |||
72 | 73 | | |
73 | 74 | | |
74 | 75 | | |
| 76 | + | |
75 | 77 | | |
76 | 78 | | |
77 | 79 | | |
| |||
440 | 442 | | |
441 | 443 | | |
442 | 444 | | |
| 445 | + | |
| 446 | + | |
443 | 447 | | |
444 | | - | |
| 448 | + | |
445 | 449 | | |
446 | | - | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
447 | 454 | | |
448 | 455 | | |
449 | 456 | | |
| |||
471 | 478 | | |
472 | 479 | | |
473 | 480 | | |
474 | | - | |
| 481 | + | |
475 | 482 | | |
476 | 483 | | |
477 | 484 | | |
| |||
Lines changed: 17 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
568 | 568 | | |
569 | 569 | | |
570 | 570 | | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
571 | 588 | | |
572 | 589 | | |
573 | 590 | | |
| |||
0 commit comments