-
Type: Improvement
-
Resolution: Won't Do
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
Creating a Binary UUID from a string is currently somewhat painful, as the typical representation of a UUID cannot be used directly. This requires glue code:
$uuid = '0e18d0d3-ed38-4e7f-a18e-507a78ad901d'; $binaryUuid = new Binary(hex2bin(str_replace('-', '', $uuid)), Binary::TYPE_UUID);
Similarly, converting a binary UUID back to a string representation is equally difficult, as both the getData() method and the __toString() magic method return binary data:
php > var_dump((string) $binaryUuid); php shell code:1: string(16) "???8N??Pzx??" php > var_dump($binaryUuid->getData()); php shell code:1: string(16) "???8N??Pzx??"
The creation of a Binary UUID could be simplified by offering a factory that takes a string representation of a UUID to return a Binary UUID object:
$binaryUuid = Binary::uuid('0e18d0d3-ed38-4e7f-a18e-507a78ad901d');
As getData() should always return the binary data, the __toString() method could be repurposed to output a formatted UUID:
php > var_dump((string) $binaryUuid);
php shell code:1:
string(36) "0e18d0d3-ed38-4e7f-a18e-507a78ad901d"
This repurposing of __toString() could extend to all other types as well, either returning base64 encoded data or hex data (e.g. for MD5 hashes). Note that while some people may consider the behavioural changes in __toString() a BC break, one could argue that the string representation of a value object is not covered by the backward compatibility promise. Either way, we should carefully weigh the pros and cons of making such a change.
- is related to
-
PHPC-1988 Report "data" as hex bytes in MongoDB\BSON\Binary debug output
- Backlog