1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package s9e\Bencode |
5
|
|
|
* @copyright Copyright (c) The s9e authors |
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
7
|
|
|
*/ |
8
|
|
|
namespace s9e\Bencode; |
9
|
|
|
|
10
|
|
|
use ArrayObject; |
11
|
|
|
use const SORT_STRING; |
12
|
|
|
use function array_is_list, get_object_vars, gettype, ksort, strlen; |
13
|
|
|
use s9e\Bencode\Exceptions\EncodingException; |
14
|
|
|
use stdClass; |
15
|
|
|
|
16
|
|
|
class Encoder |
17
|
|
|
{ |
18
|
24 |
|
public static function encode(mixed $value): string |
19
|
|
|
{ |
20
|
24 |
|
return match (gettype($value)) |
21
|
24 |
|
{ |
22
|
24 |
|
'array' => static::encodeArray($value), |
23
|
24 |
|
'integer' => "i{$value}e", |
24
|
24 |
|
'object' => static::encodeObject($value), |
25
|
24 |
|
'string' => strlen($value) . ':' . $value, |
26
|
24 |
|
default => static::encode(static::coerceUnsupportedValue($value)) |
27
|
24 |
|
}; |
28
|
|
|
} |
29
|
|
|
|
30
|
2 |
|
protected static function coerceBool(bool $value): int |
31
|
|
|
{ |
32
|
2 |
|
return (int) $value; |
33
|
|
|
} |
34
|
|
|
|
35
|
4 |
|
protected static function coerceFloat(float $value): int |
36
|
|
|
{ |
37
|
4 |
|
$int = (int) $value; |
38
|
4 |
|
if ((float) $int === $value) |
39
|
|
|
{ |
40
|
1 |
|
return $int; |
41
|
|
|
} |
42
|
|
|
|
43
|
3 |
|
throw new EncodingException('Unsupported value', $value); |
44
|
|
|
} |
45
|
|
|
|
46
|
7 |
|
protected static function coerceUnsupportedValue(mixed $value): array|int|string |
47
|
|
|
{ |
48
|
7 |
|
return match (gettype($value)) |
49
|
7 |
|
{ |
50
|
7 |
|
'boolean' => static::coerceBool($value), |
51
|
7 |
|
'double' => static::coerceFloat($value), |
52
|
7 |
|
default => throw new EncodingException('Unsupported value', $value) |
53
|
7 |
|
}; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Encode a PHP array into either a list of a dictionary |
58
|
|
|
*/ |
59
|
7 |
|
protected static function encodeArray(array $value): string |
60
|
|
|
{ |
61
|
7 |
|
return array_is_list($value) |
62
|
3 |
|
? static::encodeIndexedArray($value) |
63
|
7 |
|
: static::encodeAssociativeArray($value); |
64
|
|
|
} |
65
|
|
|
|
66
|
10 |
|
protected static function encodeAssociativeArray(array $array): string |
67
|
|
|
{ |
68
|
10 |
|
ksort($array, SORT_STRING); |
69
|
|
|
|
70
|
10 |
|
$str = 'd'; |
71
|
10 |
|
foreach ($array as $k => $v) |
72
|
|
|
{ |
73
|
8 |
|
$str .= strlen((string) $k) . ':' . $k . static::encode($v); |
74
|
|
|
} |
75
|
10 |
|
$str .= 'e'; |
76
|
|
|
|
77
|
10 |
|
return $str; |
78
|
|
|
} |
79
|
|
|
|
80
|
3 |
|
protected static function encodeIndexedArray(array $array): string |
81
|
|
|
{ |
82
|
3 |
|
$str = 'l'; |
83
|
3 |
|
foreach ($array as $v) |
84
|
|
|
{ |
85
|
2 |
|
$str .= static::encode($v); |
86
|
|
|
} |
87
|
3 |
|
$str .= 'e'; |
88
|
|
|
|
89
|
3 |
|
return $str; |
90
|
|
|
} |
91
|
|
|
|
92
|
6 |
|
protected static function encodeObject(object $value): string |
93
|
|
|
{ |
94
|
6 |
|
if ($value instanceof BencodeSerializable) |
95
|
|
|
{ |
96
|
1 |
|
return static::encode($value->bencodeSerialize()); |
97
|
|
|
} |
98
|
5 |
|
if ($value instanceof ArrayObject) |
99
|
|
|
{ |
100
|
1 |
|
return static::encodeAssociativeArray($value->getArrayCopy()); |
101
|
|
|
} |
102
|
4 |
|
if ($value instanceof stdClass) |
103
|
|
|
{ |
104
|
3 |
|
return static::encodeAssociativeArray(get_object_vars($value)); |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
throw new EncodingException('Unsupported value', $value); |
108
|
|
|
} |
109
|
|
|
} |