|
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
|
|
|
public static function encode($value): string |
|
17
|
|
|
{ |
|
18
|
|
|
if (is_scalar($value)) |
|
19
|
|
|
{ |
|
20
|
|
|
return self::encodeScalar($value); |
|
21
|
|
|
} |
|
22
|
|
|
if (is_array($value)) |
|
23
|
|
|
{ |
|
24
|
|
|
return self::encodeArray($value); |
|
25
|
|
|
} |
|
26
|
|
|
if ($value instanceof stdClass) |
|
27
|
|
|
{ |
|
28
|
|
|
$value = new ArrayObject(get_object_vars($value)); |
|
29
|
|
|
} |
|
30
|
|
|
if ($value instanceof ArrayObject) |
|
31
|
|
|
{ |
|
32
|
|
|
return self::encodeArrayObject($value); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
throw new InvalidArgumentException('Unsupported value'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Encode an array into either an array of a dictionary |
|
40
|
|
|
*/ |
|
41
|
|
|
protected static function encodeArray(array $value): string |
|
42
|
|
|
{ |
|
43
|
|
|
if (empty($value)) |
|
44
|
|
|
{ |
|
45
|
|
|
return 'le'; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (array_keys($value) === range(0, count($value) - 1)) |
|
49
|
|
|
{ |
|
50
|
|
|
return 'l' . implode('', array_map([__CLASS__, 'encode'], $value)) . 'e'; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// Encode associative arrays as dictionaries |
|
54
|
|
|
return self::encodeArrayObject(new ArrayObject($value)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Encode given ArrayObject instance into a dictionary |
|
59
|
|
|
*/ |
|
60
|
|
|
protected static function encodeArrayObject(ArrayObject $dict): string |
|
61
|
|
|
{ |
|
62
|
|
|
$vars = $dict->getArrayCopy(); |
|
63
|
|
|
ksort($vars); |
|
64
|
|
|
|
|
65
|
|
|
$str = 'd'; |
|
66
|
|
|
foreach ($vars as $k => $v) |
|
67
|
|
|
{ |
|
68
|
|
|
$str .= strlen($k) . ':' . $k . self::encode($v); |
|
69
|
|
|
} |
|
70
|
|
|
$str .= 'e'; |
|
71
|
|
|
|
|
72
|
|
|
return $str; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Encode a scalar value |
|
77
|
|
|
*/ |
|
78
|
|
|
protected static function encodeScalar($value): string |
|
79
|
|
|
{ |
|
80
|
|
|
if (is_int($value) || is_float($value)) |
|
81
|
|
|
{ |
|
82
|
|
|
return sprintf('i%de', round($value)); |
|
83
|
|
|
} |
|
84
|
|
|
if (is_bool($value)) |
|
85
|
|
|
{ |
|
86
|
|
|
return ($value) ? 'i1e' : 'i0e'; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return strlen($value) . ':' . $value; |
|
90
|
|
|
} |
|
91
|
|
|
} |