1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package s9e\Bencode |
5
|
|
|
* @copyright Copyright (c) 2014-2020 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 InvalidArgumentException; |
12
|
|
|
use stdClass; |
13
|
|
|
|
14
|
|
|
class Encoder |
15
|
|
|
{ |
16
|
15 |
|
public static function encode($value): string |
17
|
|
|
{ |
18
|
15 |
|
$callback = get_called_class() . '::encode' . ucfirst(gettype($value)); |
19
|
15 |
|
if (is_callable($callback)) |
20
|
|
|
{ |
21
|
14 |
|
return $callback($value); |
22
|
|
|
} |
23
|
|
|
|
24
|
1 |
|
throw new InvalidArgumentException('Unsupported xvalue'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Encode an array into either an array of a dictionary |
29
|
|
|
*/ |
30
|
5 |
|
protected static function encodeArray(array $value): string |
31
|
|
|
{ |
32
|
5 |
|
if (empty($value)) |
33
|
|
|
{ |
34
|
1 |
|
return 'le'; |
35
|
|
|
} |
36
|
|
|
|
37
|
4 |
|
if (array_keys($value) === range(0, count($value) - 1)) |
38
|
|
|
{ |
39
|
2 |
|
return 'l' . implode('', array_map(get_called_class() . '::encode', $value)) . 'e'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// Encode associative arrays as dictionaries |
43
|
4 |
|
return static::encodeArrayObject(new ArrayObject($value)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Encode given ArrayObject instance into a dictionary |
48
|
|
|
*/ |
49
|
7 |
|
protected static function encodeArrayObject(ArrayObject $dict): string |
50
|
|
|
{ |
51
|
7 |
|
$vars = $dict->getArrayCopy(); |
52
|
7 |
|
ksort($vars); |
53
|
|
|
|
54
|
7 |
|
$str = 'd'; |
55
|
7 |
|
foreach ($vars as $k => $v) |
56
|
|
|
{ |
57
|
6 |
|
$str .= strlen($k) . ':' . $k . static::encode($v); |
58
|
|
|
} |
59
|
7 |
|
$str .= 'e'; |
60
|
|
|
|
61
|
7 |
|
return $str; |
62
|
|
|
} |
63
|
|
|
|
64
|
4 |
|
protected static function encodeObject(object $value): string |
65
|
|
|
{ |
66
|
4 |
|
if ($value instanceof stdClass) |
67
|
|
|
{ |
68
|
2 |
|
$value = new ArrayObject(get_object_vars($value)); |
69
|
|
|
} |
70
|
|
|
|
71
|
4 |
|
if ($value instanceof ArrayObject) |
72
|
|
|
{ |
73
|
3 |
|
return static::encodeArrayObject($value); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
throw new InvalidArgumentException('Unsupported value'); |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
protected static function encodeBoolean(bool $value): string |
80
|
|
|
{ |
81
|
2 |
|
return static::encodeInteger((int) $value); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
protected static function encodeDouble(float $value): string |
85
|
|
|
{ |
86
|
1 |
|
return static::encodeInteger((int) $value); |
87
|
|
|
} |
88
|
|
|
|
89
|
9 |
|
protected static function encodeInteger(int $value): string |
90
|
|
|
{ |
91
|
9 |
|
return sprintf('i%de', round($value)); |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
protected static function encodeString(string $value): string |
95
|
|
|
{ |
96
|
2 |
|
return strlen($value) . ':' . $value; |
97
|
|
|
} |
98
|
|
|
} |