|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Riimu\Kit\PHPEncoder\Encoder; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Encoder for integer values. |
|
7
|
|
|
* @author Riikka Kalliomäki <[email protected]> |
|
8
|
|
|
* @copyright Copyright (c) 2014-2020 Riikka Kalliomäki |
|
9
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT License |
|
10
|
|
|
*/ |
|
11
|
|
|
class IntegerEncoder implements Encoder |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var array Default values for options in the encoder */ |
|
14
|
|
|
private static $defaultOptions = [ |
|
15
|
|
|
'integer.type' => 'decimal', |
|
16
|
|
|
]; |
|
17
|
|
|
|
|
18
|
|
|
/** @var \Closure[] Encoders for different types of integers */ |
|
19
|
|
|
private $encoders; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* IntegerEncoder constructor. |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->encoders = [ |
|
27
|
|
|
'binary' => function ($value) { |
|
28
|
|
|
return $this->encodeBinary($value); |
|
29
|
|
|
}, |
|
30
|
|
|
'octal' => function ($value) { |
|
31
|
|
|
return $this->encodeOctal($value); |
|
32
|
|
|
}, |
|
33
|
|
|
'decimal' => function ($value, $options) { |
|
34
|
|
|
return $this->encodeDecimal($value, $options); |
|
35
|
|
|
}, |
|
36
|
|
|
'hexadecimal' => function ($value, $options) { |
|
37
|
|
|
return $this->encodeHexadecimal($value, $options); |
|
38
|
|
|
}, |
|
39
|
|
|
]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function getDefaultOptions() |
|
43
|
|
|
{ |
|
44
|
|
|
return self::$defaultOptions; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function supports($value) |
|
48
|
|
|
{ |
|
49
|
|
|
return \is_int($value); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function encode($value, $depth, array $options, callable $encode) |
|
53
|
|
|
{ |
|
54
|
|
|
if (!isset($this->encoders[$options['integer.type']])) { |
|
55
|
|
|
throw new \InvalidArgumentException('Invalid integer encoding type'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$callback = $this->encoders[$options['integer.type']]; |
|
59
|
|
|
|
|
60
|
|
|
return $callback((int) $value, $options); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Encodes an integer into binary representation. |
|
65
|
|
|
* @param int $integer The integer to encode |
|
66
|
|
|
* @return string The PHP code representation for the integer |
|
67
|
|
|
*/ |
|
68
|
|
|
public function encodeBinary($integer) |
|
69
|
|
|
{ |
|
70
|
|
|
return sprintf('%s0b%b', $this->sign($integer), abs($integer)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Encodes an integer into octal representation. |
|
75
|
|
|
* @param int $integer The integer to encode |
|
76
|
|
|
* @return string The PHP code representation for the integer |
|
77
|
|
|
*/ |
|
78
|
|
|
public function encodeOctal($integer) |
|
79
|
|
|
{ |
|
80
|
|
|
return sprintf('%s0%o', $this->sign($integer), abs($integer)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Encodes an integer into decimal representation. |
|
85
|
|
|
* @param int $integer The integer to encode |
|
86
|
|
|
* @param array $options The integer encoding options |
|
87
|
|
|
* @return string The PHP code representation for the integer |
|
88
|
|
|
*/ |
|
89
|
|
|
public function encodeDecimal($integer, $options) |
|
90
|
|
|
{ |
|
91
|
|
|
if ($integer === 1 << (\PHP_INT_SIZE * 8 - 1)) { |
|
92
|
|
|
return sprintf('(int)%s%d', $options['whitespace'] ? ' ' : '', $integer); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return var_export($integer, true); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Encodes an integer into hexadecimal representation. |
|
100
|
|
|
* @param int $integer The integer to encode |
|
101
|
|
|
* @param array $options The integer encoding options |
|
102
|
|
|
* @return string The PHP code representation for the integer |
|
103
|
|
|
*/ |
|
104
|
|
|
public function encodeHexadecimal($integer, $options) |
|
105
|
|
|
{ |
|
106
|
|
|
if ($options['hex.capitalize']) { |
|
107
|
|
|
return sprintf('%s0x%X', $this->sign($integer), abs($integer)); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return sprintf('%s0x%x', $this->sign($integer), abs($integer)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Returns the negative sign for negative numbers. |
|
115
|
|
|
* @param int $integer The number to test for negativity |
|
116
|
|
|
* @return string The minus sign for negative numbers and empty string for positive numbers |
|
117
|
|
|
*/ |
|
118
|
|
|
private function sign($integer) |
|
119
|
|
|
{ |
|
120
|
|
|
if ($integer < 0) { |
|
121
|
|
|
return '-'; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return ''; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|