Skip to content

Commit

Permalink
Fix conversion of non-utf8 sequences to AnyValue (#1253)
Browse files Browse the repository at this point in the history
String values which are not valid Unicode sequences SHOULD be converted to AnyValue's bytes_value with the bytes representing the string in the original order and format of the source string.
  • Loading branch information
Nevay authored Mar 13, 2024
1 parent 43a1699 commit e82c663
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion AttributesConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,23 @@ public static function convertAnyValue($value): AnyValue
$result->setDoubleValue($value);
}
if (is_string($value)) {
$result->setStringValue($value);
if (self::isUtf8($value)) {
$result->setStringValue($value);
} else {
$result->setBytesValue($value);
}
}

return $result;
}

private static function isUtf8(string $value): bool
{
return \extension_loaded('mbstring')
? \mb_check_encoding($value, 'UTF-8')
: (bool) \preg_match('//u', $value);
}

/**
* Test whether an array is simple (non-KeyValue)
*/
Expand Down

0 comments on commit e82c663

Please sign in to comment.