Skip to content

Commit d866e04

Browse files
committed
Improve documentation
1 parent c1e9947 commit d866e04

File tree

5 files changed

+78
-1
lines changed

5 files changed

+78
-1
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog #
22

3+
## v2.3.0 (2017-07-15) ##
4+
5+
*
6+
37
## v2.2.0 (2017-07-08) ##
48

59
* Increase the minimum PHP version requirement to 5.6

examples/export_difference.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
5+
$precision = 17;
6+
$normalEncoder = new \Riimu\Kit\PHPEncoder\PHPEncoder(['float.precision' => $precision]);
7+
$exportEncoder = new \Riimu\Kit\PHPEncoder\PHPEncoder(['float.export' => true]);
8+
9+
ini_set('serialize_precision', $precision);
10+
11+
$count = 0;
12+
$mindiff = 1;
13+
$maxdiff = 0;
14+
$total = 0;
15+
16+
for ($i = 0; $i <= 2 ** 53; $i++) {
17+
$normal = $normalEncoder->encode($i / 2 ** 53); // $i / 2 ** 53
18+
$export = $exportEncoder->encode($i / 2 ** 53); // $i / 2 ** 53
19+
20+
$normalResult = eval("return $normal;");
21+
$exportResult = eval("return $export;");
22+
23+
if ($normalResult !== $exportResult) {
24+
$diff = abs($normalResult - $exportResult);
25+
26+
$count++;
27+
$mindiff = min($diff, $mindiff);
28+
$maxdiff = max($diff, $maxdiff);
29+
$total += $diff;
30+
$avgdiff = $total / $count;
31+
$pct = round($count / $i * 100, 2);
32+
33+
echo "Different ($count / $i, $pct%): $normal, $export, Diff: $mindiff / $avgdiff / $maxdiff\n";
34+
}
35+
}

src/Encoder/IntegerEncoder.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,32 @@ public function encode($value, $depth, array $options, callable $encode)
6060
return $callback((int) $value, $options);
6161
}
6262

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+
*/
6368
public function encodeBinary($integer)
6469
{
6570
return sprintf('%s0b%s', $this->sign($integer), decbin(abs($integer)));
6671
}
6772

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+
*/
6878
public function encodeOctal($integer)
6979
{
7080
return sprintf('%s0%s', $this->sign($integer), decoct(abs($integer)));
7181
}
7282

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+
*/
7389
public function encodeDecimal($integer, $options)
7490
{
7591
if ($integer === 1 << (PHP_INT_SIZE * 8 - 1)) {
@@ -79,11 +95,21 @@ public function encodeDecimal($integer, $options)
7995
return var_export($integer, true);
8096
}
8197

98+
/**
99+
* Encodes an integer into hexadecimal representation.
100+
* @param int $integer The integer to encode
101+
* @return string The PHP code representation for the integer
102+
*/
82103
public function encodeHexadecimal($integer)
83104
{
84105
return sprintf('%s0x%s', $this->sign($integer), dechex(abs($integer)));
85106
}
86107

108+
/**
109+
* Returns the negative sign for negative numbers.
110+
* @param int $integer The number to test for negativity
111+
* @return string The minus sign for negative numbers and empty string for positive numbers
112+
*/
87113
private function sign($integer)
88114
{
89115
if ($integer < 0) {

src/Encoder/ObjectEncoder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ private function encodeObject($object, array $options, callable $encode)
6666
* @param array $options List of encoder options
6767
* @param callable $encode Callback used to encode values
6868
* @return string The object encoded as string
69+
* @throws \RuntimeException If the object format is invalid
6970
*/
7071
private function encodeObjectArray($object, array $options, callable $encode)
7172
{

src/Encoder/StringEncoder.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public function encode($value, $depth, array $options, callable $encode)
4242
return $this->getSingleQuotedString($value);
4343
}
4444

45+
/**
46+
* Tells if the string is not a valid UTF-8 string.
47+
* @param string $string The string to test
48+
* @param array $options The string encoding options
49+
* @return bool True if the string is not valid UTF-8 and false if it is
50+
*/
4551
private function isBinaryString($string, $options)
4652
{
4753
if (!$options['string.binary']) {
@@ -64,6 +70,11 @@ private function isBinaryString($string, $options)
6470
return !preg_match($pattern, $string);
6571
}
6672

73+
/**
74+
* Encodes the given string into base 64 encoded format.
75+
* @param string $string The string to encode
76+
* @return string A base 64 PHP code representation for the string
77+
*/
6778
private function encodeBinaryString($string)
6879
{
6980
return sprintf("base64_decode('%s')", base64_encode($string));
@@ -110,7 +121,7 @@ function ($matches) {
110121
}
111122

112123
/**
113-
* Encodes all multibyte UTF-8 characters into PHP7 string encoding
124+
* Encodes all multibyte UTF-8 characters into PHP7 string encoding.
114125
* @param string $string The string to encoder
115126
* @return string The string with all the multibyte characters encoded
116127
*/

0 commit comments

Comments
 (0)