EncodeUtil.php
25 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Yethee\Tiktoken\Util; |
| 6 | |
| 7 | use function array_map; |
| 8 | use function bin2hex; |
| 9 | use function hexdec; |
| 10 | use function str_split; |
| 11 | |
| 12 | /** @psalm-type NonEmptyByteVector = non-empty-list<int<0, 255>> */ |
| 13 | final class EncodeUtil |
| 14 | { |
| 15 | /** |
| 16 | * @param non-empty-string $text Text must be valid UTF-8 string. |
| 17 | * |
| 18 | * @psalm-return NonEmptyByteVector |
| 19 | */ |
| 20 | public static function toBytes(string $text): array |
| 21 | { |
| 22 | return array_map(hexdec(...), str_split(bin2hex($text), 2)); |
| 23 | } |
| 24 | } |
| 25 |