Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Serializer] Add XmlEncoder::CDATA_WRAPPING_PATTERN context option #54155

Closed
@jimmyhayek

Description

@jimmyhayek

Description

With the release of version 6.4, the addition of CDATA wrapping context option was introduced, which is undoubtedly great and much appreciated! #49893

However, I frequently encounter 3rd party XML consumers that require CDATA wrapping for all content. Currently, I solve this requirement with a custom modified XML encoder that enforces CDATA wrapping everywhere. It would be nice if this functionality could be configured through context options of the default XML encoder. Some questions regarding this were already hinted at in the PR for the last feature: #49893

IMHO the most feasible solution appears to be the ability to define a custom pattern for the needsCdataWrapping function to have full control over the CDATA wrapping. It would make it possible to enforce wrapping always or only for specific characters beyond the default ones.

Example

$data = [
    'Hello' => 'John Doe',
];

return new Response(
    $this->serializer->serialize(
        $data,
        XmlEncoder::FORMAT,
        [
            XmlEncoder::CDATA_WRAPPING_PATTERN => '/.*/'
        ]
    ),
    headers: [
        'Content-Type' => 'application/xml'
    ]
);

will output

<?xml version="1.0"?>
<response><Hello><![CDATA[John Doe]]></Hello></response>

instead of

<?xml version="1.0"?>
<response><Hello>John Doe</Hello></response>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions