[Serializer] Added XML attributes support for DomDocument in XmlEncoder.

This is a rebase and refactoring of #8424.

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #8424
| License       | MIT
| Doc PR        | -

| New Code | Result
| --- | ---
| Code coverage | 100%
| PSR-2 | No violations
| PHP-CS-Fixer | No changes