index/src/Serialisation/Base64.php

34 lines
789 B
PHP
Raw Normal View History

2022-09-13 13:13:11 +00:00
<?php
2023-07-21 21:47:41 +00:00
// Base64.php
2022-09-13 13:13:11 +00:00
// Created: 2022-01-13
2023-07-21 21:47:41 +00:00
// Updated: 2023-07-21
2022-09-13 13:13:11 +00:00
namespace Index\Serialisation;
use Index\IO\Stream;
/**
* Provides a Base64 serialiser.
*/
2023-07-21 21:47:41 +00:00
final class Base64 {
2022-09-13 13:13:11 +00:00
/**
* Encodes binary data as a Base64 string.
*
2023-07-21 21:47:41 +00:00
* @param Stream|string $input Input binary data.
2022-09-13 13:13:11 +00:00
* @return string Base64 string representing the binary data.
*/
2023-07-21 21:47:41 +00:00
public static function encode(Stream|string $input): string {
2022-09-13 13:13:11 +00:00
return base64_encode((string)$input);
}
/**
* Decodes a Base64 string back to binary data.
*
* @param Stream|string $input Input Base64 string.
2023-07-21 21:47:41 +00:00
* @return string|false Binary data.
2022-09-13 13:13:11 +00:00
*/
2023-07-21 21:47:41 +00:00
public static function decode(Stream|string $input): string|bool {
2022-09-13 13:13:11 +00:00
return base64_decode((string)$input);
}
}