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 symfony#60355 [Serializer] Add CDATA_WRAPPING_NAME_PATTERN support to XmlEncoder (Maximilian Ruta)
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 XmlEncoder
0 commit comments