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 s9e\Bencode\Exceptions\EncodingException; |
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 EncodingException('Unsupported value', $value); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Encode a PHP 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::encodeInstanceOfArrayObject(new ArrayObject($value)); |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
protected static function encodeBoolean(bool $value): string |
47
|
|
|
{ |
48
|
2 |
|
return static::encodeInteger((int) $value); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
protected static function encodeDouble(float $value): string |
52
|
|
|
{ |
53
|
1 |
|
return static::encodeInteger((int) $value); |
54
|
|
|
} |
55
|
|
|
|
56
|
7 |
|
protected static function encodeInstanceOfArrayObject(ArrayObject $dict): string |
57
|
|
|
{ |
58
|
7 |
|
$vars = $dict->getArrayCopy(); |
59
|
7 |
|
ksort($vars); |
60
|
|
|
|
61
|
7 |
|
$str = 'd'; |
62
|
7 |
|
foreach ($vars as $k => $v) |
63
|
|
|
{ |
64
|
6 |
|
$str .= strlen($k) . ':' . $k . static::encode($v); |
65
|
|
|
} |
66
|
7 |
|
$str .= 'e'; |
67
|
|
|
|
68
|
7 |
|
return $str; |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
protected static function encodeInstanceOfStdClass(stdClass $value): string |
72
|
|
|
{ |
73
|
2 |
|
return static::encodeInstanceOfArrayObject(new ArrayObject(get_object_vars($value))); |
74
|
|
|
} |
75
|
|
|
|
76
|
9 |
|
protected static function encodeInteger(int $value): string |
77
|
|
|
{ |
78
|
9 |
|
return sprintf('i%de', round($value)); |
79
|
|
|
} |
80
|
|
|
|
81
|
4 |
|
protected static function encodeObject(object $value): string |
82
|
|
|
{ |
83
|
4 |
|
$methodName = 'encodeInstanceOf' . str_replace('\\', '', ucwords(get_class($value), '\\')); |
84
|
4 |
|
$callback = get_called_class() . '::' . $methodName; |
85
|
4 |
|
if (is_callable($callback)) |
86
|
|
|
{ |
87
|
3 |
|
return $callback($value); |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
throw new EncodingException('Unsupported value', $value); |
91
|
|
|
} |
92
|
|
|
|
93
|
2 |
|
protected static function encodeString(string $value): string |
94
|
|
|
{ |
95
|
2 |
|
return strlen($value) . ':' . $value; |
96
|
|
|
} |
97
|
|
|
} |