Allow file resource in Bencode encoder.

This commit is contained in:
flash 2023-09-15 22:44:36 +00:00
parent 6e4d155536
commit 82a350a5c7
2 changed files with 10 additions and 5 deletions

View file

@ -1 +1 @@
0.2309.152202
0.2309.152244

View file

@ -1,11 +1,13 @@
<?php
// Bencode.php
// Created: 2022-01-13
// Updated: 2023-07-21
// Updated: 2023-09-15
namespace Index\Serialisation;
use InvalidArgumentException;
use RuntimeException;
use Index\IO\GenericStream;
use Index\IO\Stream;
use Index\IO\TempFileStream;
@ -60,11 +62,14 @@ final class Bencode {
}
}
public static function decode(Stream|string $input, int $depth = self::DEFAULT_DEPTH, bool $dictAsObject = false): mixed {
if(!($input instanceof Stream)) {
public static function decode(mixed $input, int $depth = self::DEFAULT_DEPTH, bool $dictAsObject = false): mixed {
if(is_string($input)) {
$input = TempFileStream::fromString($input);
$input->seek(0);
}
} elseif(is_resource($input))
$input = new GenericStream($input);
elseif(!($input instanceof Stream))
throw new InvalidArgumentException('$input must be a string, an Index Stream or a file resource.');
if($depth < 1)
throw new RuntimeException('Maximum depth reached, structure is too dense.');