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
|
16 |
|
public static function encode($value): string |
17
|
|
|
{ |
18
|
16 |
|
$callback = get_called_class() . '::encode' . ucfirst(gettype($value)); |
19
|
16 |
|
if (is_callable($callback)) |
20
|
|
|
{ |
21
|
15 |
|
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
|
7 |
|
protected static function encodeAssociativeArray(array $array): string |
47
|
|
|
{ |
48
|
7 |
|
ksort($array); |
49
|
|
|
|
50
|
7 |
|
$str = 'd'; |
51
|
7 |
|
foreach ($array as $k => $v) |
52
|
|
|
{ |
53
|
6 |
|
$str .= strlen($k) . ':' . $k . static::encode($v); |
54
|
|
|
} |
55
|
7 |
|
$str .= 'e'; |
56
|
|
|
|
57
|
7 |
|
return $str; |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
protected static function encodeBoolean(bool $value): string |
61
|
|
|
{ |
62
|
2 |
|
return static::encodeInteger((int) $value); |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
protected static function encodeDouble(float $value): string |
66
|
|
|
{ |
67
|
2 |
|
$int = (int) $value; |
68
|
2 |
|
if ((float) $int !== $value) |
69
|
|
|
{ |
70
|
1 |
|
throw new EncodingException('Unsupported value', $value); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
return static::encodeInteger($int); |
74
|
|
|
} |
75
|
|
|
|
76
|
5 |
|
protected static function encodeInstanceOfArrayObject(ArrayObject $dict): string |
77
|
|
|
{ |
78
|
5 |
|
return static::encodeAssociativeArray($dict->getArrayCopy()); |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
|
protected static function encodeInstanceOfStdClass(stdClass $value): string |
82
|
|
|
{ |
83
|
2 |
|
return static::encodeAssociativeArray(get_object_vars($value)); |
84
|
|
|
} |
85
|
|
|
|
86
|
9 |
|
protected static function encodeInteger(int $value): string |
87
|
|
|
{ |
88
|
9 |
|
return sprintf('i%de', round($value)); |
89
|
|
|
} |
90
|
|
|
|
91
|
4 |
|
protected static function encodeObject(object $value): string |
92
|
|
|
{ |
93
|
4 |
|
$methodName = 'encodeInstanceOf' . str_replace('\\', '', ucwords(get_class($value), '\\')); |
94
|
4 |
|
$callback = get_called_class() . '::' . $methodName; |
95
|
4 |
|
if (is_callable($callback)) |
96
|
|
|
{ |
97
|
3 |
|
return $callback($value); |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
throw new EncodingException('Unsupported value', $value); |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
protected static function encodeString(string $value): string |
104
|
|
|
{ |
105
|
2 |
|
return strlen($value) . ':' . $value; |
106
|
|
|
} |
107
|
|
|
} |