Skip to content

Commit 025f1ec

Browse files
committed
Refactor float encoding
1 parent 083136e commit 025f1ec

12 files changed

+326
-145
lines changed

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ php:
66
- 5.4
77

88
before_script:
9-
- composer require "satooshi/php-coveralls dev-master"
9+
- composer require "satooshi/php-coveralls:dev-master"
10+
- composer require "squizlabs/php_codesniffer:2.*"
1011

1112
script:
1213
- mkdir -p build/logs
1314
- phpunit --coverage-clover build/logs/clover.xml
15+
- php vendor/bin/phpcs --standard=PSR2 src
1416

1517
after_script:
1618
- php vendor/bin/coveralls -v

CHANGES.md

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

3+
## v2.1.0 (2015-04-18) ##
4+
5+
* Encoder options with `null` default value will now be recognized
6+
* The integer encoder will now add an `(int)` cast in front of integers, if
7+
their value equals `PHP_INT_MAX * -1 - 1`.
8+
* If `float.integers` is set to `true`, the float encoder will now only encode
9+
floats as integers if the value is accurately represented by the float. Set
10+
the value to `"all"` to restore the previous behavior.
11+
* The float encoder no longer breaks if the PHP locale uses comma as a decimal
12+
separator.
13+
* The float encoder now behaves slightly differently when deciding whether to
14+
use the exponential float notation or not.
15+
* The float encoder now uses `serialize_precision` when the option `precision`
16+
is set to `false`
17+
* Several methods will now throw an InvalidOptionException if any invalid
18+
encoder options have been provided
19+
320
## v2.0.2 (2015-01-21) ##
421

522
* `array.align` will now respect `array.omit` and `array.inline` settings if

README.md

Lines changed: 61 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
# PHP Variable Exporter #
22

3-
*PHPEncoder* is a PHP library for exporting variables into PHP code similar to the
4-
built in function `var_export()`. Compared to the built in function, this
5-
library provides a more customizable alternative that makes it easier to
6-
dynamically create configuration files and cache files in the format you desire.
7-
8-
The purpose of this library is to address some of the problems with
9-
`var_export()`. For example, there are no options to control the amount of
10-
whitespace in the output, which sometimes results in too verbose output. This
11-
library also offers more options in how to export objects.
3+
*PHPEncoder* is a PHP library for exporting variables and generating PHP code
4+
representations for said variables similar to the built in function
5+
`var_export()`. Compared to the built in function, however, this library
6+
provides more options to customize the output, which makes it easier to generate
7+
code for different kinds of purposes such as readable configuration files or
8+
optimized cache files.
9+
10+
The purpose of this library is to address some of the shortcomings with the
11+
built in `var_export()`. For example, there is no way to control the amount of
12+
whitespace in the output and there is no way to choose between different array
13+
notations. This library also provides functionality to convert objects into PHP
14+
code that is actually useful when compared to the built in function.
1215

1316
The large number of customization options in this library allows you to create
1417
code that fits your purposes. You can create very compact code, when you need to
@@ -30,8 +33,8 @@ In order to use this library, the following requirements must be met:
3033

3134
## Installation ##
3235

