Skip to content

DocumentEncoding

The character encoding a document is being populated in, and the conversions between it and UTF-8.

DOM works in UTF-8 internally, but a field name read out of the document and a value written back into it both have to be in the document’s own encoding — otherwise a name like “straße” never matches the submitted parameter, and a populated value arrives mojibaked. Both directions are needed, which is why this is one object rather than two helpers.

ISO-8859-1 is converted with mbstring, which every build has; anything else needs iconv, so an encoding that would silently fail is refused up front instead.

final readonly class DocumentEncoding

SourceUtil/FormPopulation/DocumentEncoding.php
ConstantValueDescription
ISO_8859_1'iso-8859-1'
UTF_8'utf-8'
PropertyTypeDescription
$isUtf8boolreadonly.
$namestringreadonly.
MethodDescription
fromUtf8(mixed $value): mixedConverts a value from UTF-8 to this encoding, recursing into arrays.
named(string $encoding): DocumentEncoding
toUtf8(mixed $value): mixedConverts a value from this encoding to UTF-8, recursing into arrays.
utf8(): DocumentEncoding

public function fromUtf8(mixed $value): mixed

Converts a value from UTF-8 to this encoding, recursing into arrays.

Used on the way out: a value about to be written into the document has to be in the document’s own encoding.

ParameterTypeDescription
$valuemixed

Returns mixed

public static function named(string $encoding): DocumentEncoding

ParameterTypeDescription
$encodingstring

Returns DocumentEncoding

ThrowsWhen
QuioteExceptionif the encoding needs iconv and iconv is absent.

public function toUtf8(mixed $value): mixed

Converts a value from this encoding to UTF-8, recursing into arrays.

Used on the way in: a field name lifted out of the document is in the document’s encoding and has to be UTF-8 to match a submitted parameter.

ParameterTypeDescription
$valuemixed

Returns mixed

public static function utf8(): DocumentEncoding

Returns DocumentEncoding