Allow file resource in Bencode encoder.

This commit is contained in:
Pachira 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 <?php
// Bencode.php // Bencode.php
// Created: 2022-01-13 // Created: 2022-01-13
// Updated: 2023-07-21 // Updated: 2023-09-15
namespace Index\Serialisation; namespace Index\Serialisation;
use InvalidArgumentException;
use RuntimeException; use RuntimeException;
use Index\IO\GenericStream;
use Index\IO\Stream; use Index\IO\Stream;
use Index\IO\TempFileStream; 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 { public static function decode(mixed $input, int $depth = self::DEFAULT_DEPTH, bool $dictAsObject = false): mixed {
if(!($input instanceof Stream)) { if(is_string($input)) {
$input = TempFileStream::fromString($input); $input = TempFileStream::fromString($input);
$input->seek(0); $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) if($depth < 1)
throw new RuntimeException('Maximum depth reached, structure is too dense.'); throw new RuntimeException('Maximum depth reached, structure is too dense.');