33-
This library can be installed via [Composer](http://getcomposer.org/). To do
34-
this, download the `composer.phar` and require this library as a dependency. For
36+
This library can be installed via [Composer](http://getcomposer.org/). To do so,
37+
download the `composer.phar` and require this library as a dependency. For
3538
example:
3639

3740
```
@@ -51,12 +54,12 @@ Alternatively, you can add the dependency to your `composer.json` and run
5154
```
5255

5356
Any library that has been installed via Composer can be loaded by including the
54-
`vendor/autoload.php` file that was generated by Composer.
57+
`vendor/autoload.php` file that was generated by Composer during the install.
5558

5659
It is also possible to install this library manually. To do this, download the
5760
[latest release](https://github.com/Riimu/Kit-PHPEncoder/releases/latest) and
58-
extract the `src` folder to your project folder. To load the library, include
59-
the provided `src/autoload.php` file.
61+
extract the `src` folder to your project folder. To load the library, simply
62+
include the provided `src/autoload.php` file.
6063

6164
## Usage ##
6265

@@ -124,71 +127,81 @@ is possible to set these options in three different ways:
124127

125128
* Options can be provided as an array to the `PHPEncoder` constructor.
126129
* Option values can be set via the `setOption()` method.
127-
* Options can be passed as an array as the second argument to the `encode()` method.
130+
* Options can be passed as an array as the second argument to the `encode()`
131+
method.
128132

129133
Note that options passed to the `encode()` method are only temporary and do not
130134
apply to following calls.
131135

132136
#### List of Options ####
133137

134138
* **whitespace** : <boolean> (true)
135-
When set to false, generation of all extra whitespace is disabled and all
139+
When set to `false`, generation of all extra whitespace is disabled and all
136140
other settings that affect whitespace are ignored.
137141

138142
* **null.capitalize** : <boolean> (false)
139-
When set to true, nulls are written in upper case instead of lower case.
143+
When set to `true`, all `null` values are written in upper case instead of
144+
lower case.
140145

141146
* **boolean.capitalize** : <boolean> (false)
142-
When set to true, true and false are written in upper case instead of lower case.
147+
When set to `true`, all `true` and `false` values are written in upper case
148+
instead of lower case.
143149

144-
* **float.integers** : <boolean> (false)
145-
When set to true, float values that represent integers are encoded as
146-
integers instead of floats (e.g. '2.0' will be simply written as '2').
150+
* **float.integers** : <boolean|"all"> (false)
151+
When set to `true`, any float that represents an integer and has a value
152+
that is accurately represented by the floating point number will be encoded
153+
as an integer instead of a float. (e.g. the value `2.0` will be encoded as
154+
`2`). To include the values that are not accurately represented, you may set
155+
option to `"all"`.
147156

148157
* **float.precision** : <integer|false> (17)
149-
Maximum number of decimals in floats. If set to false, the PHP ini setting
150-
'precision' is used instead. Note that due to the way floating point values
151-
work, more than 17 digits does not provide any additional precision.
158+
The maximum precision of encoded floating point values, which usually also
159+
means the maximum number of digits in encoded floats. If the value is set to
160+
`false`, the PHP ini setting `serialize_precision` will be used instead.
161+
Note that due to the way floating point values work, a value greater than 17
162+
does not provide any additional precision.
152163

153164
* **string.escape** : <boolean> (true)
154-
When set to true, all strings containing bytes outside the 32-126 ASCII
165+
When set to `true`, all strings containing bytes outside the 32-126 ASCII
155166
range will be written with double quotes and the characters outside the
156167
range will be escaped.
157168

158169
* **array.short** : <boolean> (true)
159-
When set to true, arrays are enclosed using square brackets `[]` instead of
160-
the `array()` notation.
170+
When set to `true`, arrays are enclosed using square brackets `[]` instead
171+
using of the long array notation `array()`.
161172

162173
* **array.base** : <integer|string> (0)
163-
Base indentation for arrays as number of spaces or as a string. Useful, when
164-
generating code into files with specific level of indentation.
174+
Base indentation for arrays as a number of spaces or as a string. Provides
175+
convenience when you need to output code to a file with specific level of
176+
indentation.
165177

166178
* **array.indent** : <integer|string> (4)
167-
Amount of indentation for single level of indentation as number of spaces or
168-
a string.
179+
Amount of indentation for single level of indentation as a number of spaces
180+
or a string.
169181

170182
* **array.align** : <boolean> (false)
171-
When set to true, array assignment operators `=>` are aligned to the same
183+
When set to `true`, array assignment operators `=>` are aligned to the same
172184
column using spaces. Even if enabled, `array.omit` and `array.inline`
173-
options are still respected, but only if all the keys in the array (or in
174-
any multi level array) can be omitted.
185+
options are still respected, but only if all the keys in the specific array
186+
can be omitted.
175187

176188
* **array.inline** : <boolean|integer> (70)
177-
When set to true, any array that can be written without any array keys will
178-
be written in a single line. If an integer is provided instead, the array
179-
will be written as a single line only if it does not exceed that number of
180-
characters. This option has no effect when `array.omit` is set to false.
189+
When set to `true`, any array that can be written without any array keys
190+
will be written in a single line. If an integer is provided instead, the
191+
array will be written as a single line only if it does not exceed that
192+
number of characters. This option has no effect when `array.omit` is set to
193+
false.
181194

182195
* **array.omit** : <boolean> (true)
183-
When set to true, any redundant array key will not be written. (e.g. the
184-
array `[0 => 'a', 1 => 'b']` would be written just as `['a', 'b']`)
196+
When set to `true`, any redundant array keys will not be included (e.g. the
197+
array `[0 => 'a', 1 => 'b']` would be encoded just as `['a', 'b']`).
185198

186199
* **array.eol** : <string|false> (false)
187-
The end of line character used by array output. When set to false, the
200+
The end of line character used by array output. When set to `false`, the
188201
default `PHP_EOL` will be used instead.
189202

190203
* **object.method** : <boolean> (true)
191-
When set to true, any encoded object will be checked for methods `toPHP()`
204+
When set to `true`, any encoded object will be checked for methods `toPHP()`
192205
and `toPHPValue()`. If the method `toPHP()` exists, the returned string will
193206
be used as the PHP code representation of the object. If the method
194207
`toPHPValue()` exists instead, the returned value will be encoded as PHP and
@@ -206,19 +219,19 @@ apply to following calls.
206219

207220
* **object.cast** : <boolean> (true)
208221
Whether to add an `(object)` cast in front of arrays generated from objects
209-
when using `vars`, `array` or `iterate` formats.
222+
or not when using the object encoding formats `vars`, `array` or `iterate`.
210223

211224
* **recursion.detect** : <boolean> (true)
212-
When set to true, the encoder will attempt to detect circular references in
213-
arrays and objects to avoid infinite loops.
225+
When set to `true`, the encoder will attempt to detect circular references
226+
in arrays and objects to avoid infinite loops.
214227

215228
* **recursion.ignore** : <boolean> (false)
216-
When set to true, any circular reference will be replaced with null instead
217-
of throwing an exception.
229+
When set to `true`, any circular reference will be replaced with `null`
230+
instead of throwing an exception.
218231

219232
* **recursion.max** : <integer|false> (false)
220233
Maximum number of levels when encoding arrays and objects. Exception is
221-
thrown when the maximum is exceeded. Set to false to have no limit.
234+
thrown when the maximum is exceeded. Set to `false` to have no limit.
222235

223236
## Credits ##
224237

apigen.neon

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ charset:
77

88
main: Riimu\Kit\PHPEncoder
99
title: PHPEncoder
10-
php: false
10+
php: true
1111
tree: true
12+
templateTheme: bootstrap

src/Encoder/ArrayEncoder.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function encode($value, $depth, array $options, callable $encode)
5050
/**
5151
* Returns the PHP code for aligned array accounting for omitted keys and inlined arrays.
5252
* @param array $array Array to encode
53-
* @param integer $depth Current indentation depth of the output
53+
* @param int $depth Current indentation depth of the output
5454
* @param array $options List of encoder options
5555
* @param callable $encode Callback used to encode values
5656
* @return string The PHP code representation for the array
@@ -75,7 +75,7 @@ private function getAlignedArray(array $array, $depth, array $options, callable
7575
/**
7676
* Returns the PHP code for the array as inline or multi line array.
7777
* @param array $array Array to encode
78-
* @param integer $depth Current indentation depth of the output
78+
* @param int $depth Current indentation depth of the output
7979
* @param array $options List of encoder options
8080
* @param callable $encode Callback used to encode values
8181
* @return string The PHP code representation for the array
@@ -117,7 +117,7 @@ private function getInlineArray(array $lines, array $options)
117117
/**
118118
* Builds the complete array from the encoded key and value pairs.
119119
* @param string[] $lines Encoded key and value pairs
120-
* @param integer $depth Current indentation depth of the output
120+
* @param int $depth Current indentation depth of the output
121121
* @param array $options List of encoder options
122122
* @return string Array encoded as PHP code
123123
*/
@@ -136,7 +136,7 @@ private function buildArray(array $lines, $depth, array $options)
136136
/**
137137
* Wraps the array code using short or long array notation.
138138
* @param string $string Array string representation to wrap
139-
* @param boolean $short True to use short notation, false to use long notation
139+
* @param bool $short True to use short notation, false to use long notation
140140
* @return string The array wrapped appropriately
141141
*/
142142
private function wrap($string, $short)
@@ -148,7 +148,7 @@ private function wrap($string, $short)
148148
* Builds the indentation based on the options.
149149
* @param string|integer $base The base indentation
150150
* @param string|integer $indent A single indentation level
151-
* @param integer $depth The level of indentation
151+
* @param int $depth The level of indentation
152152
* @return string The indentation for the current depth
153153
*/
154154
private function buildIndent($base, $indent, $depth)
@@ -191,9 +191,9 @@ private function getAlignedPairs(array $array, callable $encode)
191191
* Returns each key and value pair encoded as array assignment.
192192
* @param array $array Array to convert into code
193193
* @param string $space Whitespace between array assignment operator
194-
* @param boolean $omit True to omit unnecessary keys, false to not
194+
* @param bool $omit True to omit unnecessary keys, false to not
195195
* @param callable $encode Callback used to encode values
196-
* @param boolean $omitted Set to true, if all the keys were omitted, false otherwise
196+
* @param bool $omitted Set to true, if all the keys were omitted, false otherwise
197197
* @return string[] Each of key and value pair encoded as php
198198
*/
199199
private function getPairs(array $array, $space, $omit, callable $encode, & $omitted = true)
@@ -217,8 +217,8 @@ private function getPairs(array $array, $space, $omit, callable $encode, & $omit
217217

218218
/**
219219
* Tells if the key can be omitted from array output based on expected index.
220-
* @param integer|string $key Current array key
221-
* @param integer $nextIndex Next expected key that can be omitted
220+
* @param int|string $key Current array key
221+
* @param int $nextIndex Next expected key that can be omitted
222222
* @return bool True if the key can be omitted, false if not
223223
*/
224224
private function canOmitKey($key, & $nextIndex)

src/Encoder/Encoder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
interface Encoder
1212
{
1313
/**
14-
* Returns a list of options and their default values as associative array.
14+
* Returns a list of options and their default values as an associative array.
1515
* @return array List of options and their default values
1616
*/
1717
public function getDefaultOptions();
@@ -24,9 +24,9 @@ public function getDefaultOptions();
2424
public function supports($value);
2525

2626
/**
27-
* Generates to code for the given value.
27+
* Generates the PHP code representation for the given value.
2828
* @param mixed $value Value to encode
29-
* @param integer $depth Current indentation depth of the output
29+
* @param int $depth Current indentation depth of the output
3030
* @param array $options List of encoder options
3131
* @param callable $encode Callback used to encode values
3232
* @return string The PHP code that represents the given value

0 commit comments

Comments
 (0)