Skip to content

Commit c1e9947

Browse files
committed
Add documentation
1 parent 35d12bb commit c1e9947

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

src/Encoder/FloatEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ private function encodeFloat($float, $precision)
121121
{
122122
$log = (int) floor(log(abs($float), 10));
123123

124-
if (abs($float) < self::FLOAT_MAX && $log > -5 && abs($log) < $precision) {
124+
if ($log > -5 && abs($float) < self::FLOAT_MAX && abs($log) < $precision) {
125125
return $this->formatFloat($float, $precision - $log - 1);
126126
}
127127

src/Encoder/IntegerEncoder.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,15 @@ class IntegerEncoder implements Encoder
1515
'integer.type' => 'decimal',
1616
];
1717

18-
public function getDefaultOptions()
19-
{
20-
return self::$defaultOptions;
21-
}
22-
23-
public function supports($value)
24-
{
25-
return is_int($value);
26-
}
18+
/** @var \Closure[] Encoders for different types of integers */
19+
private $encoders;
2720

28-
public function encode($value, $depth, array $options, callable $encode)
21+
/**
22+
* IntegerEncoder constructor.
23+
*/
24+
public function __construct()
2925
{
30-
$encoders = [
26+
$this->encoders = [
3127
'binary' => function ($value) {
3228
return $this->encodeBinary($value);
3329
},
@@ -41,12 +37,25 @@ public function encode($value, $depth, array $options, callable $encode)
4137
return $this->encodeHexadecimal($value);
4238
},
4339
];
40+
}
4441

45-
if (!isset($encoders[$options['integer.type']])) {
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']])) {
4655
throw new \InvalidArgumentException('Invalid integer encoding type');
4756
}
4857

49-
$callback = $encoders[$options['integer.type']];
58+
$callback = $this->encoders[$options['integer.type']];
5059

5160
return $callback((int) $value, $options);
5261
}

src/Encoder/StringEncoder.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ private function getSingleQuotedString($string)
8282
/**
8383
* Returns the string wrapped in double quotes and all but print characters escaped.
8484
* @param string $string String to wrap and escape
85+
* @param array $options String encoding options
8586
* @return string The string wrapped in double quotes and escape correctly
8687
*/
8788
private function getDoubleQuotedString($string, $options)
@@ -108,6 +109,11 @@ function ($matches) {
108109
));
109110
}
110111

112+
/**
113+
* Encodes all multibyte UTF-8 characters into PHP7 string encoding
114+
* @param string $string The string to encoder
115+
* @return string The string with all the multibyte characters encoded
116+
*/
111117
private function encodeUtf8($string)
112118
{
113119
$pattern =
@@ -124,6 +130,11 @@ private function encodeUtf8($string)
124130
}, $string);
125131
}
126132

133+
/**
134+
* Returns the unicode code point for the given multibyte UTF-8 character.
135+
* @param string $bytes The multibyte character
136+
* @return int The code point for the multibyte character
137+
*/
127138
private function getCodePoint($bytes)
128139
{
129140
if (strlen($bytes) === 2) {

tests/tests/EncodingTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ public function testUtf8String()
240240
$encoder = new PHPEncoder(['string.utf8' => true]);
241241

242242
$this->assertEncode('"\nA"', "\nA", $encoder);
243+
$this->assertEncode('"\nA\u{c4}\x00"', "\n\x00", $encoder);
243244

244245
if (version_compare(PHP_VERSION, '7', '<')) {
245246
$this->assertSame('"\u{a2}"', $encoder->encode("\xC2\xA2"));

0 commit comments

Comments
 (0)