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-2017 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
|
165 |
|
public function __construct() |
25
|
|
|
{ |
26
|
165 |
|
$this->encoders = [ |
|
|
|
|
27
|
|
|
'binary' => function ($value) { |
28
|
3 |
|
return $this->encodeBinary($value); |
29
|
165 |
|
}, |
30
|
|
|
'octal' => function ($value) { |
31
|
3 |
|
return $this->encodeOctal($value); |
32
|
165 |
|
}, |
33
|
|
|
'decimal' => function ($value, $options) { |
34
|
63 |
|
return $this->encodeDecimal($value, $options); |
35
|
165 |
|
}, |
36
|
165 |
|
'hexadecimal' => function ($value, $options) { |
37
|
6 |
|
return $this->encodeHexadecimal($value, $options); |
38
|
165 |
|
}, |
39
|
|
|
]; |
40
|
165 |
|
} |
41
|
|
|
|
42
|
165 |
|
public function getDefaultOptions() |
43
|
|
|
{ |
44
|
165 |
|
return self::$defaultOptions; |
45
|
|
|
} |
46
|
|
|
|
47
|
150 |
|
public function supports($value) |
48
|
|
|
{ |
49
|
150 |
|
return is_int($value); |
50
|
|
|
} |
51
|
|
|
|
52
|
66 |
|
public function encode($value, $depth, array $options, callable $encode) |
53
|
|
|
{ |
54
|
66 |
|
if (!isset($this->encoders[$options['integer.type']])) { |
55
|
3 |
|
throw new \InvalidArgumentException('Invalid integer encoding type'); |
56
|
|
|
} |
57
|
|
|
|
58
|
63 |
|
$callback = $this->encoders[$options['integer.type']]; |
59
|
|
|
|
60
|
63 |
|
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
|
3 |
|
public function encodeBinary($integer) |
69
|
|
|
{ |
70
|
3 |
|
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
|
3 |
|
public function encodeOctal($integer) |
79
|
|
|
{ |
80
|
3 |
|
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
|
63 |
|
public function encodeDecimal($integer, $options) |
90
|
|
|
{ |
91
|
63 |
|
if ($integer === 1 << (PHP_INT_SIZE * 8 - 1)) { |
92
|
3 |
|
return sprintf('(int)%s%d', $options['whitespace'] ? ' ' : '', $integer); |
93
|
|
|
} |
94
|
|
|
|
95
|
60 |
|
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
|
6 |
|
public function encodeHexadecimal($integer, $options) |
105
|
|
|
{ |
106
|
6 |
|
if ($options['hex.capitalize']) { |
107
|
3 |
|
return sprintf('%s0x%X', $this->sign($integer), abs($integer)); |
108
|
|
|
} |
109
|
|
|
|
110
|
6 |
|
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
|
6 |
|
private function sign($integer) |
119
|
|
|
{ |
120
|
6 |
|
if ($integer < 0) { |
121
|
3 |
|
return '-'; |
122
|
|
|
} |
123
|
|
|
|
124
|
6 |
|
return ''; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..